Ratana Ratana - 6 months ago 23
SQL Question

PHP SQL Error for INSERT FUNCTION

I am using using

INSERT
function in
PHP
to insert data into a table.
Here's the code given below:
Note, when I execute the code, i get an error:


Error: INSERT INTO tbl_test (name) VALUES​​ ('222') You have an error
in your SQL syntax; check the manual that corresponds to your MariaDB
server version for the right syntax to use near 'VALUES​​ ('222')' at
line 1


<?php
$servername = "localhost";
$username = "root";
$passwords = "";
$dbname = "cmg_log";

//open connection
$con = mysqli_connect ($servername, $username, $passwords, $dbname);
if (!$con){
die("Connection fail. ".mysqli_connect_error());
}

//insert into database
$query="INSERT INTO tbl_test (name) VALUES​​ ('222')";
if (mysqli_query($con, $query)){
echo "Successful";
}
else{
echo "Error: " .$query ."<br>" . mysqli_error($con);
}
?>

Answer

Looks like case sensitive error, Just use lower case for values

 $query="INSERT INTO tbl_test (name) values ('222')";

Error is annoying but i tried your code and get same error, after replace to lower it works