woodplease Posted January 26, 2010 Share Posted January 26, 2010 i have created a form in which enters a question, and releveant answers into a database. the questions go into one table, and the ansers go into another. i have a piece of code (below) which enters this information into the 2 tables, but in the question table, only the first character of the question field is entered and the rest of the question is missing, e.g. if the question is "How old are you?" only "h" is entered into the table. All of the answer information enters into the answer table correctly. i have made sure that the field length is long enough in both the database and the form (60 characters), so that is not the problem. Can anyone see anything else that is wrong? <?php $db = pg_connect("host=database.dcs.aber.ac.uk port=5432 dbname=agd8 user=agd8 password=quizdom1"); $output = ""; if (isset($_POST['questionno']) && (isset($_POST['answerno']) && is_array($_POST['answerno']))) { // make sure that we have both sections, just in case. // first lets insert the question data: $questionno = isset($_POST['questionno'][$i]) ? pg_escape_string($_POST['questionno'][$i]) : ''; // ternary operator (? : ) acts as a short if/else. If isset set the variable to it after escaping it, else set it to '' $question = isset($_POST['question'][$i]) ? pg_escape_string($_POST['question'][$i]) : ''; $quizref = isset($_POST['quizref'][$i]) ? pg_escape_string($_POST['quizref'][$i]) : ''; $query="INSERT INTO questions (questionno, question, quizref) VALUES ('".$questionno."', '".$question."', '".$quizref."')"; pg_query($query) or trigger_error ('Error adding new question: ' . pg_last_error()); $output .= "Question has been added succesfully to the quiz reference {$quizref}<br />"; $cnt = count($_POST['answerno']); for ($i=0; $i < $cnt; $i++) { // first define variables to avoid notice errors and escape the data: $answerno = isset($_POST['answerno'][$i]) ? pg_escape_string($_POST['answerno'][$i]) : null; $answer = isset($_POST['answer'][$i]) ? pg_escape_string($_POST['answer'][$i]) : null; $answervalue = isset($_POST['answervalue'][$i]) ? pg_escape_string($_POST['answervalue'][$i]) : null; // check if all values are null, if they are skip the insert. if (is_null($answer) && is_null($answerno) && is_null($answervalue)) continue; // continue the loop as there was no data entered there to insert. $queryanswers="INSERT INTO answers (answerno, answer, answervalue, questionno, quizref)VALUES ('".$answerno."', '".$answer."','".$answervalue."', '".$questionno."', '".$quizref."')"; pg_query($queryanswers) or trigger_error ('Error adding new answers: ' . pg_last_error()); } $output .= "The answers have been succesfully added to the quiz reference {$quizref}.<br />"; } ?> <html> <head> <title>Add </title> </head> <body> <?php echo $output; ?> <form method="post" action=""> quizref: <br/> <input type="text" name ="quizref" size="5" /><br/> Question Number: <br/> <input type="text" name="questionno" size="5" /><br/> Question: <br/> <input type="text" name="question" size="60" /><br/> <table> Answers<br/><br/> <tr><td>Answer No.</td><td>Answer</td><td>Value</td></tr> <tr><td><input type="text" name ="answerno[]" size="5" /></td> <td><input type="text" name ="answer[]" size="60" /></td> <td><input type="text" name ="answervalue[]" size="5" /></td></tr> <tr><td><input type="text" name ="answerno[]" size="5" /></td> <td><input type="text" name ="answer[]" size="60" /></td> <td><input type="text" name ="answervalue[]" size="5" /></td></tr> <tr><td><input type="text" name ="answerno[]" size="5" /></td> <td><input type="text" name ="answer[]" size="60" /></td> <td><input type="text" name ="answervalue[]" size="5" /></td></tr> <tr><td><input type="text" name ="answerno[]" size="5" /></td> <td><input type="text" name ="answer[]" size="60" /></td> <td><input type="text" name ="answervalue[]" size="5" /></td></tr> <br/><br/> </table> <input type="submit" value="Send and Add another Add Questions" /> <a href="addfinish.php"><input type="submit" value="Add and finish" /></a> </form> </body> </html> Any Help would be greatly appreciated Thanks Link to comment https://forums.phpfreaks.com/topic/189883-problem-using-a-form-to-add-to-database/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.