mysqli_stmt :: bind_param()的参数计数错误

时间:2015-04-08 18:57:10

标签: php

你好我有这个代码试图在数据库中插入数据,但我面临mysqli的错误rong参数计数

<?php

session_start();

$regValue = $_GET['regName'];

echo "Your registration is: ".$regValue.".";


$servername = "localhost";
$username = "root";
$password = "b4sonic";
$dbname = "b4sonic2";



$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// prepare and bind
$stmt = $conn->prepare("INSERT INTO b4sonic (first_name) VALUES (?)");
$stmt->bind_param( $firstname);

// set parameters and execute

$firstname = "John";
$stmt->execute();


echo "New records created successfully";

$stmt->close();
$conn->close();
?>

请我快速推迟,因为我在项目上工作,我应该推荐它,但这个问题

2 个答案:

答案 0 :(得分:3)

弗雷德解决了一个问题。在bind_param时,$firstname未定义。

您也没有正确调用bind_param

第一个参数需要标识变量类型。例如's'表示字符串。请参阅我添加的链接。

// define the variable first
$firstname = "John";

// prepare and bind
$stmt = $conn->prepare("INSERT INTO b4sonic (first_name) VALUES (?)");
$stmt->bind_param("s", $firstname);

// set parameters and execute
$stmt->execute();

答案 1 :(得分:0)

$firstname放在您的行上方:

// prepare and bind
$firstname = "John";

$stmt = $conn->prepare("INSERT INTO b4sonic (first_name) VALUES ('$firstname')");