Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

php - mysqli_stmt_bind_param(): Number of variables doesn't match number of parameters in prepared statement in bind param

I am getting this error.i wanna register to page. mysqli_stmt_bind_param(): Number of variables doesn't match number of parameters in prepared statement in bind param. `

<?php       if(isset($_POST['submit'])) { // Was the form submitted?
        $link = mysqli_connect("localhost", "root", "", "databaseInitialization") or die ("Connection Error " . mysqli_error($link));
            $sql = "INSERT INTO user(first_name, last_name, email, password, bio, location, industry,salt) VALUES(?,?,?,?,?,?,?,?)"; 
            if ($stmt = mysqli_prepare($link, $sql)) {
                $fname = $_POST['fname'];
                $lname = $_POST['lname'];
                $email = $_POST['email'];
                $_SESSION['email'] = $email;
                $bio = $_POST['bio'];

                $location = $_POST['location'];
                $industry = $_POST['industry'];
                        $salt = mt_rand();
                $password = password_hash($salt.$_POST['pass'], PASSWORD_BCRYPT)  or die("bind param");
                //echo "before bind";
                mysqli_stmt_bind_param($stmt, 'sssssss', $fname, $lname, $password, $email, $bio, $location, $industry) or die("bind param");
                //echo "after bind";


    if(mysqli_stmt_execute($stmt)) {
                          echo "<h4><b><center>Success</center></b></h4>";
            //this redirects to user.php - but still need to log in 
            header('location: user.php');
                } else {
                    echo "<h4><b><center>Failed</center></b></h4>";
                    printf("<b><center>Error: %s</center></b>.
", mysqli_stmt_error($stmt));
                }
            $result = mysqli_stmt_get_result($stmt);
            }
        } 
        else { ?>`
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your insert, are 8 columns and only 7 binds

                     1           2        3      4        5      6
INSERT INTO user(first_name, last_name, email, password, bio, location, 
   7       8
industry,salt)
VALUES(?,?,?,?,?,?,?,?)
       1 2 3 4 5 6 7 8

The binds, is missing one, propably the salt

                               1234567 
mysqli_stmt_bind_param($stmt, 'sssssss',
   1      2        3         4       5      6          7
$fname, $lname, $password, $email, $bio, $location, $industry) or die("bind param");

the numbers of s, columns and variables must be the same, in this case 8. To fix just add one s and $salt at bind_param(), like this:

mysqli_stmt_bind_param($stmt, 'ssssssss', $fname, $lname, $password, $email, $bio, $location, $industry, $salt) or die("bind param");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...