簡體   English   中英

PHP SQL查詢服務器錯誤

[英]PHP SQL query server error

我有這樣一張桌子:

  TABLE click_count
   (
   count int(3)
   );

目前是任意數字。 我有一個帶有html按鈕的php腳本,應該將數字增加一個。 SQL查詢可在我的管理員php中使用,但在頁面上運行時會出錯。

<?php

require("config.inc.php");

if(!empty($_POST)){

$query = "UPDATE click_count 
  SET count = count + :submit_1
  ";


    $query_params_ = array(
'submit_1' => $_POST['count']
);

try {
    // These two statements run the query against your database table. 
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
    }

 catch (PDOException $ex) {
    $response["message"] = "Database Error. Please Try Again!";
    die(json_encode($response));
    }

    $response["message"] = "Vote Cast!";
    echo json_encode($response);



} else {
?>    
<form action="vote.php" method="post"> 
Count:<br /> 
<input type="number" name="submit_1" value="1" /> 
<br /><br /> 

<input type="submit" value="Cast Vote" /> 
</form>

<?php
}

?>

count是mysql的保留關鍵字,請參見: http : //dev.mysql.com/doc/refman/5.0/en/reserved-words.html

嘗試將其包含在重音符號中,如下所示:

UPDATE click_count 
  SET `count` = `count` + :submit_1
  ";

您必須更改數組鍵以匹配prepareStatement中的鍵。 像這樣:

$query_params_ = array(':submit_1' => $_POST['count']);

確實,您在execute()方法中引用了$ query_params,但您正在定義$ query_params_(最后帶有下划線)。

暫無
暫無

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

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