数据没有插入mysql.where是错的?

时间:2017-09-20 06:31:54

标签: php mysql database variables sql-insert

    $value1=$_POST['name'];
    $value1=mysqli_real_escape_string($link,$value1);
    $value2=$_POST['addr'];
    $value2=mysqli_real_escape_string($link,$value2);
    $value3=$_POST['phone'];
    $value3=mysqli_real_escape_string($link,$value3);
    $value4=$_POST['type'];
    $value4=mysqli_real_escape_string($link,$value4);
    $value5=$_POST['qty'];
    $value5=mysqli_real_escape_string($link,$value5);

    $sql1= "INSERT INTO guests       ('Name','Address','phone','pizza_type','quantity') VALUES ('$value1','$value2','$value3','$value4','$value5')";

    $records1=mysqli_query($link,$sql1)  or trigger_error(mysqli_error($link). " in ".$sql1);

     $sql2="SELECT * FROM 'guests'";
$records2=mysqli_query($link,$sql2) or trigger_error(mysqli_error($link)." in ".$sql2);

3 个答案:

答案 0 :(得分:0)

1)更改

$sql1= "INSERT INTO guests ('Name','Address','phone','pizza_type','quantity') VALUES ('$value1','$value2','$value3','$value4','$value5')";

$sql1= "INSERT INTO guests (`Name`,`Address`,`phone`,`pizza_type`,`quantity`) VALUES ('$value1','$value2','$value3','$value4','$value5')";

2)更改

$sql2="SELECT * FROM 'guests'";

$sql2="SELECT * FROM `guests`";

Single quotes不允许包含列名或表名。相反,请使用backtick

查找反引号

enter image description here

答案 1 :(得分:0)

对表名使用反引号(`)。

$sql1= "INSERT INTO `guests`....";
...
$sql2="SELECT * FROM `guests`";

更多:How to insert data to columns from PHP into MariaDB?

答案 2 :(得分:0)

更新了从查询中删除单引号

1. $sql1= "INSERT INTO guests (Name, Address, phone, pizza_type, quantity) VALUES ('$value1','$value2','$value3','$value4','$value5')";


2. $sql2="SELECT * FROM guests";