Icewolf Posted March 28, 2013 Share Posted March 28, 2013 (edited) Hi I am trying to create a page that display the information from the database. But also there is a place to update it. I can get it to display correctly but I am having problems with the update. It keeps failing. </HTML> <body> <Form action="Dropdown_new.php" method="POST" target="showhere"> <select name="Member_ID"> <option value="">Select one…</option> <option value="Icewolf">Icewolf</option> <option value="Travisr826">Travisr826</option> <option value="josehasthestick">josehasthestick</option> <option value="Hhn87">Hhn87</option> <option value="Twisted31007">Twisted31007</option> <option value="Midget_chunkin">Midget_chunkin</option> <option value="cashstro718">cashstro718</option> <option value="mpnak313">mpnak313</option> <option value="Wisconsin24">Wisconsin24</option> <option value="usmc57-2">usmc57-2</option> <option value="Derbin11">Derbin11</option> <input type="submit" value="filter"> </select> </form> <iframe width="800" height="100" name="showhere" marginheight="0" marginwidth="0" frameborder="0" scrolling="no"> </iframe> <Form method ="POST" name="update" action="Update.php" /> Bank: <input type="text" name="bank" /> Reward 1: <input type="text" name="reward1" /> Reward 2: <input type="text" name="reward2" /> Reward 3: <input type="text" name="reward3" /> <input type="submit" name="submit" value="Update" /> </form> </body> </html> Here is the PHP <?php $username = "UserName"; $password = "Password"; $hostname = "localhost"; //connection to the database $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); echo "<font face=tahoma color=#ff000><b>Connected to MySQL</b></font><br><br>"; //select a database to work with $selected = mysql_select_db("pdogclan_points",$dbhandle) or die("Did this change"); // Formulate Query if (isset($_POST["Member_ID"])){ $memid = mysql_real_escape_string($_POST["Member_ID"]); $bank = $_POST['bank']; $reward1 = $_POST['reward1']; $reward2 = $_POST['reward2']; $reward3 = $_POST['reward3']; $query = "UPDATE Points_Rewards Set Bank = '$bank', Reward_1 = '$reward1', Reward_2 = '$reard2', Reward_3 = '$reward3' WHERE Member_ID = '$memid'" or die("Could Not Formulate the Query"); } if(mysql_query($query)){ echo "updated";} else{ echo "fail";} ?> I am getting through all the code but it hitting the last echo. I need to pull the ID from the top form. I don't know if this is correct way to do this. Thanks Edited March 28, 2013 by Icewolf Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/ Share on other sites More sharing options...
Psycho Posted March 28, 2013 Share Posted March 28, 2013 Do you realize that your update query is hard coded for specific values and does not use the values passed in the POST data? So, every time you run that page you will have the same value int he database. So, you probably aren't seeing any changes because of that. Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1421550 Share on other sites More sharing options...
Icewolf Posted March 28, 2013 Author Share Posted March 28, 2013 Yes I did after I posted it. I hard coded the data to see if I can get it to work but that didn't even work. I am not sure what is causing it to fail. It is running through everythng and gets to the end and fails. Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1421707 Share on other sites More sharing options...
akphidelt2007 Posted March 28, 2013 Share Posted March 28, 2013 (edited) You are putting this in the wrong spot... or die("Could Not Formulate the Query"); //try this and see what it tells you $query = "UPDATE Points_Rewards Set Bank = '$bank', Reward_1 = '$reward1', Reward_2 = '$reard2', Reward_3 = '$reward3' WHERE Member_ID = '$memid'"; $result = mysql_query($query) or die(mysql_error()); Edited March 28, 2013 by akphidelt2007 Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1421715 Share on other sites More sharing options...
Icewolf Posted March 29, 2013 Author Share Posted March 29, 2013 I did try that but I am still getting a fail. I am thinking it is getting here and failing. if(mysql_query($query)){ echo "updated";} else{ echo "fail";} Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1421716 Share on other sites More sharing options...
akphidelt2007 Posted March 29, 2013 Share Posted March 29, 2013 Most like isset($_POST['Member_ID']) isn't getting set. echo $query right before the if(mysql_query($query)){} and see what it says. Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1421718 Share on other sites More sharing options...
mac_gyver Posted March 29, 2013 Share Posted March 29, 2013 your form page has two separate forms. only the first one has a Member_ID field. the Member_ID isn't submitted with the second form. you need one form that has all the fields in it. all your form processing code should be inside the conditional if(){} statement - if(isset($_POST["Member_ID"])){ all the form processing code needs to be in here } you currently have the mysql_query() statement outside of and after the conditional statement. Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1421725 Share on other sites More sharing options...
Icewolf Posted March 29, 2013 Author Share Posted March 29, 2013 Most like isset($_POST['Member_ID']) isn't getting set. echo $query right before the if(mysql_query($query)){} and see what it says. You are correct. It is not pulling the member_id. How do I get to the ID if it is on another form? Do I need to create another drop down in this form? Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1421727 Share on other sites More sharing options...
akphidelt2007 Posted March 29, 2013 Share Posted March 29, 2013 You are correct. It is not pulling the member_id. How do I get to the ID if it is on another form? Do I need to create another drop down in this form? Why do you have two forms? Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1421730 Share on other sites More sharing options...
Icewolf Posted March 29, 2013 Author Share Posted March 29, 2013 (edited) your form page has two separate forms. only the first one has a Member_ID field. the Member_ID isn't submitted with the second form. you need one form that has all the fields in it. all your form processing code should be inside the conditional if(){} statement - if(isset($_POST["Member_ID"])){ all the form processing code needs to be in here } you currently have the mysql_query() statement outside of and after the conditional statement. Thanks Mac I was wondering this but how do use two different buttons on here to work. One is to pull the information the other is to update the information. AK I wasn't sure how to get the two buttons to work on one form. Edited March 29, 2013 by Icewolf Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1421736 Share on other sites More sharing options...
akphidelt2007 Posted March 29, 2013 Share Posted March 29, 2013 (edited) Thanks Mac I was wondering this but how do use two different buttons on here to work. One is to pull the information the other is to update the information. AK I wasn't sure how to get the two buttons to work on one form. You can have multiple submit buttons in one form. Just got to give them a different name or value and check which one was pushed. Edited March 29, 2013 by akphidelt2007 Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1421749 Share on other sites More sharing options...
Barand Posted March 29, 2013 Share Posted March 29, 2013 (edited) Easiest way is give the submit buttons the same name, say "btnSubmit", but with different values. Then check the value of $_POST['btnSubmit'] to see which was clicked. If the user presses return button to submit the form then the result depends on the user's browser - eg Firefox sends no value for btnSubmit, IE sends the first value on the form Edited March 29, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1421786 Share on other sites More sharing options...
Icewolf Posted March 31, 2013 Author Share Posted March 31, 2013 Okay I think I understand what you all are saying but I don't know if I did it right. I can get the filter button to work but not the submit. HTML <!DOCTYPE HTML> <html> <head> <title>Icewolf is the Man!!</title> </head> <body> <Form action="Dropdown_new.php" method="POST" target="showhere"> <select name="Member_ID"> <option value="">Select one…</option> <option value="Icewolf">Icewolf</option> <option value="Travisr826">Travisr826</option> <option value="josehasthestick">josehasthestick</option> <option value="Hhn87">Hhn87</option> <option value="Twisted31007">Twisted31007</option> <option value="Midget_chunkin">Midget_chunkin</option> <option value="cashstro718">cashstro718</option> <option value="mpnak313">mpnak313</option> <option value="Wisconsin24">Wisconsin24</option> <option value="usmc57-2">usmc57-2</option> <option value="Derbin11">Derbin11</option> <input type="submit" name="filter" value="filter"> </select> Bank: <input type="text" name="bank" /> Reward 1: <input type="text" name="reward1" /> Reward 2: <input type="text" name="reward2" /> Reward 3: <input type="text" name="reward3" /> <input type="submit" name="submit" value="Update" /> </form> <iframe width="800" height="100" name="showhere" marginheight="0" marginwidth="0" frameborder="0" scrolling="no"> </iframe> </body> </html> PHP <?php $username = "Userid"; $password = "Password"; $hostname = "localhost"; //connection to the database $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); echo "<font face=tahoma color=#ff000><b>Connected to MySQL</b></font><br><br>"; //select a database to work with $selected = mysql_select_db("pdogclan_points",$dbhandle) or die("Did this change"); // Formulate Query if (isset($_POST['filter'])){ $memid = mysql_real_escape_string($_POST["Member_ID"]); $query = sprintf("SELECT Member_ID, Bank, Reward_1, Reward_2, Reward_3 FROM Points_Rewards WHERE Member_ID = '$memid'") or die("Could Not Formulate the Query"); } //execute the SQL query and return records $result = mysql_query($query); // Check result // This shows the actual query sent to MySQL, and the error. Useful for debugging. //if (!$result) { // $message = 'Invalid query: ' . mysql_error() . "\n"; // $message .= 'Whole query: ' . $query; // die($message); //} //fetch tha data from the database while ($row = mysql_fetch_array($result)) echo "<table width=750 cellspacing=2 cellpadding=2 border=2> <tr> <td bgcolor=#000000 width=150><font face=tahoma color=white>ID: {$row['Member_ID']}</font></td>". "<td width=150><font face=tahoma>Bank: {$row['Bank']}</td>". "<td width=150><font face=tahoma>Reward 1: {$row['Reward_1']}</td>". "<td width=150><font face=tahoma>Reward 2: {$row['Reward_2']}</td> ". "<td width=150><font face=tahoma>Reward 3: {$row['Reward_3']}</td> </tr> </table><br></font>";//display the results // Formulate Update Query Else if (isset($_POST["submit"])){ $memid = mysql_real_escape_string($_POST["Member_ID"]); $bank = $_POST['bank']; $reward1 = $_POST['reward1']; $reward2 = $_POST['reward2']; $reward3 = $_POST['reward3']; } $query = "UPDATE Points_Rewards Set Bank = '$bank', Reward_1 = '$reward1', Reward_2 = '$reward2', Reward_3 = '$reward3' WHERE Member_ID = '$memid'"; $result = mysql_query($query) or die(mysql_error()); //echo $query; //if(mysql_query($query)){ //echo "updated";} //echo "fail";} //close the connection mysql_close($dbhandle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1422036 Share on other sites More sharing options...
Barand Posted March 31, 2013 Share Posted March 31, 2013 That's because both buttons are always posted. If you give them the same name only one is posted - test for its value Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1422054 Share on other sites More sharing options...
Icewolf Posted April 1, 2013 Author Share Posted April 1, 2013 What do you mean if you give them the same name? I thought if I use Name="" defines the button. The filter button works just fine. Then when I hit the Update button I can see the values are coming over I am just getting an error message "Invalid query: Query was empty Whole query:" Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1422342 Share on other sites More sharing options...
Barand Posted April 1, 2013 Share Posted April 1, 2013 What do you mean if you give them the same name? I meant exactly that EG <?php if (isset($_POST['btnSubmit'])) { if ($_POST['btnSubmit']=='Filter') { echo "Filter button clicked"; } else { echo "Update button clicked"; } echo '<br /><hr /><br />'; } ?> <form method="post"> <input type="submit" name="btnSubmit" value="Update" /> <br /> <input type="submit" name="btnSubmit" value="Filter" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1422348 Share on other sites More sharing options...
Icewolf Posted April 3, 2013 Author Share Posted April 3, 2013 (edited) Here is what is interesting I can get the filter to work now problem when I have both buttons here. I am getting an error "Invalid query: Query was empty Whole query:". and I notice is that it updates the database to 0 for that id. I know the query works because I removed the filter piece and it works fine. Edited April 3, 2013 by Icewolf Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1422557 Share on other sites More sharing options...
Solution Icewolf Posted April 3, 2013 Author Solution Share Posted April 3, 2013 I got it to work I had to remove the isset. Once I did that it started to work. Quote Link to comment https://forums.phpfreaks.com/topic/276247-update-query/#findComment-1422558 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.