简体   繁体   English

PHP表单更新mysql数据库在编辑一条记录后返回所有数据库记录

[英]PHP form update mysql database returns all database records after editing one record

so I have this code that other Stack members have helped me fine tune and correct some errors, the code all works as it should but there is one small detail, after successfully editing one record and clicking the update button ALL of the existing records that are in the database load into the page. 因此,我得到了该代码,其他Stack成员也帮助我进行了微调和更正了一些错误,这些代码都可以正常工作,但是有一个小细节,在成功编辑一条记录并单击更新按钮后,在数据库中加载到页面中。 Here is my code below: 这是我的代码如下:

<?php 

$con = mysql_connect("localhost", "root", "M1q2w3e4r");
if (!$con) {
die("Can not connect: " . mysql_error());
}
mysql_select_db("inventory",$con);

if (isset($_POST['update'])){
$UpdateQuery = "UPDATE invoice SET `inv_number`='$_POST[inv_number]', `from_date`='$_POST[from_date]', `to_date`='$_POST[to_date]',`date_type`='$_POST[date_type]', `notes`='$_POST[notes]' WHERE id='$_POST[id]'";               
mysql_query($UpdateQuery, $con);
};

$where = '';
    if(!empty($_GET) && !empty($_GET['edit'])) {
        $where = ' where id='.$_GET['edit'];
    }
    $sql = "SELECT * FROM invoice".$where;

$myData = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>Inv #</th>
<th>From</th>
<th>To</th>
<th>Type</th>
<th>Notes</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action='edit.php' method='post'>";
echo "<tr>";
echo "<td>" . "<input type='text' name='inv_number' value='" . $record['inv_number'] . "'> </td>";
echo "<td>" . "<input type='text' id='from' name='from_date' value='" . $record['from_date'] . "'> </td>";
echo "<td>" . "<input type='text' id='to' name='to_date' value='" . $record['to_date'] . "'> </td>";
echo "<td>" . "<input type='text' name='date_type' value='" . $record['date_type'] . "'> </td>";
echo "<td>" . "<input type='text' name='notes' value='" . $record['notes'] . "'> </td>";
echo "<td>" . "<input type='hidden' name='id' value='" . $record['id'] . "'> </td>";
echo "<td>" . "<input type='hidden' name='hidden' value='" . $record['id'] . "'> </td>";
echo "<td>" . "<input type='submit' name='update' value='update'>" . " </td>";
echo "</tr>";
echo "</form>";
}

echo "</table>";
mysql_close($con);

?>

I know it has to do with the form action="edit.php", as it refreshes the page the id number in the url is pulled out. 我知道这与action =“ edit.php”形式有关,因为它刷新了页面中URL中的ID号。 So I tried this: 所以我尝试了这个:

echo "<form action='edit.php?edit=<?php echo $_REQUEST["id"]; ?>' method='post'>";

but this only led to my edit.php to display as a blank page. 但这只会导致我的edit.php显示为空白页。 If anyone can help me figure out how to prevent all the database records from being displayed in the page after clicking the update button it would really help. 如果有人可以帮助我找出在单击更新按钮后如何防止所有数据库记录显示在页面中的方法,那将非常有帮助。

I might do this, for example, if I just wanted to show the record that was just updated: 例如,如果我只想显示刚刚更新的记录,我可以这样做:

if (isset($_POST['update'])){
    $UpdateQuery = "UPDATE invoice SET `inv_number`='$_POST[inv_number]',    `from_date`='$_POST[from_date]', `to_date`='$_POST[to_date]',`date_type`='$_POST[date_type]', `notes`='$_POST[notes]' WHERE id='$_POST[id]'";               
    mysql_query($UpdateQuery, $con);
    $where = ' where id='.$_POST[id];
}
else {
    $where = '';
    if(!empty($_GET) && !empty($_GET['edit'])) {
        $where = ' where id='.$_GET['edit'];
    }
}

You could also use REQUEST instead of GET and make a hidden input field with the name "edit" in your form. 您也可以使用REQUEST而不是GET,并在表单中创建一个名称为“ edit”的隐藏输入字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM