woodplease Posted February 2, 2010 Share Posted February 2, 2010 I'm trying to update values in a database using a form. I'm reading the current values into the form, which works fine, and then posting them to another page which holds the code to update the form, but when the page is displayed, its just blank. Any ideas? the code that sends the data <?php $db = pg_connect("host=database.dcs.aber.ac.uk port=5432 dbname=agd8 user=agd8 password=csgroup5"); $quizno = $_POST['quizno']; ?> <?php $id = $_GET['id']; echo' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><link rel="stylesheet" type="text/css" href="sheet1.css" /> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Add Questions to a Quiz</title> </head> <body> <div id="container"> <div id="header"></div> <div id="maincontent"> <form method="post" action="questionupdated.php"><?php echo $output; ?>'; echo'<br/>You are editing question '.$id. ' from quiz number '.$quizno.'<br/><br/>'; echo' The question currently likes like:<br/><br/>'; $query4 = "SELECT * FROM questions WHERE quizref = " .$quizno. "AND questionno=".$id ; $result2 = pg_query($query4) or die ("Query failed"); //let's get the number of rows in our result so we can use it in a for loop $numofrows = pg_num_rows($result2); $row = pg_fetch_array($result2); echo'Question : '.$row['questionno'].' : <input type="text" name="question" size="60" value="'.$row['question'].'"/>'; $query2 = "SELECT answers.answerno, answers.answer, answers.answervalue, answers.questionno, answers.quizref, answers.answerref WHERE answers.questionno = ".$row['questionno']. " AND answers.quizref = ".$quizno; $result3 = pg_query($query2) or die ("Query failed"); //let's get the number of rows in our result so we can use it in a for loop $numofrows2 = pg_num_rows($result3); echo '<center><table><tr><td>Answer No.</td><td>Answer</td><td>Value</td></tr>'; for($j = 0; $j < $numofrows2; $j++) { $row2 = pg_fetch_array($result3); //get a row from our result set echo ' <tr><td><input type="text" name ="answerno[]" size="5" value="'.$row2['answerno'].'"/></td> <td><input type="text" name ="answer[]" size="60" value="'.$row2['answer'].'"/></td> <td> <input type="text" name="answervalue[]" size="5" id="answervalue" value="'.$row2['answervalue'].'" /> '; // Notice how cutting in/out of SINGLE-QUOTES is much easier to read and debug } echo'</table> <input type="submit" value="Update" /></center></form> </div> </div> </body> </html>' ?> the code that is supposed to update the database( but doesnt) <?php $db = pg_connect("host=database.dcs.aber.ac.uk port=5432 dbname=agd8 user=agd8 password=csgroup5"); $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']) ? 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']) ? pg_escape_string($_POST['question']) : ''; $quizref = isset($_POST['quizref']) ? pg_escape_string($_POST['quizref']) : ''; $query="Update 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="UPDATE 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 />"; } echo $output; ?> Any help would be great. i know theres a lot of code, but i wanted to give as much info as possible Also, you can see the pages online here http://users.aber.ac.uk/agd8/quizdom/editquestions.php?id=1 Thanks Link to comment https://forums.phpfreaks.com/topic/190714-update-database-values-with-form/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.