Kari
Kari

Reputation: 125

Inserting survey into SQL database with PHP

I have an HTML survey. I am handling it with PHP and passing it with PHP into a MySQl database. Before this section of code, I post every input, and echo it out as a summary. Every input is reading correctly in the summary, so the form seems to be working fine. I manually input 1 dataset to test the database columns, and then 1 set of data went straight from the form to the database without issue. Now, however, I tried to insert another set of data and it isn't uploading.

I have each field outlined because I have another field that is an autoincrement for when a row is inserted. On a previous form handle I did, I also had an autoincrement field that worked perfectly without including it in the insertion process, so I'm fairly certain I don't need to include it here.

Is there something in the insert code that I've overlooked? I can manually input results just fine that match exactly what I put into the survey fields, but the digital upload from survey submission to database is not being completed. I AM connected to the database, because I have an error for failed connection set up that isn't popping up (it is paired with $dbcon. $dbcon stands for database connection).

 //Data Insertion
 $res_ins = "INSERT INTO Survey (name, zip, 
 gender, income, savings, disaster, work, 
 res_road, work_road, evacuation, lodging, 
 injury, children, num_child, educ, city_prep, 
 PrepComments, emer_res, info, prep, fut_prep) 

 VALUES ('$name', '$zip', '$gender', '$income', 
 '$savings', '$disaster', '$work', '$res_road', 
 '$work_road', '$evacuation', '$lodging', 
 '$injury', '$children', '$num_child', '$educ', 
 '$city_prep', '$PrepComments', '$emer_res', 
 '$info', '$prep', '$fut_prep')"; 

    $insert = $dbcon->query($res_ins);

 //Terminate connection to database and end 
 insertion
 mysqli_close($dbcon); 

Upvotes: 1

Views: 341

Answers (2)

annydreams
annydreams

Reputation: 53

I can't comment because of reputation, so I have to give you a hint in the answer: did you try to use this query directly on your database, using some interface?

However, you could try to add some rows to see what is the error, before to close the connection:

if ($dbcon->query($res_ins) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $res_ins. "<br>" . $dbcon->error;
}

Upvotes: 1

Sarwar
Sarwar

Reputation: 59

before executing, print the query. it will help you to find out the root cause. most common reason of this type of issue is special character. You can check is there any special character in your query.

Upvotes: 0

Related Questions