试图使用PHP和mySQL创建一个可编辑的HTML表,但该表失败了;t更新


Trying to create an editable HTML table using PHP and mySQL but the table won't update

我正在尝试制作一个HTML表作为mySQL数据库的前端。该表显示良好,我可以键入我想对表的每一行进行的编辑,但当我按下提交按钮时,实际上并没有进行更改。有人看到我哪里错了吗?

<?php 
include("db.php");
$sql = "SELECT * FROM `artist`";
$result = mysqli_query($conn, $sql);
if (isset($_POST['update'])){
    $artID = $_POST['artID'];
    $artName = $_POST['artName'];
    $key = $_POST['hidden'];
    $UpdateQuery = "UPDATE `artist` SET `artID` = '$artID', `artName` = '$artName' WHERE `artist`.`artID` = '$key'"; 
    mysqli_query($conn,$UpdateQuery);
    header("Location: {$_SERVER['HTTP_REFERER']}");
    exit;
};
echo "<table border='1'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "</tr>";
if ($result->num_rows > 0) {
    echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
    // output data of each row
    while($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
        echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
        echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
        echo "<td>" . "<input type='submit' name ='update'" . " </td>";
        echo "</tr>";
    }
    echo "</form>";
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close(); 
?> 

db.php文件只是包含到mySQL数据库的连接信息,我100%确信它没有任何问题,因为它正确地检索了表,只是没有更新。

您将表单标记放入tr中,这是不允许的,td只允许所以你必须去掉tr。

你必须使用jquery,或者你可以用其他网格结构替换表,这样它看起来就一样了,表单也可以放在那里

还有一个建议不要将php和html混合在一起,将它们分开以获得干净的代码

如果你做了所有这些,你的代码将是这样的

您的form是用多个同名元素构建的。当您提交表单时,它将使用最后一个元素作为值,因此无论您想要更新的记录是什么,最后一个记录都将被更新(或者由于字符串封装而引发错误)。您也应该使用参数化查询。

所以不是:

if ($result->num_rows > 0) {
    echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
    // output data of each row
    while($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
        echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
        echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
        echo "<td>" . "<input type='submit' name ='update'" . " </td>";
        echo "</tr>";
    }
    echo "</form>";
    echo "</table>";

用途:

if ($result->num_rows > 0) {
// output data of each row
    while($row = mysqli_fetch_array($result)) {?>
        <form class='artisttable' action ='getartiststable.php' method ='post'>
            <tr>
                <td><input type='text' name ='artID' value ='<?php echo $row['artID'];?>' /></td>
                <td><input type='text' name ='artName' value ='<?php echo $row["artName"];?>' /></td>
                <td><input type = 'hidden' name ='hidden' value='<?php echo $row['artID'];?>' /></td>
                <td><input type='submit' name ='update'" . " </td>
            </tr>
        </form>
    <?php } ?>
    </table>

因此,您可以为每个数据集获得一个form。以下是关于使用mysqli编写的语句的链接:http://php.net/manual/en/mysqli.quickstart.prepared-statements.php.你也应该更新你的标记。用于格式化的Table不是最好的方法。您的input也没有关闭缺少>

这也将artisttableid改变为class,因为将存在倍数。相应地更新CSS/JS。