I'm trying to insert data into a mysql database using php. Seems like a simple task.
While using dummy data (just strings of text), it was working perfectly fine. And when I replaced the dummy data with the actual variables, I get this error:
ERROR: Could not able to execute INSERT INTO tbl_users (id, timestamp, testid, email, wants_newsletter, ip, country) VALUES (NULL, CURRENT_TIMESTAMP, , , , , ). 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 ' , , , )' at line 1
Below is the script that I'm using. I am 100% sure that all the variables are working. I currently have them spitting out dummy data as well.
$testid = 1;
$email = 'test@yahoo.com'
$wants_newsletter = true;
//get $ip
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$country = 'US';
$user_id = 100;
$question = 14
$answer = 14
function saveToDB() {
/* Attempt MySQL server connection. */
$link = mysqli_connect("localhost", "$user", "$pass", "$db");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt insert query execution
$sql = "INSERT INTO tbl_users (id, timestamp, testid, email, wants_newsletter, ip, country) VALUES (NULL, CURRENT_TIMESTAMP, $testid, $useremail, $newsletter, $ip, $country)";
if(mysqli_query($link, $sql)){
echo "Records inserted successfully.";
$user_id = $link->insert_id;
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Attempt insert query execution
$sql = "INSERT INTO tbl_answers (id, user_id, question_id, answer) VALUES (NULL, $user_id, $question, $answer)";
if(mysqli_query($link, $sql)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
}//end saveToDB
saveToDB(); //Call saveToDB()
I've been busting my head for hours trying to figure out this error. It might be something small that I'm overlooking and perhaps I need a fresh pair of eyes on it.
Any help would be greatly appreciated.