...

View Full Version : problem using a form to add to database



woodplease
01-26-2010, 05:46 PM
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=***** password=*****");

$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

mlseim
01-26-2010, 05:55 PM
Go into your MySQL admin page and see what parameters are set for "question" VARCHAR.
I wonder if the length was not specified correctly, so it's length is 1.

Also ... Remove this line from your post ... or erase the user and password (don't show us that) ...
$db = pg_connect("host=database.dcs..........");

woodplease
01-26-2010, 06:08 PM
i'm using postgres, but i went into the admin and the field length is correct, i've evn tried recreating the table, and i still get the same problem

tomws
01-26-2010, 06:32 PM
Aren't you seeing the same thing for quizref and questionno, also? See this section of code:

$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]) : '';


$i is undefined at that point, I think, so a zero index will be used. Since the POSTED values are strings (not arrays), you're setting the variables to the first character of the values. Remove that second array index.

woodplease
01-26-2010, 06:43 PM
thanks very much, the problem is now fixed



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum