簡體   English   中英

mysql更新不更新?

[英]mysql Update doesn't update?

我試圖弄清楚為什么這個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>

可能會從數據庫服務器中獲取錯誤並忽略它們。 使用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文檔徹底解釋了該概念 ,並提供了替代示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM