尝试使用PHP插入值时出现错误

时间:2019-03-05 15:34:30

标签: php

DbOperations.php

<?php
class DbOperations {
    private $con;

    function __construct() {
        require_once dirname(__FILE__).'/DbConnect.php';
        $db = new DbConnect();
        $this->con = $db->connect();
    }

    function createUser($firstname, $lastname, $score) {
        $stmt = $this->con->prepare("INSERT INTO `users` (`firstname`, `lastname`, `score`) VALUES (?, ?, ?);");
        $stmt->bind_param($firstname, $lastname, $score);
        if($stmt->execute()) {
            return true;
        } else {
            return false;
        }
    }
}

highScore.php

<?php

require_once '../includes/DbOperations.php';

$response = array();

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(
        isset($_POST['firstname']) and 
        isset($_POST['lastname']) and 
        isset($_POST['score']))
    {
        $db = new DbOperations();
        if($db->createUser(
            $_POST['firstname'],
            $_POST['lastname'],
            $_POST['score']
        )){
            $response['error'] = false;
            $response['message'] = "User successfully added";
        } else {
            $response['error'] = true;
            $response['message'] = "Error occured, please try again";
        }
    } else {
        $response['error'] = true;
        $response['message'] = "Required fields are missing";
    }
} else {
    $response['error'] = true;
    $response['message'] = "Invalid Request";
}

echo json_encode($response);

我不断收到此错误(请参见下面的错误) 如何解决该错误?

  

警告:mysqli_stmt :: bind_param():类型定义字符串中的元素数与C:\ xampp \ htdocs \ users \ includes \ DbOperations.php中第13行上的绑定变量数不符{“错误”: true,“ message”:“发生错误,请重试”}

0 个答案:

没有答案