-1

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

<?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
4

4 回答 4

2

删除'更新查询中的 ( ) 单引号并改用反引号 (`)

所以

"UPDATE `forum` SET `Views` = Views+ 1 WHERE `ForumId` = ?"
于 2014-02-12T14:56:11.127 回答
1

尽管 Krish R 的响应是解决方案,但在这种情况下您想要做的事情之一是查看$mysqli->error是否实际收到错误消息。这将告诉您在 .附近有语法错误'forum' SET 'Vi...。这本身应该表明该特定字符(字符串中的第一个 ' )是最可能导致错误的原因。

于 2014-02-12T15:02:47.777 回答
0

您的查询似乎有问题。

请注意,PDO 语句不需要单引号

试试这个:

$stmt = $mysqli->prepare("UPDATE forum SET Views = Views+ 1 WHERE ForumId = ?");
$stmt->bind_param('i', $post);
于 2014-02-12T15:07:43.540 回答
0
<?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>
于 2014-02-12T15:08:24.057 回答