gpt4 book ai didi

php - 使用php显示数据库中的重复条目

转载 作者:行者123 更新时间:2023-11-29 20:46:28 25 4
gpt4 key购买 nike

我正在尝试显示数据库中搜索功能的数据,但不幸的是我得到了重复的条目,任何人都可以检查一下并帮助我一点:

我正在使用此查询和此 php 代码,其中 startdateenddate 来自 members 表,所有其他数据均来自 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>';

这就是我得到的输出:所以我只想显示在姓名为 aa 且姓氏 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!';
}
}
?>

我修改了这段代码,但似乎我犯了错误,而且我找不到。在能够编辑姓名后,我还将添加其他字段。

最佳答案

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

$query = "select * FROM person, members where fname= '$fname' AND lname = '$lname' GROUP BY person.person_id";

关于php - 使用php显示数据库中的重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38412553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com