Jump to content

Recommended Posts

I have a page which updates the database when the user submits the data. This page has multiple inserts, some of which are empty. what is the best way to prevent clearing data? for example i have this query:

 

switch($method)
    {
        case 'personal':
        $sql = "INSERT INTO ".TBL_PREFIX."users_profile
                (p_real_name, 
                p_location,
                p_gender,
                p_dob_day,
                p_dob_month,
                p_dob_year,
                p_website,
                p_occupation)
                VALUES
                (:location,
                :gender,
                :day,
                :month,
                :year,
                :website,
                :occupation)";
        $sth = $db->prepare($sql);
        $sth->execute(array(
            ':location'        => $data['location'],
            ':gender'        => $data['gender'],
            ':day'            => $data['dob_day'],
            ':month'        => $data['dob_month'],
            ':year'            => $data['dob_year'],
            ':website'        => $data['website'],
            ':occupation'    => $data['occupation'],
            )
        );
        break;
    }

 

if the database column "occupation" contains information but the input is empty then it would replace the info in the database. So what is the best way to prevent this? i know i could use if(empty($data['occupation'])) and build an sql query on loads of if statements to check each input. Is that best?

 

NB. this is example code, most inputs will contain data pulled from the database. This is only relevant for purposely empty inputs like password and secret answers.

 

Link to comment
https://forums.phpfreaks.com/topic/242595-conditional-insert-query/
Share on other sites

that shows how tired i am, its supposed to be an update query.

 

$db = db_pdo::$instance;
    
    switch($method)
    {
        case 'personal':
        $sql = "UPDATE ".TBL_PREFIX."users_profile
                SET
                    p_real_name = :real_name,
                    p_location  = :location,
                    p_gender    = :gender,
                    p_dob_day   = :day,
                    p_dob_month = :month,
                    p_dob_year  = :year,
                    p_website   = :website,
                    p_occupation = :occupation";
                
        $sth = $db->prepare($sql);
        
        $sth->execute(array(
            ':real_name'    => $data['real_name'],
            ':location'        => $data['location'],
            ':gender'        => $data['gender'],
            ':day'            => $data['dob_day'],
            ':month'        => $data['dob_month'],
            ':year'            => $data['dob_year'],
            ':website'        => $data['website'],
            ':occupation'    => $data['occupation'],
            )
        );
        break;
    }

 

You'll have to determine if the variable is empty before hand. If it is empty, you have two options:

 

If you have the data in your script somewhere, you can replace the post value with the database value.

 

The other option is to simply make your script determine what values to set IE:

 

$update=array();
if (!empty($_POST['value'])) {
   $update[]="value = '{$_POST['value']}'";
}

if (count($update) > 0) {
   $updateString=implode(",", $update);
   $sql="UPDATE table SET {$updateString} WHERE VALUE = '{$value}'";
   // Rest of your mysql code.
}

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.