gpt4 book ai didi

php - 使用 PHP 获取 mySQL 行项目 ID

转载 作者:行者123 更新时间:2023-11-29 14:15:00 25 4
gpt4 key购买 nike

现在我有一个显示菜单项的表格,我希望管理员能够删除和编辑特定的行项目。如果我的 id 等于特定数字,我的 delete.php 代码可以正常工作,但我希望 id 等于删除按钮所在行的 id。所以我的问题是如何获取该 id 数字?因为我现在做的事情是不正确的。

这是我的delete.php 文件:

<?php 
$con = mysql_connect("localhost", "root", "");
if(!$con)
{
die('Could not connect: ' .mysql_error());
}
mysql_select_db("bics_place", $con);

echo "mysql connected";

$myid = $_POST['id'];

$sql = "DELETE FROM menu WHERE id='$myid'";

echo "sql = $sql";

if(!mysql_query($sql, $con))
{
die('Error: ' .mysql_Error());
}
echo "1 record deleted";
header("location:admin_menu.php");

mysql_close($con);

?>

这是在 admin_menu.php 中制作的表格

            $result = mysql_query("SELECT * FROM menu");

echo "<table border='1' id='menu'>
<form method='post' action='delete.php'>
<tr>
<th> Id </th>
<th> Type </th>
<th> Product Name </th>
<th> Price </th>
<th></th>
<th></th>
</tr>";


while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['productName'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . '<input type="submit" name="delete" value="Delete">' . "</td>";
echo "<td>" . '<input type="submit" name="edit" value="Edit">' . "</td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";

最佳答案

在表单中获取隐藏字段。然后在提交之前将按钮 onclick 事件将 id 设置为隐藏字段。

<script>
function setId(idValue){
document.getElementById('myid').value=idValue;
}
</script>
echo "<table border='1' id='menu'>
<form method='post' action='delete.php'>
<input type="hidden" name="id" id="myid" value="" />
<tr>
<th> Id </th>
<th> Type </th>
<th> Product Name </th>
<th> Price </th>
<th></th>
<th></th>
</tr>";


while($row = mysql_fetch_assoc($result))
{
$myID = $row["id"];

echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['productName'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . '<input type="submit" name="delete" value="Delete" onClick="setId('.$myID.');">' . "</td>";
echo "<td>" . '<input type="submit" name="edit" value="Edit">' . "</td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";

关于php - 使用 PHP 获取 mySQL 行项目 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12825351/

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