1

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); 
Kari
  • 115
  • 1
  • 1
  • 10
  • what are the errors you have when it fails? are you validating your data? use print_r() to dump out your final sql statement and look at that. – noid Oct 31 '18 at 09:16
  • this is a link where you can find the solution [here](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-insert-statement) – Ferdinando Oct 31 '18 at 09:17

2 Answers2

1

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;
}
annydreams
  • 53
  • 1
  • 5
0

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.

Sarwar
  • 59
  • 8
  • It IS a special character problem, how could I fix that? I've seen the magic_quotes function but see that its been deprecated, so what could be done to alleviate this problem? – Kari Nov 01 '18 at 06:42
  • yo can use the php function addslashes to avoid this issue and when you will retrieve the data then you can use stripslashes. Or you can pass your whole query through this function mysql_real_escape_string($string) – Sarwar Nov 01 '18 at 06:46