我正在尝试在数据库中显示来自搜索功能的数据但不幸的是我得到重复的条目,任何人都可以检查这一点并帮助我一点:
我正在使用此查询和此php代码,其中 startdate
和 enddate
来自 members
< / em>表,所有其他数据来自 person
表:
$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>';
这就是我得到的结果:所以我想只在姓名和姓氏bb的人身上显示(不是五次但只有一次)。
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
after searching during the edit process the ID is not changing at all
<?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!';
}
}
?>
我修改了这段代码,但似乎我犯了错误而且我找不到。在能够编辑名称和姓氏后,我还将添加其他字段。
答案 0 :(得分:0)
我假设您的人员表中有主要/唯一密钥
$query = "select * FROM person, members where fname= '$fname' AND lname = '$lname' GROUP BY person.person_id";