问题
My php form inserts a few columns and an encrypted password into my table. However when I run it it says the variable number doesn't match the number of parameters. This is my code:
<?php
if (isset($_POST['insert'])) {
require_once 'login.php';
$OK = false;
$conn = new mysqli ($host, $user, $password, $database) or die("Connection Failed");
$stmt = $conn->stmt_init();
$sql = 'INSERT INTO users (user_email, user_name, user_pref, user_password)
VALUES(?, ?, ?, des_encrypt(substring(md5(rand()),1,8)))';
if ($stmt->prepare($sql)) {
// bind parameters and execute statement
$stmt->bind_param('ssss', $_POST['user_email'], $_POST['user_name'], $_POST['user_pref'], $_POST['user_password']);
// execute and get number of affected rows
$stmt->execute();
if ($stmt->affected_rows > 0) {
$OK = true;
}
}
if ($OK) {
header('Location: confirm.php');
exit;
} else {
$error = $stmt->error;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Add User</title>
</head>
<body>
<h1>Add User</h1>
<?php if (isset($error)) {
echo "<p>Error: $error</p>";
} ?>
<form id="form1" method="post" action="">
<p>
<label for="user_email">User email:</label>
<input name="user_email" type="text" class="widebox" id="user_email">
</p>
<p>
<label for="user_name">User name:</label>
<input name="user_name" type="text" class="widebox" id="user_name">
</p>
<p>
User role: <select name = "user_pref">
<option value = "BLU">Blue</option>
<option value = "YEL">Yellow<option>
<option value = "GRE">GREEN</option>
</select>
</p>
<p>
<input type="submit" name="insert" value="Register New User" id="insert">
</p>
</form>
</body>
</html>
When I test the form without the ENCRYPTED PASSWORD it works fine, so this line causes issue when i'm trying to insert the password:
$stmt->bind_param('ssss', $_POST['user_email'], $_POST['user_name'], $_POST['user_pref'], $_POST['user_password']);
Am I supposed to change string to something else for password ?
thanks
回答1:
$sql = 'INSERT INTO users (user_email, user_name, user_pref, user_password)
VALUES(?, ?, ?, des_encrypt(substring(md5(rand()),1,8)))';
Defines only 3 placeholders but you try to write to 4 ones.
$stmt->bind_param('ssss', $_POST['user_email'], $_POST['user_name'], $_POST['user_pref'], $_POST['user_password']);
For every ? you insert in the prepared SQL statement you have to pass a variable in bind_param.
回答2:
You're passing four variables here:
$stmt->bind_param('ssss', $_POST['user_email'], $_POST['user_name'], $_POST['user_pref'], $_POST['user_password']);
but only three of them are required
$sql = 'INSERT INTO users (user_email, user_name, user_pref, user_password)
VALUES(?, ?, ?, des_encrypt(substring(md5(rand()),1,8)))';
See "?" marks, it will be replaced with bild_params. You probably want to replace your SQL query to the next one:
$sql = 'INSERT INTO users (user_email, user_name, user_pref, user_password)
VALUES(?, ?, ?, des_encrypt(substring(md5(?),1,8)))';
回答3:
The number of parameter to be taken by your query is determined by the number of ?
in your query.
You have 3 ?
in your query :
$sql = 'INSERT INTO users (user_email, user_name, user_pref, user_password)
VALUES(?, ?, ?, des_encrypt(substring(md5(rand()),1,8)))';
and you are passing 5 parameter in bind_param
:
$stmt->bind_param($_POST['user_email'], $_POST['user_name'], $_POST['user_pref']);
There are 2 solution possible :
Take 5 parameters in the query :
$sql = 'INSERT INTO users (user_email, user_name, user_pref, user_password) VALUES(?, ?, ?, ?, des_encrypt(?))';
Pass only 3 param in the
bind_param
function :$stmt->bind_param('ssss', $_POST['user_email'], $_POST['user_name'], $_POST['user_pref'], $_POST['user_password']);
来源:https://stackoverflow.com/questions/14605084/mysqli-stmtbind-param-mysqli-stmt-bind-param-number-of-variables-doesnt