jlindsey Posted November 28, 2012 Share Posted November 28, 2012 (edited) I know what the issue is, I just can't figure out the solution. The problem is I have a page which searches for users in a database by their User ID. That is the first form that is processed and it works great. It finds the user and outputs the correct information. The second form is then supposed to let users enter data and then update the database. Only it doesn't update the database at all. I know the issue has something to do with how I'm processing two forms on one page. I just can't seem to get the second form to work and update the database. I think I am this close to getting this worked out I just need a little hope with the script below. <?php $form = "<form id='search' action='./purchasegifts.php' method='get'> <label>User ID: <input type='text' name='keyname' /> </label> <input type='submit' name='viewgift' value='Search Gift Registry' /> </form>"; echo $form; echo "<br></br>"; $searchTerm = trim($_GET['keyname']); //check whether the name parsed is empty if($searchTerm == "") { echo ""; exit(); } //database connection info $host = "localhost"; //server $db = "users"; //database name $user = "######"; //dabases user name $pwd = "#######"; //password //connecting to server and creating link to database $link = mysqli_connect($host, $user, $pwd, $db); //MYSQL search statement $sql = "SELECT * FROM users WHERE userid='$searchTerm'"; $myData = mysqli_query($link, $sql); $myData2 = mysqli_query($link, $sql); $myData3 = mysqli_query($link, $sql); if(mysqli_num_rows($myData) >= 1) { echo '<table align="center" cellspacing="0" cellpadding="5" border="2"> <tr> <td align="center"><b>First Name</b></td> <td align="center"><b>Last Name</b></td> </tr>'; while($row = mysqli_fetch_array($myData2)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; } echo "</table>"; echo "<br></br>"; if (isset($_POST['updatebtn'])){ $UpdateQuery = "UPDATE users SET purchased_gift='$_POST[purchased_gift]' WHERE username='$username'"; mysql_query($UpdateQuery, $link); }; echo '<form id="update" method="post" action="purchasegifts.php">'; // Table header. echo '<table align="center" cellspacing="0" cellpadding="5" border="2"> <tr> <td align="center"><b>Gifts</b></td> <td align="center"><b>Purchase Gifts</b></td> </tr>'; { while ($row = mysqli_fetch_array($myData3)) { echo "<tr>"; echo "<td>" . $row['Gift'] . "</td>"; echo ' <td align="left"><input type="text" size="25" name="purchased_gift" value="'.$row['purchased_gift'].'"></td> </tr>'; echo "<tr>"; echo "<td>" . $row['Gift2'] . "</td>"; echo ' <td align="left"><input type="text" size="25" name="purchased_gift2" value="'.$row['purchased_gift2'].'"></td> </tr>'; } } echo '</table>'; echo '<br /><div align="center"><input type="submit" name="Submit" value="Purchase Gifts" /></div> <input type="hidden" name="updatebtn" value="updatebtn" />'; echo '</form>'; mysqli_close($link); } else echo "There was no matching record for the User ID " . $searchTerm; ?> Edited November 28, 2012 by jlindsey Quote Link to comment https://forums.phpfreaks.com/topic/271311-stuck-on-a-two-form-processing-page-which-processes-the-first-form-but-not-the-second/ Share on other sites More sharing options...
MDCode Posted November 28, 2012 Share Posted November 28, 2012 1. Remove your connection details. 2. There is no where that you are using an update query so...won't get very far Quote Link to comment https://forums.phpfreaks.com/topic/271311-stuck-on-a-two-form-processing-page-which-processes-the-first-form-but-not-the-second/#findComment-1395991 Share on other sites More sharing options...
mrMarcus Posted November 28, 2012 Share Posted November 28, 2012 (edited) How are you updating the db table? Can you show your update query (presumably in purchasegifts.php). And this just plain hurts to see: $myData = mysqli_query($link, $sql); $myData2 = mysqli_query($link, $sql); $myData3 = mysqli_query($link, $sql); Consolidate your 3 queries into 1 and make better use of your HTML display code. Edited November 28, 2012 by mrMarcus Quote Link to comment https://forums.phpfreaks.com/topic/271311-stuck-on-a-two-form-processing-page-which-processes-the-first-form-but-not-the-second/#findComment-1395992 Share on other sites More sharing options...
jlindsey Posted November 28, 2012 Author Share Posted November 28, 2012 (edited) Well that is where I'm having trouble, cant figure out what to do with my updatequery or where to put it. I left it out of the script above hoping someone would help me with the updatequery. I updated the script above with the updatequery but I know it needs work. Just getting into php so learning as I go. Edited November 28, 2012 by jlindsey Quote Link to comment https://forums.phpfreaks.com/topic/271311-stuck-on-a-two-form-processing-page-which-processes-the-first-form-but-not-the-second/#findComment-1395996 Share on other sites More sharing options...
MDCode Posted November 28, 2012 Share Posted November 28, 2012 (edited) Where are you defining $username? Edited November 28, 2012 by SocialCloud Quote Link to comment https://forums.phpfreaks.com/topic/271311-stuck-on-a-two-form-processing-page-which-processes-the-first-form-but-not-the-second/#findComment-1395998 Share on other sites More sharing options...
jlindsey Posted November 28, 2012 Author Share Posted November 28, 2012 (edited) What about my two forms? First form works but second does not. The second form basically keeps calling for the first for which makes sense. So how can I differentiate between the two forms? Edited November 28, 2012 by jlindsey Quote Link to comment https://forums.phpfreaks.com/topic/271311-stuck-on-a-two-form-processing-page-which-processes-the-first-form-but-not-the-second/#findComment-1396001 Share on other sites More sharing options...
MDCode Posted November 28, 2012 Share Posted November 28, 2012 (edited) Use an if(isset($_POST['name of submit button'])) I would also highly suggesting organizing your queries as suggested above. Move php processing above any html and use the form normally Edited November 28, 2012 by SocialCloud Quote Link to comment https://forums.phpfreaks.com/topic/271311-stuck-on-a-two-form-processing-page-which-processes-the-first-form-but-not-the-second/#findComment-1396002 Share on other sites More sharing options...
jlindsey Posted November 28, 2012 Author Share Posted November 28, 2012 OK I did some cleaning up to the script, Still cannot get the second form which is located inside the script to work. I moved the first form outside my php tags and it call on the data just fine and pulls up the correct information that is needed. After the search is complete a user can then enter data into a new form that was displayed by the first search. Except my script is not updating my database to whatever it is that I entered in the form. But if I go into the database and manually enter it, the information is then displayed on the form where I need to update information. So obviously I am doing something wrong with this script. I cleaned it up a bunch so it should be easier to read. Thank You for any help. <form action = "" method = "GET"> <input type = "text" name = "keyword" /> <input type = "submit" name = "searchbtn" value = "Search Gift Registry" /> </form> <br></br> <?php if($_GET) { $searchTerm = $_GET['keyword']; $connect = mysql_connect("localhost","#####","######"); if($connect) { mysql_select_db("users",$connect); $query = "SELECT * FROM users WHERE userid='$searchTerm'"; $results = mysql_query($query); while($row = mysql_fetch_array($results)) { $output .= "<b>First Name:</b> " . $row['FirstName'] . "<br />"; $output .= "<b>Last Name:</b> " . $row['LastName'] . "<br />"; $output .= "<b>User ID:</b> " . $row['userid'] . "<br />"; } echo $output; if (isset($_POST['submitbtn'])){ $UpdateQuery = "UPDATE users SET purchased_gift=$_POST[purchased_gift]' WHERE userid='$searchTerm'"; mysql_query($UpdateQuery, $connect); }; $query = "SELECT * FROM users WHERE userid='$searchTerm'"; $myData = mysql_query($query, $connect); echo '<form method="post" action="">'; // Table header. echo '<table align="center" cellspacing="0" cellpadding="5" border="2"> <tr> <td align="center"><b>Gifts</b></td> <td align="center"><b>Purchase Gifts</b></td> </tr>'; while ($row = mysql_fetch_array($myData)) { echo "<tr>"; echo "<td>" . $row['Gift'] . "</td>"; echo ' <td align="left"><input type="text" size="25" name="purchased_gift" value="'.$row['purchased_gift'].'"></td> </tr>'; echo "<tr>"; echo "<td>" . $row['Gift2'] . "</td>"; echo ' <td align="left"><input type="text" size="25" name="purchased_gift2" value="'.$row['purchased_gift2'].'"></td> </tr>'; } echo '</table>'; echo '<br /><div align="center"><input type="submit" name="Submit" value="Update Gift Registry" /></div> <input type="hidden" name="submitbtn" value="submitbtn" />'; echo '</form>'; } else { die(mysql_error()); } } mysql_close($connect); ?> Quote Link to comment https://forums.phpfreaks.com/topic/271311-stuck-on-a-two-form-processing-page-which-processes-the-first-form-but-not-the-second/#findComment-1396029 Share on other sites More sharing options...
jlindsey Posted November 28, 2012 Author Share Posted November 28, 2012 Never Mind, figured it out. Quote Link to comment https://forums.phpfreaks.com/topic/271311-stuck-on-a-two-form-processing-page-which-processes-the-first-form-but-not-the-second/#findComment-1396037 Share on other sites More sharing options...
jlindsey Posted November 28, 2012 Author Share Posted November 28, 2012 $UpdateQuery = "UPDATE users SET purchased_gift=$_POST[purchased_gift]' WHERE userid='$searchTerm'"; Forgot a ' in front of =$_POST[ Quote Link to comment https://forums.phpfreaks.com/topic/271311-stuck-on-a-two-form-processing-page-which-processes-the-first-form-but-not-the-second/#findComment-1396038 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.