PHP + MySQL - 如何以单一形式发布2个值

时间:2017-04-06 16:21:39

标签: php mysql post

我正在尝试建立一个非常基本的库存管理网站供我个人使用。我在尝试构建'删除'时遇到问题。功能到我的退货项目列表中。

这是来自可见网页的PHP脚本:

<form action='actions.php' method='post'>
<?php

ini_set('display_errors', 1);

//Create Connection
$conn = new mysqli($servername, $username, $password, $dbname);

//Check Connection
if ($conn->connect_error) {
    die("Connection Failed: " . $conn->connect_error);
    }

$sql = "SELECT * FROM computers WHERE status='Available'";
$result = $conn->query($sql);


if ($result->num_rows > 0) {
    echo "<table id='unassigned'>
            <tr>
                <th onclick='sortTable(0)'>Manufacturer</th>
                <th onclick='sortTable(1)'>Model</th>
                <th onclick='sortTable(2)'>Serial Number</th>
                <th onclick='sortTable(3)'>CPU</th>
                <th onclick='sortTable(4)'>Memory</th>
                <th onclick='sortTable(5)'>HDD</th>
                <th onclick='sortTable(6)'>Status</th>
                <th onclick='sortTable(7)'>Assignee</th>
                <th onclick='sortTable(8)'>Date Assigned</th>
                <th onclick='sortTable(9)'>Date Returned</th>
                <th>ID - TESTING ONLY</th>
                <th>Actions</th>
            </tr>";
    //output data of each row

    while($row = $result->fetch_assoc()) 
    { $id = $row["sn"];

    {
        echo "<tr>
                <td>".$row["manufacturer"]."</td>
                <td>".$row["model"]."</td>
                <td>".$row["sn"]."</td>
                <td>".$row["CPU"]."</td>
                <td>".$row["memory"]."</td>
                <td>".$row["hdd"]."</td>
                <td>".$row["status"]."</td>
                <td>".$row["assignee"]."</td>
                <td>".$row["dateout"]."</td>
                <td>".$row["datein"]."</td>
                <td>" . " <input type='hidden' name = 'id' value= " . $row["sn"] . "> " . "</td>
                <td>" . " <input type='submit' name = 'delete' . ' value='Delete' >" . "</td>
            </tr>";
        }

    }   

    echo "</table>";
    }

    else {
    echo "0 results";
    }
$conn->close();
?>
</form>

这就是&action; .php&#39;文件:

<?php
$delete = $_POST["delete"];
$id = $_POST["id"];

ini_set('display_errors', 1);

//Create Connection
$conn = new mysqli($servername, $username, $password, $dbname);

//Check Connection
if ($conn->connect_error) {
    die("Connection Failed: " . $conn->connect_error);
    }

if (isset($_POST['id'])){
    echo "ID is set and it is" . $id;
    }
else {
    echo "Where is the ID you numpty";
    }

if (isset($_POST['delete'])){
    $sql = "DELETE FROM computers WHERE sn= $id";  
    echo "Deleting Stuff";
    //if ($conn->query($sql === TRUE) {
        //echo "Record deleted successfully";
        //}
   //else {
        //echo "Error deleting record: " . $conn->error;
        //}

}

$conn->close();
?>

(请忽略我放入的各种测试内容)

我当然遇到的问题是我已定义:

name = "id"

在我的桌子的每一行上,所以当我按下网页上的任何删除按钮时,在actions.php页面上返回的ID就是最后一行$ [&#39; sn&#39;] ,无论可能是什么......但我想不出为每个ID 动态分配不同名称的方法,然后在actions.php中抓住它

任何人都可以就如何将SN字段传递到单独文件中的删除查询提供一些指导吗?

谢谢!

0 个答案:

没有答案