mysqli_stmt_bind_param和mysqli_stmt_execute期望参数为mysqli_stmt,boolean

时间:2016-01-18 09:39:15

标签: php mysql mysqli

用户点击按钮:

<input type="image" src="img/like.png" alt="Like" value="Like">
<input type="hidden" name="IP" value="<?php $_SERVER ["REMOTE_ADDR"] ?>">

保存到数据库中(错误在评论中):

<?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    if (!empty($_POST)) {
        $connection = mysqli_connect ($servername, $username, $password);
        $statement = mysqli_prepare ($connection, "INSERT INTO Like (User, PageId) VALUES (?, ?)");
        mysqli_stmt_bind_param ($statement, "si", $_POST[IP], $_GET[id]); 
        //mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean
        mysqli_stmt_execute ($statement);
       //mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean
        exit;
    }
?>

显示金额:

<?php
    $connection = mysqli_connect ($servername, $username, $password);
    $statement = mysqli_prepare ($connection, "SELECT * FROM Like WHERE PageId=$_GET[id];");
?>

一切看起来都对我不错,但我是新手,很难学习PHP / SQL。

2 个答案:

答案 0 :(得分:3)

通过查看您的连接找到错误,您没有连接到数据库,您拥有的是:

$connection = mysqli_connect ($servername, $username, $password);

您需要的是:

$database = 'my_db';
$connection = mysqli_connect ($servername, $username, $password, $database);

答案 1 :(得分:3)

您的代码中的问题是,您使用输入字段:

<input type="hidden" name="IP" value="<?php $_SERVER ["REMOTE_ADDR"] ?>">

这应该是这样的:

<input type="hidden" name="IP" value="<?php echo $_SERVER["REMOTE_ADDR"]; ?>">

<input type="hidden" name="IP" value="<?=$_SERVER["REMOTE_ADDR"]?>">

<强>问题:

您在mysqli_stmt_bind_param

中使用空输入字段