duan6832168 2016-10-09 05:38
浏览 102
已采纳

修复mysql错误mysqli_stmt_bind_param()[重复]

I have created a form so students can input their information. All the data that students input will be stored in a MYSQL database. However, when the submit button is clicked after data is entered into form fields, a few error messages are displayed on the screen. These are the following error messages:

Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in C:\xampp\htdocs\MYSQL\studentadded.php on line 317

Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in C:\xampp\htdocs\MYSQL\studentadded.php on line 327

Warning: mysqli_stmt_affected_rows() expects parameter 1 to be mysqli_stmt, boolean given in C:\xampp\htdocs\MYSQL\studentadded.php on line 331 Error Occurred

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\MYSQL\studentadded.php on line 357

Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given in C:\xampp\htdocs\MYSQL\studentadded.php on line 361

This is the code:

<!DOCTYPE html>
    <html>

    <head>

    <title>Add Student</title>

    </head>
    <body>

    <?php



    if(isset($_POST['submit'])){



        $data_missing = array();



        if(empty($_POST['first_name'])){



            // Adds name to array

            $data_missing[] = 'First Name';



        } else {



            // Trim white space from the name and store the name

            $f_name = trim($_POST['first_name']);



        }



        if(empty($_POST['last_name'])){



            // Adds name to array

            $data_missing[] = 'Last Name';



        } else{



            // Trim white space from the name and store the name

            $l_name = trim($_POST['last_name']);



        }



        if(empty($_POST['email'])){



            // Adds name to array

            $data_missing[] = 'Email';


        } else {


            // Trim white space from the name and store the name

            $email = trim($_POST['email']);



        }



        if(empty($_POST['street'])){



            // Adds name to array

            $data_missing[] = 'Street';



        } else {



            // Trim white space from the name and store the name

            $street = trim($_POST['street']);



        }



        if(empty($_POST['city'])){



            // Adds name to array

            $data_missing[] = 'City';



        } else {



            // Trim white space from the name and store the name

            $city = trim($_POST['city']);



        }



        if(empty($_POST['state'])){



            // Adds name to array

            $data_missing[] = 'State';



        } else {



            // Trim white space from the name and store the name

            $state = trim($_POST['state']);



        }



        if(empty($_POST['zip'])){



            // Adds name to array

            $data_missing[] = 'Zip Code';



        } else {



            // Trim white space from the name and store the name

            $zip = trim($_POST['zip']);



        }



        if(empty($_POST['phone'])){



            // Adds name to array

            $data_missing[] = 'Phone Number';



        } else {



            // Trim white space from the name and store the name

            $phone = trim($_POST['phone']);



        }



        if(empty($_POST['birth_date'])){



            // Adds name to array

            $data_missing[] = 'Birth Date';



        } else {



            // Trim white space from the name and store the name

            $b_date = trim($_POST['birth_date']);



        }



        if(empty($_POST['sex'])){



            // Adds name to array

            $data_missing[] = 'Sex';



        } else {



            // Trim white space from the name and store the name

            $sex = trim($_POST['sex']);



        }



        if(empty($_POST['lunch'])){



            // Adds name to array

            $data_missing[] = 'Lunch Cost';



        } else {



            // Trim white space from the name and store the name

            $lunch = trim($_POST['lunch']);



        }



        if(empty($data_missing)){



            require_once('mysqli_connect.php');



            $query = "INSERT INTO students (first_name, last_name, email,

            street, city, state, zip, phone, birth_date, sex, date_entered,

            lunch_cost, student_id) VALUES (?, ?, ?,

            ?, ?, ?, ?, ?, ?, ?, NOW(), ?, NULL)";



            $stmt = mysqli_prepare($dbc, $query);



            /*i Integers

            d Doubles

            b Blobs

            s Everything Else */


            //below is line 317 where the error is happening
            mysqli_stmt_bind_param($stmt, "ssssssisssd", $f_name,

                                   $l_name, $email, $street, $city,

                                   $state, $zip, $phone, $b_date,

                                   $sex, $lunch);


            //below is line 327 where the error is happening
            mysqli_stmt_execute($stmt);


             //below is line 331 where the error is happening
            $affected_rows = mysqli_stmt_affected_rows($stmt);



            if($affected_rows == 1){



                echo 'Student Entered';



                mysqli_stmt_close($stmt);



                mysqli_close($dbc);



            } else {



                echo 'Error Occurred<br />';
                //below is line 357 where the error is happening
                echo mysqli_error();


                //below is line 361 where the error is happening
                mysqli_stmt_close($stmt);



                mysqli_close($dbc);



            }



        } else {



            echo 'You need to enter the following data<br />';



            foreach($data_missing as $missing){



                echo "$missing<br />";



            }


        }



    }



    ?>



    <form action="http://localhost/studentadded.php" method="post">



        <b>Add a New Student</b>



        <p>First Name:

    <input type="text" name="first_name" size="30" value="" />

    </p>



    <p>Last Name:

    <input type="text" name="last_name" size="30" value="" />

    </p>



    <p>Email:

    <input type="text" name="email" size="30" value="" />

    </p>



    <p>Street:

    <input type="text" name="street" size="30" value="" />

    </p>



    <p>City:

    <input type="text" name="city" size="30" value="" />

    </p>



    <p>State (2 Characters):

    <input type="text" name="state" size="30" maxlength="2" value="" />

    </p>



    <p>Zip Code:

    <input type="text" name="zip" size="30" maxlength="5" value="" />

    </p>



    <p>Phone Number:

    <input type="text" name="phone" size="30" value="" />

    </p>



    <p>Birth Date (YYYY-MM-DD):

    <input type="text" name="birth_date" size="30" value="" />

    </p>



    <p>Sex (M or F):

    <input type="text" name="sex" size="30" maxlength="1" value="" />

    </p>



    <p>Lunch Cost:

    <input type="text" name="lunch" size="30" value="" />

    </p>



    <p>

        <input type="submit" name="submit" value="Send" />

    </p>



    </form>

    </body>

    </html>
</div>
  • 写回答

1条回答 默认 最新

  • dtz46697 2016-10-09 06:26
    关注

    I think that you might have problem in your connection file mysqli_connect.php

    Here is my code for your reference

    define("DB_HOST", "localhost");
    define("DB_USERNAME", "username");
    define("DB_PASSWORD", "password");
    define("DB_NAME", "database name");
    $dbc = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
    

    With this connection parameters I've executed your code and its working.

    Also please change the line echo mysqli_error(); to echo mysqli_error($dbc);

    The mysqli_error accepts one parameter which is the connection link.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 qt中connect两个signal
  • ¥20 pix2pixHD运行测试命令时出现数据类型错误无法反向传播的问题
  • ¥15 python处理Excel符合条件的行自动填写数据分类
  • ¥15 汇编hook举例并讲解(通俗易懂,学习用)
  • ¥20 用c++语言模拟键盘电子琴设计
  • ¥15 STM32cubemx生成keil工程,有问题与正常的情况不同,求解!
  • ¥15 如何自动点击银行app的安全键盘,实现密码自动输入
  • ¥15 关于四边形重叠的问题
  • ¥15 用verilog语言设计一个简易的八音符电子琴,可通过按键输入来控制音响。演奏时可以选择是手演奏(由键盘输入)或自动演奏已存入的乐曲。能够自动演奏多首乐曲,且每首乐曲可重复演奏
  • ¥15 sap gui脚本每次到导出Excel的时候就停住不动。不会另存为。