【问题标题】:Get a mySQL line item id with PHP使用 PHP 获取 mySQL 行项目 ID
【发布时间】:2012-10-10 17:50:05
【问题描述】:

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

【问题讨论】:

    标签: php mysql html-table


    【解决方案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>";
    

    【讨论】:

      【解决方案2】:

      在表单中添加一个隐藏字段-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_ 函数。阅读here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-28
        • 1970-01-01
        • 2011-10-02
        • 2016-01-19
        • 2022-09-23
        相关资源
        最近更新 更多