Link to home
Start Free TrialLog in
Avatar of kenjpete
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','$username','$password','$state','$active');";
       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
ASKER CERTIFIED SOLUTION
Avatar of Brian Tao
Brian Tao
Flag of Taiwan, Province of China image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Brian is right.  The alternate syntax is using back tiks which are similar to single quotes.
(`first`, `last`, `uname`, `password`, `state`, `active`) 

Open in new window

Avatar of kenjpete
kenjpete

ASKER

Thank You! I knew it had to be something simple!