无法保存到使用mysqli

时间:2014-07-08 09:53:37

标签: php mysqli

我有一个插入代码,假设使用mysqli扩展名保存到数据库。 这是我的mysqli连接

  <?php 
   global $db;
     //dbconnector.php       
     //define the mysql connection variables.
       define ("MYSQLHOST", "localhost");
       define ("MYSQLUSER", "root");
       define ("MYSQLPASS", "password");
       define ("MYSQLDB", "sissystem"); 

        // connect to mysql server
        $db =  new mysqli(MYSQLHOST,MYSQLUSER,MYSQLPASS,MYSQLDB);

        // check if any connection error was encountered
        if(mysqli_connect_errno()){
            echo "Error: Could not connect to database.";
            exit;

        }
?>

这是我的插入功能代码

function enterStudent($db,$regid,$fName,$lName,$class,$dobirth,$parentName,$addrs,$s_area,$s_city,$s_state,$s_country,$s_zip,$s_phone,$s_email,$spix_link,$s_xinfo){

     //sql query
     //$query ="insert into students_info(student_regNo,student_firstName,student_lastName,class,student_dateOfBirth,student_fathersName,student_address,student_area,student_city,student_state,student_country,student_zipCode,student_phoneNum,student_email,student_imageLink,student_extraInfo) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

     $query ="insert into students_info values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";   
     //echo $query;
     $stmt = $db->prepare($query);
     $stmt->bind_param('ssssssssssssssss',$regid,$fName,$lName,$class,$dobirth,$parentName,$addrs,$s_area,$s_city,$s_state,$s_country,$s_zip,$s_phone,$s_email,$spix_link,$s_xinfo);

    // execute the insert query
    if($stmt->execute()){
        $affected_rows = $stmt->rowCount();
        echo "New Student Detail Added.";
        $stmt->close();
    }else{
        die("Unable to save.");
    }
 }

我的输入学生信息页面

<div>
     <?php
                include("../connect/dbconnector.inc");
                //include("../connect/PDOConnect.inc");
                //include("../includes/PDOConnect.inc");
                    include("../functions/db_functions.inc");
                    include("../functions/validate_functions.inc");
                    include("../functions/functions.inc");
                    //$db = connectdb();
                    //opendatabase();
                if(isset($_POST['submit'])){
                    if((isset($_POST['firstName']) && $_POST['firstName']=="") || (isset($_POST['lastName']) && $_POST['lastName']=="") || (isset($_POST['reg_id']) && $_POST['reg_id']=="") || (isset($_POST['class']) && $_POST['class']=="") || (isset($_POST['birthdate']) && $_POST['birthdate']=="") || (isset($_POST['fatherName']) && $_POST['fatherName']=="") || (isset($_POST['addrs']) && $_POST['addrs']=="") || (isset($_POST['area']) && $_POST['area']=="") || (isset($_POST['city'])&& $_POST['city']=="") || (isset($_POST['state']) && $_POST['state']=="") || (isset($_POST['country']) && $_POST['country']=="") || (isset($_POST['zip']) && $_POST['zip']=="") || (isset($_POST['phone']) && $_POST['phone']=="") || (isset($_POST['email']) && $_POST['email']=="") || (isset($_POST['more_info']) && $_POST['more_info'] == "") || (isset($_FILES['stud_image']) && $_FILES['stud_image'] =="")){
                        echo "<p style='width:900px;margin:0px;margin-left:auto;margin-right:auto;text-align:center;background-color:#66ff00'><font style='color:#ff0066;font-family:Arial'>all field are required</font></p>";                     
                    }else{
                    $fName = $_POST['firstName'];
                    $lName = $_POST['lastName'];
                    $regid = $_POST['reg_id'];
                    $st_class = $_POST['class'];
                    $dob = $_POST['birthdate'];
                    $fatherName = $_POST['fatherName'];
                    $address = $_POST['addrs'];
                    $st_area = $_POST['area'];
                    $st_city = $_POST['city'];
                    $st_state = $_POST['state'];
                    $st_country = $_POST['country'];
                    $st_zip = $_POST['zip'];
                    $st_phone = $_POST['phone'];
                    $st_email = $_POST['email'];
                    $st_xtra = $_POST['more_info'];
                    $image_link = $_FILES['stud_image'];

                    //upload the passport to students image folder and extract the image link
                    $filename = basename($image_link['name']);
                    $s_passLink = "studentspassport/".$filename;
                    uploadpassport($image_link,$s_passLink);
                    $success = enterStudent($db,$regid,$fName,$lName,$st_class,$dob,$fatherName,$address,$st_area,$st_city,$st_state,$st_country,$st_zip,$st_phone,$st_email,$s_passLink,$st_xtra);
                    if($success){
                        echo "New Student is added to the database";
                    }else {
                        echo "There is an error, please try again";
                    }
                }
            }   

            ?>

问题是它无法将值插入数据库我不知道为什么     

2 个答案:

答案 0 :(得分:1)

这是范围问题。 mysqli不是&#34;范围&#34;在你的功能中。

读这个。 http://php.net/manual/en/language.variables.scope.php - 请参阅&#34;全球&#34;关键字。

在您的函数enterStudent中写下:global $db

答案 1 :(得分:0)

你是if语句没有明确的声明..确保你从enterStudent()函数返回true或false ...