mysql更新不更新?

时间:2014-03-10 23:59:47

标签: php mysql

我想弄清楚为什么这个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>&nbsp;</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>

1 个答案:

答案 0 :(得分:4)

可能从数据库服务器获取错误并忽略它们。 Use 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 injection attacks。幸运的是,PHP文档explains the concept已经彻底,并且有examples of alternatives