Landslyde Posted March 5, 2015 Share Posted March 5, 2015 I load a Selection box's options with names from my db. The <select> name = 'view'. When I select a name, it loads input boxes with the record by the selected name's ID in the table. So I then set the variable $id to that: $id = $_POST['view']; // From <select> option if($_POST['fetch']) { $stmt = $db->prepare('SELECT * FROM attendees WHERE memberid = :memberid AND attendeeid = :id'); $stmt->bindValue(':memberid', $memberid, PDO::PARAM_INT); $stmt->bindValue(':id', $id, PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetchAll(); etc etc The record pulls up fine. But after doing any editing, I submit it to UPDATE the record and it doesn't get updated. So I set an echo one line above my UPDATE statement: } elseif($_POST['update']) { echo 'Attendee ID is '.$id; // $id is gone at this point $stmt = $db->prepare('UPDATE attendees SET fname = :fname, etc, etc WHERE attendeeid = :id'); } only to find that the value of $id is missing. Does the fact that I bind $id to :id in my SELECT query cause $id to become empty after executing that query? The value of $id is held through the SELECT stmt but is lost when I get to the UPDATE stmt. Any ideas? This is driving me nuts! Quote Link to comment https://forums.phpfreaks.com/topic/295136-losing-my-variable/ Share on other sites More sharing options...
mac_gyver Posted March 5, 2015 Share Posted March 5, 2015 does the form that sets/submits $_POST['update'], also have a field named 'view', so that $_POST['view'] would have a value on that particular page request? Quote Link to comment https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507698 Share on other sites More sharing options...
Landslyde Posted March 5, 2015 Author Share Posted March 5, 2015 does the form that sets/submits $_POST['update'], also have a field named 'view', so that $_POST['view'] would have a value on that particular page request? No, the two submit buttons and the select box all have unique names. No conflict there that shows its face. The only quirky thing going on is $id losing its value as it goes from the SELECT to the UPDATE. Quote Link to comment https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507699 Share on other sites More sharing options...
mac_gyver Posted March 5, 2015 Share Posted March 5, 2015 web servers are stateless. each request for a page is completely separate from all other requests to the server. except for session variables, the only way any variable can exist is if it was supplied with a value on that particular request. if $_POST['view'] was set as the result of one form being submitted, it will only be present on that one page request. a different form submitting data will only supply the data corresponding to the fields in that form. Quote Link to comment https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507703 Share on other sites More sharing options...
Landslyde Posted March 5, 2015 Author Share Posted March 5, 2015 Have a look. All the code is on one page. http://paste.ofcode.org/h8gVHHCuTKMW4YJXqR53jV Quote Link to comment https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507705 Share on other sites More sharing options...
CroNiX Posted March 5, 2015 Share Posted March 5, 2015 } elseif($_POST['update']) { echo 'Attendee ID is '.$id; // $id is gone at this point $stmt = $db->prepare('UPDATE attendees SET fname = :fname, etc, etc WHERE attendeeid = :id'); } You don't bind the parameters or execute the query, at least you didn't show that part if you do. Very hard to troubleshoot incomplete code Quote Link to comment https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507707 Share on other sites More sharing options...
Landslyde Posted March 5, 2015 Author Share Posted March 5, 2015 I tried putting the PHP code here, but it goes every which way when I paste it. Please view the link I posted above. Quote Link to comment https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507708 Share on other sites More sharing options...
mac_gyver Posted March 5, 2015 Share Posted March 5, 2015 your select menu is not persistent (doesn't pre-select the existing value), so, on the update form submission, $_POST['view'] has the value from the first option - <option value="">Select</option> 1 Quote Link to comment https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507709 Share on other sites More sharing options...
Landslyde Posted March 5, 2015 Author Share Posted March 5, 2015 How do I retain the name I selected after clicking the "Fetch" button? Is there a way to do this? I ask because you're 110% correct with your answer. I wasn't paying attention to the fact it was reverting back to "Select". I manually set it and my update held. Thank you for that. But, again, is there a way to retain the selected name after clicking the "Fetch" button? Quote Link to comment https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507713 Share on other sites More sharing options...
mac_gyver Posted March 5, 2015 Share Posted March 5, 2015 you would output a selected='selected' attribute inside the correct <option ...> tag that corresponds to the previously submitted value, if any. however, by making the select menu available during the edit phase, you create a situation where a user can retrieve the data from one record and easy (fumble-fingers) submit/overwrite a different record. to prevent any possibility of accidental/intentional miss use, at the point of editing the actual data, you should store the id of the record being edited in a session variable, out of the hands of the person involved, and don't display the select menu (or display it for reference, but ignore the input value from it.) Quote Link to comment https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507714 Share on other sites More sharing options...
Solution CroNiX Posted March 5, 2015 Solution Share Posted March 5, 2015 (edited) something like: $selected = ''; $selected_value = (isset($_POST['view'])) ? $_POST['view'] : ''; foreach($result as $row){ if ($row[0] == $selected_value) { $selected = ' selected="selected"'; } echo "<option value='$row[0]'$selected>$row[2], $row[1]</option>"; } ?> </select> Edited March 5, 2015 by CroNiX 1 Quote Link to comment https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507715 Share on other sites More sharing options...
Landslyde Posted March 5, 2015 Author Share Posted March 5, 2015 Thanks for your input, mac_gyver, for opening my eyes to a select box that I saw change every time I used it Sometimes you really can't see the trees for the forest. Quote Link to comment https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507716 Share on other sites More sharing options...
Landslyde Posted March 5, 2015 Author Share Posted March 5, 2015 something like: $selected = ''; $selected_value = (isset($_POST['view'])) ? $_POST['view'] : ''; foreach($result as $row){ if ($row[0] == $selected_value) { $selected = ' selected="selected"'; } echo "<option value='$row[0]'$selected>$row[2], $row[1]</option>"; } ?> </select> Very much appreciated, CroNIX. I'm indebted to you and mac_gyver BIG time. Many thanks... Quote Link to comment https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507718 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.