user2466886 user2466886 - 1 year ago 76
MySQL Question

PHP Inserting into SQL table error with syntax

I'm learning how to use PHP with MySQLi and I'm having issues correctly inserting information into a table I have set up. Here's my code:

$temp_category = $_POST['category'];
$temp_fname = $_POST['recipient-fname'];
$temp_lname = $_POST['recipient-lname'];
$temp_email = $_POST['recipient-email'];
$temp_date = $_POST['award-date'];
$auth_userId = $_SESSION['id'];
$auth_email = $_SESSION['email'];

$mysqli = mysqli_connect($host, $dbUser, $dbPword, $db, $port);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;

$sql = 'INSERT INTO award_entry (award_category,granted_time,authorizing_user_id,authorizing_user_email,fname,lname,recipient_email),
VALUES ("'.$temp_category.'", "'.$temp_date.'","'.$auth_userId.'", "'.$auth_email.'","'.$temp_fname.'", "'.$temp_lname.'","'.$temp_email.'")';

if ($mysqli->query($sql) === TRUE) {
echo "award_entry successfully updated";
else {
echo "Error: " . $sql . "<br>" . $mysqli->error;

I keep getting the error 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 ' VALUES ("EmployeeWeek", "2016-11-05","20", "","Jack",' at line 1"

But I don't see what is wrong with my code.

Answer Source
 $sql = 'INSERT INTO award_entry (award_category,granted_time,authorizing_user_id,authorizing_user_email,fname,lname,recipient_email)***,***
VALUES ("'.$temp_category.'", "'.$temp_date.'","'.$auth_userId.'", "'.$auth_email.'","'.$temp_fname.'", "'.$temp_lname.'","'.$temp_email.'")';

get rid of the comma after the close parenthesis right before VALUES