kenjpete
asked on
Syntax Error with PHP/MySQL Insert Statement
I have been trying to figure this out for two days and can't seem to find any syntax issues. When I run the following piece of PHP code in my page I get the error listed below. All fields in the insert statement are varchar in the database.
Also I have two other fields in my database, one is called ID and is an INT with auto increment. The other is a date field called created and is set to CURRENT_TIMESTAMP.
<?php
//set form variables
$first=$_POST['first'];
$last=$_POST['last'];
$username=$_POST['username '];
$password=$_POST['password '];
$state=$_POST['state'];
$active=$_POST['active'];
$message="User added successfully!";
//Check connection
if (mysqli_connect_errno()) {
echo 'Connection to database failed:' .mysqli_connect_error();
exit();
}
//Insert record
if (isset($_POST['submit'])){
$sql = "INSERT INTO authors ('first', 'last', 'uname', 'password', 'state', 'active') VALUES ('$first','$last','$userna me','$pass word','$st ate','$act ive');";
if (mysqli_query($conn, $sql)) {
echo $message;
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn); //display error
}
}
?>
****************
Error: INSERT INTO authors ('first', 'last', 'uname', 'password', 'state', 'active') VALUES ('John','Smith','jsmith',' 1234','CT' ,'T');
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 ''first', 'last', 'uname', 'password', 'state', 'active') VALUES ('John','Smith',' at line 1
Also I have two other fields in my database, one is called ID and is an INT with auto increment. The other is a date field called created and is set to CURRENT_TIMESTAMP.
<?php
//set form variables
$first=$_POST['first'];
$last=$_POST['last'];
$username=$_POST['username
$password=$_POST['password
$state=$_POST['state'];
$active=$_POST['active'];
$message="User added successfully!";
//Check connection
if (mysqli_connect_errno()) {
echo 'Connection to database failed:' .mysqli_connect_error();
exit();
}
//Insert record
if (isset($_POST['submit'])){
$sql = "INSERT INTO authors ('first', 'last', 'uname', 'password', 'state', 'active') VALUES ('$first','$last','$userna
if (mysqli_query($conn, $sql)) {
echo $message;
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn); //display error
}
}
?>
****************
Error: INSERT INTO authors ('first', 'last', 'uname', 'password', 'state', 'active') VALUES ('John','Smith','jsmith','
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 ''first', 'last', 'uname', 'password', 'state', 'active') VALUES ('John','Smith',' at line 1
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Some "getting started" links here... Sitepoint resources are particularly good.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
ASKER
Thank You! I knew it had to be something simple!
Open in new window