MYSQL帖子查询 - 不工作需要建议


MYSQL post query - not working need advise

有人可以看看这个,也许看看为什么它不更新我的数据库?

它是一种从我的数据库中提取数据的表单,当我单击更新按钮时,它应该更新数据库,但它没有。

有没有办法显示它是否返回错误?

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Beerlist Admin</title>
</head>
<body>
<?php include '../beerlist/config.php'; ?>
<table border="0" width="95%" align="center" cellspacing="0" cellpadding="5">
    <tr>
<?php
#///////////////////////////////////
#//////////// ALL BEERS/////////////
#///////////////////////////////////
$sql = "SELECT * FROM bottles ORDER BY name";
$mydata = mysql_query($sql,$con);
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE bottles SET new='$_POST[new]', name='$_POST[name], style='$_POST[style], location='$_POST[location], size='$_POST[size], abv='$_POST[abv], number='$_POST[number], price='$_POST[price]' WHERE name='$_POST[hidden]'";
mysql_query ($UpdateQuery, $con);
};

while($record = mysql_fetch_array($mydata)){
echo "<form action=beerlist_admin.php method=post>";
    echo "<td><input size=7 type=text name=new value='"" . $record['new'] . "'" > ";
    echo "<input type=text size=50 name=name value='"" . $record['name'] . "'" >";
    echo "<input type=text size=20 name=style  value='"" . $record['style'] . "'" >";
    echo "<input type=text size=20 name=location  value='"" . $record['location'] . "'" >";
    echo "<input type=text size=7 name=size   value='"" . $record['size'] . "'" >";
    echo "<input type=text size=5 name=abv   value='"" . $record['abv'] . "'" >";
    echo "<input type=text size=5 name=number   value='"" . $record['number'] . "'" >";
    echo "<input type=text size=7 name=price   value='"" .$record['price'] . " '" >";
    echo "<input type=hidden name=hidden value=" . $record['number'] . "'"> <input type=submit name=update value=update></td></tr>";
echo "</form>";
}
echo "</table>"
?>

</body>
</html>

首先,mysql_*已被弃用,您应该考虑迁移到MySQLi或PDO,并为mysql_*被删除做好准备,这样您的网站就不会停止工作。


你的代码对SQL注入是开放的,你应该使用mysql_real_escape_string($variable)来防止这种情况。

通过sprintf的组合,您可以进一步定义所需的变量类型,例如:

%s - 用于字符串数据。
%d - 用于数字,并表示为(有符号(十进制数。

在下面的示例中,我只使用了 %s,因为我不确定您尝试插入的数据和数据库表字段类型。

您的UPDATE查询编写得很差,并且可能会在不同情况下导致问题,具体取决于您提供的数据,因此您应该至少在开发过程中始终使用该mysql_error,以便您可以调试此类错误,一旦您移动到实时站点,这些错误就会以静默方式记录到文件中。

你不需要总是使用echo来打印每一小段HTML,看看我是如何改变你的WHILE代码的。

此外,您在表格中使用表单的方式可能不正确。

由于您尚未发布config.php的内容,因此我无法判断其中是否有任何问题。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Beerlist Admin</title>
</head>
<body>
<?php
$con = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$con)
{
    die('Not connected : ' . mysql_error());
}
$db_selected = mysql_select_db('beerlist', $con);
if (!$db_selected)
{
    die ('Can''t use foo : ' . mysql_error());
}
?>
<table border="0" width="95%" align="center" cellspacing="0" cellpadding="5">
<?php
if (isset($_POST['update']))
{
    $UpdateQuery = sprintf("UPDATE bottles 
                               SET new='%s', 
                                   name='%s', 
                                   style='%s',
                                   location='%s',
                                   size='%s',
                                   abv='%s',
                                   number='%s',
                                   price='%s'
                             WHERE name='%s'",
                             mysql_real_escape_string($_POST['new']),
                             mysql_real_escape_string($_POST['name']),
                             mysql_real_escape_string($_POST['style']),
                             mysql_real_escape_string($_POST['location']),
                             mysql_real_escape_string($_POST['size']),
                             mysql_real_escape_string($_POST['abv']),
                             mysql_real_escape_string($_POST['number']),
                             mysql_real_escape_string($_POST['price']),
                             mysql_real_escape_string($_POST['hidden']));
    if (!mysql_query($UpdateQuery))
    {
        die('Invalid query: ' . mysql_error());
    }
}
$sql = "SELECT * 
          FROM bottles 
      ORDER BY name";
$mydata = mysql_query($sql);
if (!$mydata)
{
    die('Invalid query: ' . mysql_error());
}
while($record = mysql_fetch_array($mydata))
{
?>
<form action="beerlist_admin.php" method="post">
<tr><td>
<input size="7" type="text" name="new" value="<?php echo $record['new']; ?>">
<input type="text" size="50" name="name" value="<?php echo $record['name']; ?>">
<input type="text" size="20" name="style" value="<?php echo $record['style']; ?>">
<input type="text" size="20" name="location" value="<?php echo $record['location']; ?>">
<input type="text" size="7" name="size" value="<?php echo $record['size']; ?>">
<input type="text" size="5" name="abv" value="<?php echo $record['abv']; ?>">
<input type="text" size="5" name="number" value="<?php echo $record['number']; ?>">
<input type="text" size="7" name="price" value="<?php echo $record['price']; ?>">
<input type="hidden" name="hidden" value="<?php echo $record['number']; ?>">
<input type="submit" name="update" value="update">
</td></tr>
</form>
<?php
}
?>
</table>
</body>
</html>