简体   繁体   English

UPDATE准备好的语句PHP错误

[英]UPDATE prepared statement PHP Error

I am creating a dynamic page which changes depending on which ever post the user clicks onto. 我正在创建一个动态页面,该页面会根据用户点击过的帖子而变化。 I am also wanting the views (hit-counter) the page gets to go up by one each time the page is loaded. 我还希望每次加载页面时页面的视图(点击计数器)都增加一。 I am currently getting the following error. 我目前收到以下错误。

Fatal error: Call to a member function bind_param() on a non-object in C:\\Users\\PC\\Documents\\XAMPP\\htdocs\\post.php on line 13 致命错误:在第13行的C:\\ Users \\ PC \\ Documents \\ XAMPP \\ htdocs \\ post.php中的非对象上调用成员函数bind_param()

<?php
session_start();
include 'php/config.php';
$post = $_GET['post'];
$stmt = $mysqli->prepare("SELECT * FROM forum WHERE ForumId = '$post'");
$stmt->execute();
$stmt->bind_result($ForumId,$ForumTitle,$ForumPostText,$PostDate,$Views);
$stmt->fetch();
$stmt->close();

$Views = 1;
$stmt = $mysqli->prepare("UPDATE 'forum' SET 'Views' = 'Views'+ 1 WHERE 'ForumId' = '?' ");
$stmt->bind_param('i',$post);
$stmt->execute();
$stmt->close();


?>  
<!DOCTYPE html>
// The rest of the webpage yada yada yada

Remove ( ' ) single quotes in update query and use backtick (`) instead 在更新查询中删除( ' )单引号,而使用反引号(`)

So 所以

"UPDATE `forum` SET `Views` = Views+ 1 WHERE `ForumId` = ?"

Although Krish R's response is the solution, one of the things you will want to do in cases like this, is look at $mysqli->error to actually get an error message. 尽管Krish R的响应是解决方案,但是在这种情况下,您要做的一件事是查看$mysqli->error以实际获得错误消息。 This will tell you that you have a syntax error near 'forum' SET 'Vi... . 这将告诉您在'forum' SET 'Vi...附近有语法错误。 That in itself should indicate that that specific character (the first ' in the string) is the most likely cause of the error. 它本身应表明该特定字符(字符串中的第一个')是最有可能导致错误的原因。

It seems you have a problem in the query. 看来您在查询中有问题。

Take note, that PDO statement dont need single quotes 请注意,PDO语句不需要单引号

Try with this: 试试这个:

$stmt = $mysqli->prepare("UPDATE forum SET Views = Views+ 1 WHERE ForumId = ?");
$stmt->bind_param('i', $post);
<?php
session_start();
include 'php/config.php';
$post = $_GET['post'];
$stmt = $mysqli->prepare("SELECT * FROM forum WHERE ForumId = $post");
$stmt->execute();
$stmt->bind_result($ForumId,$ForumTitle,$ForumPostText,$PostDate,$Views);
$stmt->fetch();
$stmt->close();

$Views = 1;
$stmt = $mysqli->prepare("UPDATE forum SET Views = Views + 1 WHERE ForumId = ?");
$stmt->bind_param('i', $post);
$stmt->execute();
$stmt->close();


?>  
<!DOCTYPE html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM