简体   繁体   English

通过 PHP 通过 HTML 表单显示和更新 MYSQL 数据

[英]Display and update MYSQL data via HTML form via PHP


I am trying to figure out how to display all the rows of a database table in one page, all the values to be editable, and for there to be a single submit button at the end of it.我试图弄清楚如何在一页中显示数据库表的所有行,所有值都是可编辑的,并且在它的末尾有一个提交按钮。 I got half the equation figured out, but for some reason it is still not working.我已经弄清楚了一半的等式,但由于某种原因它仍然无法正常工作。
What I currently have is a table displaying all the contents of a MYSQL table and all fields are editable.我目前拥有的是一个表,显示了 MYSQL 表的所有内容,并且所有字段都是可编辑的。 There is a submit button for all each field (which is not what I want, but willing to settle if I have to), but upon editing something from the database fields, it brings me to a page that gives me a syntax error:每个字段都有一个提交按钮(这不是我想要的,但如果必须的话我愿意解决),但是在从数据库字段中编辑某些内容时,它会将我带到一个页面,该页面给我一个语法错误:
"Error updating record: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE idnum = '0000'' at line 1" “错误更新记录:您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,以了解在第 1 行的“WHERE idnum = '0000''附近使用的正确语法”

The following is from FORM.PHP以下来自FORM.PHP

<?php
include('config.php');
$result = mysqli_query($connect,"SELECT * FROM table123");
?>
<html>
<table>
<?php while ($res = mysqli_fetch_array($result)) { ?>
<tr>
        <form action="test.php" method="post">
            <td><input type="text" name="ret" value="<?php echo $res['ret']; ?>"></td>
            <td><input type="text" name="code" value="<?php echo $res['code']; ?>"></td>
            <td><input type="text" name="status" value="<?php echo $res['status']; ?>"></td>
            <td><input type="hidden" name="idnum" value="<?php echo $res['idnum']; ?>"></td>
            <td><input type="submit" name="update" value="Submit"></td>
        </form>
    </tr>
    <?php } ?>
</table>
</html>

The following is from TEST.PHP以下来自 TEST.PHP

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$connect = mysqli_connect($servername, $username, $password, $dbname);
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['update'])) {
$sql = "UPDATE ssoretailerlist SET ret = '$_POST[ret]', code = '$_POST[code]', status = '$_POST[status]', WHERE idnum = '$_POST[idnum]'";
} else {
echo "Nothing was posted";
}
if (mysqli_query($connect, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($connect);
}
mysqli_close($connect);

Syntax error is because you have an extra comma.语法错误是因为您有一个额外的逗号。 Remove the comma before WHERE and you should be fine.删除 WHERE 之前的逗号,你应该没问题。

$sql = "UPDATE ssoretailerlist
        SET ret = '$_POST[ret]', code = '$_POST[code]', status = '$_POST[status]'
        WHERE idnum = '$_POST[idnum]'";

There is a submit button for all each field .每个字段都有一个提交按钮 Instead of creating a new form and submit for every row inside the loop, one them each once manually outside the loop.不是为循环内的每一行创建一个新表单并提交,而是在循环外手动一次。

<?php
include('config.php');
$result = mysqli_query($connect, "SELECT * FROM table123");
?>
<html>
<table>
<form action="test.php" method="post">
<?php while ($res = mysqli_fetch_array($result)) { ?>
    <tr>
    <td><input type="text" name="ret" value="<?php echo $res['ret']; ?>"/></td>
    <td><input type="text" name="code" value="<?php echo $res['code']; ?>"/></td>
    <td><input type="text" name="status" value="<?php echo $res['status']; ?>"/></td>
    <td><input type="hidden" name="idnum" value="<?php echo $res['idnum']; ?>"/></td>
    </tr>
<?php } ?>
</table>
<input type="submit" name="update" value="Submit"/>
</form>
</html>

You may want to also handle the output you're inserting into the form.您可能还想处理插入到表单中的输出。 If the data has double quotes in it, it may break your HTML.如果数据中有双引号,它可能会破坏您的 HTML。 Check out htmlspecialchars() .查看htmlspecialchars() Based on your column titles I don't think it would, but always good to keep in mind.根据您的专栏标题,我认为不会,但始终牢记。

However, every single row has the exact same input names .但是,每一行都具有完全相同的输入名称 This is a problem.这是个问题。 How will it know which ret , code , status , or idnum to choose and associate together?它如何知道选择哪个retcodestatusidnum并将其关联在一起? First you want to turn the names into arrays .首先,您要将名称转换为数组 Then you want to loop through the idnum array and do multiple UPDATE queries accessing the same key location in the other arrays.然后,您希望遍历idnum数组并执行多个UPDATE查询以访问其他数组中的相同键位置。 Post a new question if you get stuck working on that.如果您遇到困难,请发布一个新问题。

And finally your config.php file is pretty necessary .最后你的 config.php 文件是非常必要的 You may want to read this thread about require_once() vs include() .您可能想阅读有关 require_once() 与 include() 的此线程 It's good to throw an error and handle it if the include fails instead of continuing to process the rest of the script.如果包含失败,最好抛出错误并处理它,而不是继续处理脚本的其余部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM