1

I've looked at some of the other similar questions, but still can't seem to get it working. FWIW, I am not getting any specific error message.

<?php

// Create connection
$con=mysqli_connect("localhost","username","password");
mysqli_select_db($con, "database");
//let's start our session, so we have access to stored data
session_start();

$class = mysqli_real_escape_string($_SESSION['exclass']);
$fullname = mysqli_real_escape_string($_SESSION['fullname']);
$june = mysqli_real_escape_string($_SESSION['June']);
$july = mysqli_real_escape_string($_SESSION['July']);
$email = mysqli_real_escape_string($_POST['email']);
$phone = mysqli_real_escape_string($_POST['phone']);

//let's create the query
$insert_query = "INSERT INTO Responses (class,fullname,June,July,email,phone) VALUES ('$class','$fullname',$june,$july,'$email','$phone',)";

//let's run the query
$result = mysqli_query($con,$insert_query);

if($result){
    echo "Your response has been recorded. Thank you!";

}else{

    printf("Insert failed: %s\n", mysqli_error());
}
?>
4
  • 1
    What happens when you run the query directly in MySQL either through the command line or a tool like phpmyadmin? Commented Mar 7, 2013 at 17:27
  • 1
    You have a trailing comma after '$phone' which breaks the query. Not sure why you aren't seeing the error message though - that should report a syntax error. Commented Mar 7, 2013 at 17:28
  • Is there a reason why you've not enclosed $june,$july in single quotes? Commented Mar 7, 2013 at 17:28
  • Why not check that you can connect? Commented Mar 7, 2013 at 17:28

2 Answers 2

3

MySQL does not support trailing comma notation.

Thus, replace:

,'$phone',)

with

,'$phone')

Also, add a fallback condition to see the error output buffer:

mysqli_query($con, $insert_query) or die(mysqli_error($con));

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to all who pointed out the trailing comma, I did miss that.
1
"INSERT INTO Responses (class,fullname,June,July,email,phone) VALUES    ('$class','$fullname',$june,$july,'$email','$phone',<--- LOOK AT THIS COMMA)

This will more than likely explain the failure to insert into the database. Remove the comma from the SQL string, and it will parse correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.