Jump to content

Losing my Variable


Go to solution Solved by CroNiX,

Recommended Posts

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! :suicide:

Link to comment
https://forums.phpfreaks.com/topic/295136-losing-my-variable/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507699
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507703
Share on other sites

 

} 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

Link to comment
https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507707
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507713
Share on other sites

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.)

Link to comment
https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507714
Share on other sites

  • Solution

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 by CroNiX
  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507715
Share on other sites

 

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...

Link to comment
https://forums.phpfreaks.com/topic/295136-losing-my-variable/#findComment-1507718
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.