0

现在我有一个显示菜单项的表,我希望管理员能够删除和编辑特定的行项目。如果我的 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>";
4

2 回答 2

1

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

<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>";
于 2012-10-10T17:57:50.930 回答
0

在表单中添加一个隐藏字段 - id:

 while($row = mysql_fetch_assoc($result)) {
    //your <td>'s here

           echo '<input type="hidden" name="id" value="{$row[id]}">';
    // echoes for form submit
 }

注意:不推荐使用 mysql_* 函数。您应该改用 mysqli_ 函数。在这里阅读

于 2012-10-10T17:55:35.957 回答