php mysqli 插入不起作用

标签 php mysql sql insertion

我正在尝试将数据插入 mysql 但不起作用, 没有错误显示,下面是 php 脚本

    $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);

最佳答案

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();

关于php mysqli 插入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44092822/

相关文章:

mysql - 从mysql中的几个子查询中选择数据

php - mysql 查询,选择或更新性能缓慢

sql - AWS RDS Postgres : No space left on Device

javascript - 从头开始jQuery多文件上传

PHP:如何访问已在其外部声明的函数内部的变量?

java - Axis2 Web 服务 -> php

php - 单例模式怎么样

mysql - 将 MYD 文件导入 MySQL 数据库

sql - 为什么使用 phone = N'123 4' slower than phone = ' 1234' 查询?

mysql - Laravel 多表查询三张表