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
kenjpeteAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian TaoSenior Business Solutions ConsultantCommented:
You don't need the single quotes around column names.  The SQL should look like this:
$sql = "INSERT INTO authors (first, last, uname, password, state, active) VALUES ('$first','$last','$username','$password','$state','$active');";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
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

0
Ray PaseurCommented:
Some "getting started" links here... Sitepoint resources are particularly good.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
kenjpeteAuthor Commented:
Thank You! I knew it had to be something simple!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.