1

Error: INSERT INTO grocery ('GrocerID', 'GrocerName', 'Address', 'LogoImage') VALUES ('GID0072', 'BigBazaar','India, Andhra Pradesh, 522124','WIN_20150817_121614.JPG') You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''GrocerID', 'GrocerName', 'Address', 'LogoImage') VALUES ('GID0072', 'BigBazaar' at line 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

Can someone please tell me what the mistake I'm doing here??

I have used database name: task, table name: grocery But i'm not able to understand where I'm doing the mistake.

Thankyou

Saty
  • 22,443
  • 7
  • 33
  • 51

3 Answers3

1

not write coloumn in '' use backticks ``

INSERT INTO grocery 
(GrocerID, GrocerName, Address, LogoImage) 
VALUES ('GID0072', 'BigBazaar','India, Andhra Pradesh, 522124','WIN_20150817_121614.JPG')
Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20
1

Either Remove '

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

OR

Replace ' with ` (backtick)

[NOTE: You can find backtick below Esc key in keyboard]

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

And, Use real_escape_string() to prevent SQL Injection Attacks

PHP provides real_escape_string() to escape special characters in a string before sending a query to MySQL. This function was adopted by many to escape single quotes in strings and by the same occasion prevent SQL injection attacks. However, it can create serious security flaws when it is not used correctly.

$GrocerName = $conn->real_escape_string($_POST['GrocerName']);
$Address = $conn->real_escape_string($_POST['Address']);
$LogoImage = $conn->real_escape_string($_POST['LogoImage']);
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
0
$sql = "INSERT INTO grocery (`GrocerID`, `GrocerName`, `Address`, `LogoImage`)
        VALUES ('$GrocerID', '$GrocerName','$Address','$LogoImage')";

Do not use quotes for column_name.

Shahzad Barkati
  • 2,532
  • 6
  • 25
  • 33
Shailesh Katarmal
  • 2,757
  • 1
  • 12
  • 15