尝试使用HTML表单和PHP更新MySQL

3

...which should normally not be too hard, but for some reason I just can't get it to work. I'm probably missing something or made a typo, I dunno, but whatever it is I can't find it.

include('connect-db.php');

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$query = "SELECT * FROM articles ORDER BY orderid";
$result = mysql_query($query) or die(mysql_error());

$count = mysql_num_rows($result);
?>
<div class="content-holder">
    <form action="" method="post">
        <table border='1' cellpadding='10' id='ViewTable'>
            <tr>
            <th>Volgorde</th>
            <th>Titel</th>
            </tr>
            <?php
            while($row = mysql_fetch_array($result))
            {
                $orderid = $row['orderid'];
                $title = $row['title'];
            ?>
            <tr>
                <td><input type="textbox" class="TextAreaTitle" name="order" value="<?=$orderid?>"></td>
                <td><?=$title?></td>
            </tr>
            <?php
            }
            ?>
        </table>
        <input type="submit" name="submit" class="submitlink" Value="Verzenden">
    </form>
</div>

<?php
if(isset($_POST['submit']))
{
    mysql_query("UPDATE articles SET orderid=$orderid  WHERE title=$title");
    echo "Updated!";
}
?>

I just pasted the entire code here to be sure. On the webpage, what you basically see if a table, with the colomns orderid and title, with all of their respective rows. the orderid's can be changed as they are a textbox. What I'm trying to achieve is that is one (or multiple) orderid's get changed, and you click on the button, then I want the orderid's to change accordingly in my database.

Thanks in advance for your help. Any questions please ask.

EDIT: I completely forgot to mention this, but what happens now when I click on the button, it returns the echo saying "Updated!" but the database doesn't actually update.


为什么你的脚本中没有进行任何成功/错误控制?评估mysql_query的返回值,并使用mysql_error。它很可能会在where子句中的标题之前警告您出现错误,因为-假设它是文本值-您忘记了在其周围添加单引号。另外,如果可能的话,不应再使用旧的mysql扩展,而应使用mysqli/pdo。此外,您必须了解SQL注入以及如何防范它。 - CBroe
最近我查了一些关于SQL注入的资料,但我还没有在这个特定的脚本中实现它。现在不用担心这个问题,我以后肯定会解决的。现在主要问题是这里。我现在会进行一次拼尽全力的成功/错误控制,哈哈。:p - Tienus McVinger
顺便提一下,“submit”不是一个好的元素名称。这会在以后引起问题。 - Joe Frambach
你犯了最大的错误- mysql_connect("$host" , "$username" , "$password")。不要使用mysql_函数,应该使用PDO。 - ShuklaSannidhya
4个回答

2
mysql_query("UPDATE articles SET orderid=$orderid  WHERE title=$title");

这需要引用表单中的<input>字段,例如:
$orderid = $_POST['order'];
$title = $_POST['title'];
mysql_query("UPDATE articles SET orderid=$orderid  WHERE title=$title");

你从表单中没有提交 title 值。你需要在你的表单中添加一个 <input name="title" />,否则它将始终使用 while 循环的最后一个值。

如果你正在从 $_POST 中读取数据,那么你需要添加输入验证,否则你会容易受到攻击。

编辑:我稍微修改了一下。

include('connect-db.php');

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$query = "SELECT * FROM articles ORDER BY orderid";
$result = mysql_query($query) or die(mysql_error());

$count = mysql_num_rows($result);
?>
<div class="content-holder">
    <form action="" method="post">
        <table border='1' cellpadding='10' id='ViewTable'>
            <tr>
            <th>Volgorde</th>
            <th>Titel</th>
            </tr>
            <?php
            while($row = mysql_fetch_array($result))
            {
                $orderid = $row['orderid'];
                $title = $row['title'];
            ?>
            <tr>
                <td><input type="textbox" class="TextAreaTitle" name="order[]" value="<?=$orderid?>"><input type="hidden" name="title[]" value="<?=$title?>"></td>
                <td><?=$title?></td>
            </tr>
            <?php
            }
            ?>
        </table>
        <input type="submit" name="submit" class="submitlink" Value="Verzenden">
    </form>
</div>

<?php
if(isset($_POST['submit']))
{
    for ($i=count($_POST['title']); $i--;) {
        $orderid = $_POST['order'][$i];
        $title = $_POST['title'][$i];
        mysql_query("UPDATE articles SET orderid=$orderid  WHERE title=$title");
    }
    echo "Updated!";
}
?>

我已经更新了我的答案。如果你想让标题只读,那么使用 <input type="hidden" name="title" value="<?=$title?>" /><?=$title?> - Joe Frambach
它仍然显示回显说“已更新!”,但实际上数据库并没有更新。问题可能出在我的包含文件中吗? - Tienus McVinger
不是包含文件的问题。更新时,用echo替换mysql_query,看看输出什么。 - Joe Frambach
WHERE title=$title 应该加上引号。 WHERE title='$title' - Joe Frambach

0

如果你使用 name="order[]",它会返回一个数组,你必须使用 foreach 将数组转换为字符串。


-1
有时候会出现这种类型的错误。在插入数据或从数据库检索数据时,它具有不同的语法,因此应该尝试所有解决方案。
mysql_query("UPDATE articles SET orderid='". $orderid ."' WHERE title='". $title ."'"); echo "已更新!";

-1

希望它是可见的

mysql_query("UPDATE articles SET orderid=' " . $orderid . " ' WHERE title=' " . $title . " ' "); echo "已更新!";


如果我使用这个,结果是一样的。 - Tienus McVinger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接