Jump to content

Recommended Posts

I am writing an application to CRUD user details. I kick off the page with a query to the user table - and use the data to populate various form elements in the page (and to check for duplicates). All form submissions submit to the same page.

 

Adding, deleting and modifying records work as expected - but the kicker is that the data *displayed* on the page does not update accordingly once a change is submitted - I have to revisit the page in order to see the relevant changes.

    try{
        $user_sql = "SELECT user_ID, user_firstname,user_surname,user_email,user_type FROM users";
        $user_stmt = $db->prepare($user_sql);
        $user_stmt->execute();
        $user_data = $user_stmt->fetchAll(PDO::FETCH_NAMED);
        $i++;
    }catch(PDOException $e){
        echo "Error: cannot retrieve user data from the data base";
    }

    /*
     * DELETE USER
     */
    if(isset($_POST['deleteUser'])){
    $_SESSION['deleteUser']=true;
    }
    if(isset($_POST['submitDeleteUserConfirm'])){
    //process query
        if($_POST['deleteUserConfirm']=='yes'){
            $deleteRange=implode(',',$_POST['deleteUser']);
            $deleteSql = 'DELETE FROM users WHERE ID_users IN (' . $deleteRange . ')';

            try{$q = $db->prepare($deleteSql);
            $q->execute();
             }
            catch(PDOException $e){
                echo '<p>System Error: '. $e->getMessage() .'</p>';
                }
            }
    //once confirmation has been processed:
    //remove session trigger to hide confirmation form
        if(isset($_SESSION['deleteUser'])){
            unset($_SESSION['deleteUser']);
        }
    }
    if(isset($_SESSION['deleteUser'])){
    ?>
        <fieldset class="radiobox">
            <legend>Confirm</legend>
            <div>
                <label for="deleteUserYes">Yes</label>
                <input type="radio" class="radio" name="deleteUserConfirm" id="deleteUserYes" value="yes">
            </div>
            <div>
                <label for="deleteUserNo">No</label>
                <input type="radio" class="radio" name="deleteUserConfirm" id="deleteUserNo" value="no" checked>
            </div>
        <input type="submit" name="submitDeleteUserConfirm" value="Confirm">
        </fieldset>
    <?php
     }
    //small function to output all the elements of an array as checkboxes
    $delStudentRollOpts = array(
        'key'=>'users',
        'sticky'=>true,
        'data'=>array(
            'values'=>$user_data,# USES DATA FROM START OF SCRIPT - NOT UPDATING ON PAGE REFRESH/FORM SUBMISSION
            'name'=>array('user_firstname','user_surname')
        ),
        'element_name'=>'delStudRoll_'.$n,
        'types'=>array('checkbox'=>array('title'=>' ',
                                     'name'=>'deleteUser',
                                     'index'=>'ID_users'
            ))
        );
    echo $GA_form->generateRoll($delStudentRollOpts);
    echo '<input type="submit" class="submit" name="submitDeleteUser_'.$n.'" value="Delete">';
    echo '</fieldset>';
    echo '</form>';

   

 

This is very much a work in progress, so it needs a lot of refactoring...

 

From the user's perspective:

 

1: enter new data

 

2: submit form

 

3: check db from MySQL console - new data inserted

 

4: no new data shown

 

5: visit page again - new data appears (refreshing will trigger an error trying to re-submit duplicate values)

 

Anyone have any ideas what's happening? At the moment I'm toying with the idea of redirecting back to the page with a header...

 

Doh! I see it now...I had assumed that because the page was refreshed, that it would send back the data from the host - but obviously that part of the page isn't executed on the host until after the update...

I feel like a spanner now!

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.