MYSQL数据库存储错误

时间:2015-12-17 10:38:17

标签: php mysql database forms

  

错误:INSERT INTO杂货店('GrocerID','GrocerName','地址',   'LogoImage')VALUES('GID0072','BigBazaar','印度,Andhra Pradesh,   522124','WIN_20150817_121614.JPG')您的SQL中有错误   句法;查看与MySQL服务器版本对应的手册   要在''GrocerID','GrocerName','地址'附近使用正确的语法,   'LogoImage')VALUES('GID0072','BigBazaar'在第1行

    <?php
$servername = "localhost";
$username = "root";
$password = "secret";
$dbname = "task";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

$GrocerID=$_POST['GrocerID']; 
$GrocerName=$_POST['GrocerName'] ;
$Address=$_POST['Address'];
$LogoImage=$_POST['LogoImage'] ;




// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO grocery ('GrocerID', 'GrocerName', 'Address', 'LogoImage')
VALUES ('$GrocerID', '$GrocerName','$Address','$LogoImage')";

if ($conn->query($sql) === TRUE) {
    header('Location:task.html');
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Data Base image

有人可以告诉我,我在做什么错误吗?

我用过 数据库名称:任务, 表名:杂货店 但是我无法理解我在做错的地方。

三江源

3 个答案:

答案 0 :(得分:1)

不要在''使用backticks``

中写下coloumn
INSERT INTO grocery 
(GrocerID, GrocerName, Address, LogoImage) 
VALUES ('GID0072', 'BigBazaar','India, Andhra Pradesh, 522124','WIN_20150817_121614.JPG')

答案 1 :(得分:1)

删除'

INSERT INTO grocery 
(GrocerID, GrocerName, Address, LogoImage) 
VALUES ('GID0072', 'BigBazaar','India, Andhra Pradesh, 522124','WIN_20150817_121614.JPG')

OR

'替换为“(反引号)

[注意:您可以在键盘backtick下方找到Esc key

INSERT INTO grocery 
(`GrocerID`, `GrocerName`, `Address`, `LogoImage`) 
VALUES ('GID0072', 'BigBazaar','India, Andhra Pradesh, 522124','WIN_20150817_121614.JPG')

并使用 real_escape_string()来阻止SQL Injection Attacks

  

PHP提供了real_escape_string()来转义a中的特殊字符   在向MySQL发送查询之前的字符串。这个功能被采用了   许多人在字符串和同一场合逃避单引号   防止SQL注入攻击。但是,它可以创造严重的安全性   如果使用不当,则会出现缺陷。

$GrocerName = $conn->real_escape_string($_POST['GrocerName']);
$Address = $conn->real_escape_string($_POST['Address']);
$LogoImage = $conn->real_escape_string($_POST['LogoImage']);

答案 2 :(得分:0)

$sql = "INSERT INTO grocery (`GrocerID`, `GrocerName`, `Address`, `LogoImage`)
        VALUES ('$GrocerID', '$GrocerName','$Address','$LogoImage')";

不要将 quotes 用于column_name