pneudralics Posted November 17, 2008 Share Posted November 17, 2008 I have a form with 3 question and answers which is in the question table with 3 rows. I can't seem to get mysql to update a specific row. Below is my page. Thanks for the help. <?php //This is the page for updating login info session_start(); if(!isset($_SESSION['username'])){ require ("error.php"); } else { include ('header.php'); ?> <table width="800" align="center"> <tr> <td align="center"> <b> <font color="red"><u>IMPORTANT!</u></font> <br /> <br /> This section updates your username and password login. It also updates your question and answer for your login. You may have to refresh the page to see the current Question, Answer, Username, and Password after you update it. <br /> <br /> The password <font color="red">"is not"</font> encrypted in the database. Anyone that have access to the database or the admin section can see the password. Make sure to update the password after you let someone access to the database or the admin section. <br /> <br /> <font color="red"> THE ANSWER, USERNAME, AND PASSWORD "IS" CASE SENSITIVE. ALPHANUMERIC CHARACTERS ONLY EXCEPT FOR THE "?" FOR THE QUESTION SECTION. SPECIAL CHARACTERS WILL AUTOMATICALLY BE STRIPPED. </font> <br /> <br /> </b> </td> </tr> <tr> <td align="center"> <hr /> <?php //Retrieve username and password $uaselect = "SELECT * FROM admin"; if ($uaresult = mysql_query ($uaselect)) { while ($row = mysql_fetch_array ($uaresult)) { $username = $row['username']; $password = $row['password']; } } //Update username and password if (isset ($_POST['uasubmit'])) { $pusername = ($_POST['username']); $pusername = preg_replace('/[^a-zA-Z0-9\s]/', '', trim($pusername)); $ppassword = ($_POST['password']); $ppassword = preg_replace('/[^a-zA-Z0-9\s]/', '', trim($ppassword)); if ( (!empty ($pusername)) && (!empty ($ppassword)) ) { //Update... $updateua = "UPDATE admin SET username=\"$pusername\", password=\"$ppassword\""; $updateuaresult = mysql_query ($updateua); echo '<font color="red">Username and Password has been successfully updated.<br />You may have to refresh the page to see them.</font><br /><br />'; } else { echo '<font color="red">Username or Password is empty.</font><br /><br />'; } } //Retrieve question and answer $qaselect = "SELECT * FROM question WHERE id = 1"; if ($qaresult = mysql_query ($qaselect)) { while ($row = mysql_fetch_array ($qaresult)) { $question = $row['question']; $answer = $row['answer']; } } //Update question and answer if (isset ($_POST['qasubmit'])) { $pquestion = ($_POST['question']); $pquestion = preg_replace('/[^a-zA-Z0-9\s?]/', '', trim($pquestion)); $panswer = ($_POST['answer']); $panswer = preg_replace('/[^a-zA-Z0-9\s]/', '', trim($panswer)); if ( (!empty ($pquestion)) && (!empty ($panswer)) ) { //Update... $updateqa = "UPDATE question WHERE id = 1 SET question=\"$pquestion\", answer=\"$panswer\" LIMIT 1"; $updateqaresult = mysql_query ($updateqa); echo '<font color="red">Question 1 and Answer 1 has been successfully updated.<br />You may have to refresh the page to see them.</font><br /><br />'; } else { echo '<font color="red">Question 1 or Answer 1 is empty.</font><br /><br />'; } } //Retrieve question2 and answer2 $qa2select = "SELECT * FROM question WHERE id = 2"; if ($qa2result = mysql_query ($qa2select)) { while ($row = mysql_fetch_array ($qa2result)) { $question2 = $row['question']; $answer2 = $row['answer']; } } //Update question2 and answer2 if (isset ($_POST['qa2submit'])) { $pquestion2 = ($_POST['question2']); $pquestion2 = preg_replace('/[^a-zA-Z0-9\s?]/', '', trim($pquestion2)); $panswer2 = ($_POST['answer2']); $panswer2 = preg_replace('/[^a-zA-Z0-9\s]/', '', trim($panswer2)); if ( (!empty ($pquestion2)) && (!empty ($panswer2)) ) { //Update... $updateqa2 = "UPDATE question WHERE id = 2 SET question=\"$pquestion2\", answer=\"$panswer2\" LIMIT 1"; $updateqa2result = mysql_query ($updateqa2); echo '<font color="red">Question 2 and Answer 2 has been successfully updated.<br />You may have to refresh the page to see them.</font><br /><br />'; } else { echo '<font color="red">Question 2 and Answer 2 is empty.</font><br /><br />'; } } //Retrieve question3 and answer3 $qa3select = "SELECT * FROM question WHERE id = 3"; if ($qa3result = mysql_query ($qa3select)) { while ($row = mysql_fetch_array ($qa3result)) { $question3 = $row['question']; $answer3 = $row['answer']; } } //Update question3 and answer3 if (isset ($_POST['qa3submit'])) { $pquestion3 = ($_POST['question3']); $pquestion3 = preg_replace('/[^a-zA-Z0-9\s?]/', '', trim($pquestion3)); $panswer3 = ($_POST['answer3']); $panswer3 = preg_replace('/[^a-zA-Z0-9\s]/', '', trim($panswer3)); if ( (!empty ($pquestion3)) && (!empty ($panswer3)) ) { //Update... $updateqa3 = "UPDATE question WHERE id = 3 SET question=\"$pquestion3\", answer=\"$panswer3\" LIMIT 1"; $updateqa3result = mysql_query ($updateqa3); echo '<font color="red">Question 3 and Answer 3 has been successfully updated.<br />You may have to refresh the page to see them.</font><br /><br />'; } else { echo '<font color="red">Question 3 and Answer 3 is empty.</font><br /><br />'; } } ?> <b>Update the username and password</b> <br /> <font size="2">(20 Max Characters Each Alphanumeric Only)</font> <br /> <br /> <form action="logininfo.php" method="post"> Username <input type="text" name="username" size="20" maxlength="20" value="<?php echo "$username"; ?>" /> <br /> <br /> Password <input type="text" name="password" size="20" maxlength="20" value="<?php echo "$password"; ?>" /> <br /> <br /> <input type="submit" name="uasubmit" value="Submit" /> </form> </td> </tr> <tr> <td align="center"> <hr /> <b>Update the question and answer</b> <br /> <font size="2">(50 Max Characters For Question 20 Max For Answer Alphanumeric Only Except for "?" for Question)</font> <br /> <br /> <form action="logininfo.php" method="post"> Question <input type="text" name="question" size="50" maxlength="50" value="<?php echo "$question"; ?>" /> <br /> <br /> Answer <input type="text" name="answer" size="20" maxlength="20" value="<?php echo "$answer"; ?>" /> <br /> <br /> <input type="submit" name="qasubmit" value="Submit" /> </form> </td> </tr> <tr> <td align="center"> <br /> <form action="logininfo.php" method="post"> Question 2 <input type="text" name="question2" size="50" maxlength="50" value="<?php echo "$question2"; ?>" /> <br /> <br /> Answer 2 <input type="text" name="answer2" size="20" maxlength="20" value="<?php echo "$answer2"; ?>" /> <br /> <br /> <input type="submit" name="qa2submit" value="Submit" /> </form> </td> </tr> <tr> <td align="center"> <br /> <form action="logininfo.php" method="post"> Question 2 <input type="text" name="question3" size="50" maxlength="50" value="<?php echo "$question3"; ?>" /> <br /> <br /> Answer 2 <input type="text" name="answer3" size="20" maxlength="20" value="<?php echo "$answer3"; ?>" /> <br /> <br /> <input type="submit" name="qa3submit" value="Submit" /> </form> </td> </tr> </table> <br /> <br /> <br /> <br /> <br /> <?php }//End session else ?> Quote Link to comment https://forums.phpfreaks.com/topic/133069-solved-cant-seem-to-get-mysql-to-update-only-one-row/ Share on other sites More sharing options...
.josh Posted November 17, 2008 Share Posted November 17, 2008 $updateqa = "UPDATE question SET question='$pquestion', answer='$panswer' where id=1"; Need to change your other ones too. Quote Link to comment https://forums.phpfreaks.com/topic/133069-solved-cant-seem-to-get-mysql-to-update-only-one-row/#findComment-692021 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.