我想弄清楚为什么mysql更新查询没有真正更新mysql数据库!
我找不到原因!
该页面从add.php文件中获取一个ID,与该ID相关的值在表单中得到正确的回显,但由于某些原因,它根本不更新mysql!
如果我遗漏了什么,谁能告诉我吗?
这是我的代码:
<?php
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
// Parse the form data and add inventory item to the system
if (isset($_POST['title'])) {
$pid = mysqli_real_escape_string($db_conx, $_POST['thisID']);
$title = mysqli_real_escape_string($db_conx, $_POST['title']);
$details = mysqli_real_escape_string($db_conx, $_POST['details']);
// See if that product name is an identical match to another product in the system
$sql = "UPDATE pages SET title='$title', details='$details', WHERE id='$pid'";
$query = mysqli_query($db_conx, $sql);
header("location: add.php");
exit();
}
?>
<?php
// Gather this product's full information for inserting automatically into the edit form below on page
if (isset($_GET['pid'])) {
$targetID = $_GET['pid'];
$sql = "SELECT * FROM pages WHERE id='$targetID' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$title = $row["title"];
$details = $row["details"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
}
} else {
echo "Sorry, that don't exist.";
exit();
}
}
?>下面是页面中的HTML表单:
<form action="pages_edit.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
<table width="90%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="20%" align="right">Page Tile</td>
<td width="80%"><label>
<input name="title" type="text" id="title" size="64" value="<?php echo $title; ?>" />
</label></td>
</tr>
<tr>
<td align="right">Page Body</td>
<td><label>
<textarea name="details" id="details" cols="64" rows="5"><?php echo $details; ?></textarea>
</label></td>
</tr>
<tr>
<td> </td>
<td><label>
<input name="thisID" type="text" value="<?php echo $targetID; ?>" />
<input type="submit" name="button" id="button" value="Make Changes" />
</label></td>
</tr>
</table>
</form>发布于 2014-03-11 00:04:36
您可能会从数据库服务器获得错误并忽略它们。mysqli_error()检查错误。
首先,查询中有一个额外的逗号。在WHERE子句之前不需要逗号,所以更改为:
UPDATE pages SET title='$title', details='$details' WHERE id='$pid'另外,id列真的是一个字符串吗?它更有可能是一个整数。(当然,除非你把它做成了字符串。)检查要知道的表模式。)如果是这样的话,那么你就不想用单引号包围这个值了。因此,改为:
UPDATE pages SET title='$title', details='$details' WHERE id=$pid很可能还有其他的错误。检查数据库响应(如前所述)是否有错误,并检查PHP日志中是否有错误。您需要调试您的代码,不要只看它并猜测问题可能是什么。
此外,值得注意的是,您的代码目前处于高度易受攻击的到SQL注入攻击。幸运的是,PHP彻底地使用了解释这个概念,并且有替代品实例。
https://stackoverflow.com/questions/22313836
复制相似问题