2017-05-21 67 views
0

我試圖將數據插入到MySQL,但沒有工作, 任何錯誤都出現了波紋管的PHP腳本PHP mysqli的插入不工作

$mysqli = new mysqli('localhost','root','','realestate'); 

    if($mysqli->connect_error){ 
    printf("can not connect database %s\n",$mysqli->connect_error); 
    exit(); 
    } 

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

    $a_name=$_POST['a_name']; 
    $a_number=$_POST['a_number']; 
    $email=$_POST['email']; 
    $password=$_POST['password']; 


    $target_dir="uploads/"; 
    $target_file=$target_dir . basename($_FILES["a_image"]["name"]);  
    $temp_file=$_FILES["a_image"]["name"]; 

    move_uploaded_file($_FILES["a_image"]["tmp_name"], $target_file); 

    $query="INSERT INTO agent (a_name,a_number,email,password,a_image) 
    VALUES ('$a_name',$a_number,'$email','$password','$temp_file')"; 
    $insert = $mysqli->query($query); 

回答

0
error_reporting(E_ALL);//show all errors 

$mysqli = new mysqli('localhost','root','','realestate'); 

    if($mysqli->connect_error){ 
    printf("can not connect database %s\n",$mysqli->connect_error); 
    exit(); 
    } 

if (isset($_POST['submit'])){ 
    //make sure $_POST is not null 
    var_dump('Posted variables are '. $_POST.' and files are '. $_FILES);//Comment out after debugging 
    $a_name=$_POST['a_name']; 
    $a_number=$_POST['a_number']; 
    $email=$_POST['email']; 
    $password=$_POST['password']; 


    $target_dir="uploads/"; 
    $target_file=$target_dir . basename($_FILES["a_image"]["name"]);  
    $temp_file=$_FILES["a_image"]["name"];//make sure this is not null 

    move_uploaded_file($_FILES["a_image"]["tmp_name"], $target_file); 

    $query="INSERT INTO `agent` (`a_name`,`a_number`,`email`,`password`,`a_image`) 
    VALUES ('$a_name','$a_number','$email','$password','$temp_file')"; 

    if($mysqli->query($query) == true){//check for success 
     echo 'Success';//display message of success 
    } else { 
     echo $mysqli->error;//display error message 
    } 

//The above code is prone to SQL injection. Below is using prepared statements: 
// prepare and bind 
$stmt = $mysqli->prepare("INSERT INTO `agent`(`a_name`,`a_number`,`email`,`password`,`a_image`) VALUES (?, ?, ?,?,?)"); 
$stmt->bind_param("sssss", $a_name, $a_number, $email, $password, $temp_file); 

$stmt->execute(); 
+0

u能解釋如何檢查,如果電子郵件地址已存在在數據庫中? –