首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用php显示来自数据库的重复条目

用php显示来自数据库的重复条目
EN

Stack Overflow用户
提问于 2016-07-16 15:23:11
回答 1查看 59关注 0票数 0

我试图在数据库中显示搜索函数中的数据,但不幸的是,我得到了重复的条目,谁能检查一下这个,并帮助我一点:

我使用这个查询和这个php代码,其中startdateenddate来自members表,所有其他数据都来自person表:

代码语言:javascript
复制
$query = "select * FROM person, members where fname= '$fname' AND lname = '$lname'";

for ($i=0; $i <$num_results; $i++)
  {
     $row = $result->fetch_assoc();
     echo '<p><strong>'.($i+1).'. Name: ';
     echo htmlspecialchars(stripslashes($row['fname']));
     echo '</strong><br />Surname: ';
     echo stripslashes($row['lname']);
     echo '<br />Board: ';
     echo stripslashes($row['board']);
     echo '<br />Department: ';
     echo stripslashes($row['departmentname']);
     echo '<br />Start Date: ';
     echo stripslashes($row['startdate']);
     echo '<br />End Date: ';
     echo stripslashes($row['enddate']);
     echo '</p>';

--这就是我输出的内容:所以我只想被显示在名为aa和姓bb的人身上(不是五次,而是只有一次)。

代码语言:javascript
复制
1. Name: aa
Surname: bb
Board: Secur
Department: Telec
Start Date: 2016-07-01
Edn Date: 2016-07-31

2. Name: aa
Surname: bb
Board: Secur
Department: Telec
Start Date: 2016-07-19
Edn Date: 2016-07-21

在编辑过程中进行搜索之后,ID根本不会更改。

代码语言:javascript
复制
<?php
function renderForm($personid, $personname, $personsurname, $error)
{
?>
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="personid" value="<?php echo $personid; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $personid; ?></p>
<strong>First Name: *</strong> <input type="text" name="personname" value="<?php echo $personname; ?>"/><br/>
<strong>Last Name: *</strong> <input type="text" name="personsurname" value="<?php echo $personsurname; ?>"/><br/>
<p>* Required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database

$host = "localhost"; 
$user = "kkoikm_kum"; 
$pass = "datgbhnkum"; 
$db = "koikm_kum";

// open connection 
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 

// select database 
mysql_select_db($db) or die ("Unable to select database!");

// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['personid']))
{
// get form data, making sure it is valid
$personid = $_POST['personid'];
$personname = mysql_real_escape_string(htmlspecialchars($_POST['personname']));
$personsurname = mysql_real_escape_string(htmlspecialchars($_POST['personsurname']));

// check that firstname/lastname fields are both filled in
if ($personname == '' || $personsurname == '')

// generate error message
$error = 'ERROR: Please fill in all required fields!';

//error, display form
renderForm($personid, $personname, $personsurname, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE tblperson SET personname='$personname', personsurname='$personsurname' WHERE personid='$personid'")
or die(mysql_error());

// once saved, redirect back to the view page
header("Location: home.php");
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'Error!';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['personid']) && is_numeric($_GET['personid']) && $_GET['personid'] > 0)
{
// query db
$personid = $_GET['personid'];
$result = mysql_query("SELECT * FROM tblperson WHERE personid=$personid")
or die(mysql_error());
$row = mysql_fetch_array($result);

// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$personname = $row['personname'];
$personsurname = $row['personsurname'];

// show form
renderForm($personid, $personname, $personsurname, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!';
}
}
?>

我已经修改了这个代码,但似乎我犯了错误,我找不到。在能够编辑名称和姓氏之后,我还将添加其他字段。

EN

回答 1

Stack Overflow用户

发布于 2016-07-16 15:27:06

我假设您的person表中有主/唯一键

代码语言:javascript
复制
$query = "select * FROM person, members where fname= '$fname' AND lname = '$lname' GROUP BY person.person_id";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38412553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档