Page 1 of 1

Update Multiple Checkbox Array In Database

Posted: Tue Sep 02, 2008 3:02 pm
by admin2gd1
I have a form that I allow a user to update there properties details to my website that I am creating and everything shows whats in that property the user posted from before but for 2 sections does not retrieve the data the user had placed.

The form that allowed the user to post this section was serialized using this.

Code: Select all

if(isset($_POST['int_features'])){
        $int_features = serialize($_POST['int_features']);
    }else{
        $int_features = "";
    }
    if(isset($_POST['ext_features'])){
        $ext_features = serialize($_POST['ext_features']);
    }else{
        $ext_features = "";
    }
 
Now I am trying to do the opposite of that and allow the user to update the features the user want.

All I get is the list of the features the user can select to choose but does not have them ticked for the ones the user picked from before.
This is what the data looks like once the user has selected the features.

Code: Select all

a:7:{i:0;s:10:"Bread Oven";i:4;s:4:"Cave";i:5;s:7:"Cellier";i:10;s:7:"Cuisine";i:20;s:5:"Patio";i:23;s:10:"Pizza Oven";i:24;s:5:"Porch";}
Here is the code that I have to unserialize the information that needs correcting.

Code: Select all

<?php
$sql = mysql_query("SELECT * FROM ".TBL_INT_FEATURES." ORDER BY `int_feature` ASC");
$int_featureNo = 0;
while ($featuresentered = mysql_fetch_array($sql)){
    $intfeaturesvalue = $featuresentered['int_feature'];
    if(!empty($int_features)){ // if feature already selected show checked
        foreach($int_features as $featVal){
            if($intfeaturesvalue == $featVal){
                $checked = "checked";
            }
        }
    }
    echo "<input type='checkbox' name='int_features[".$int_featureNo."]' value='".$intfeaturesvalue."'> <b>".$intfeaturesvalue."</b><hr size=1 color='#efefef'>";
    $int_featureNo = $int_featureNo + 1;
    $checked = "";
}
?>
This is the same code for EXT FEATURES!

I really need this corrected other wise all it does is empty what was there already and put the word ARRAY instead.

So if any one can help me with this piece of code I would really appreciate it.

Thank you.

Re: Update Multiple Checkbox Array In Database

Posted: Tue Sep 02, 2008 3:27 pm
by andyhoneycutt
You're never actually assigning $checked to the checkbox.
Change:

Code: Select all

 
echo "<input type='checkbox' name='int_features[".$int_featureNo."]' value='".$intfeaturesvalue."'> <b>".$intfeaturesvalue."</b><hr size=1 color='#efefef'>";
 
To:

Code: Select all

 
echo "<input type='checkbox' name='int_features[".$int_featureNo."]' value='".$intfeaturesvalue."' $checked> <b>".$intfeaturesvalue."</b><hr size=1 color='#efefef'>";
 

Re: Update Multiple Checkbox Array In Database

Posted: Thu Sep 04, 2008 6:09 am
by admin2gd1
OK that bit worked great and the checked boxes that are selected already show they are checked. Now if I was to change any of those check boxes on the edit form and save, in the database it puts the word Array one in CAPS. Can I get somehelp on that and this function will be complete.

Thank you.

Re: Update Multiple Checkbox Array In Database

Posted: Thu Sep 04, 2008 2:49 pm
by andyhoneycutt
Post your code for your update query and I'd be glad to take a look.

Re: Update Multiple Checkbox Array In Database

Posted: Thu Sep 04, 2008 3:59 pm
by admin2gd1

Code: Select all

<?php
if($_GET['mode'] == 'update'){
    if(isset($_POST['username'])){
        $username = serialize($_POST['username']);
    }else{
        $username = "";
    }
    if(isset($_POST['int_features'])){
        $int_features = serialize($_POST['int_features']);
    }else{
        $int_features = "";
    }
    if(isset($_POST['ext_features'])){
        $ext_features = serialize($_POST['ext_features']);
    }else{
        $ext_features = "";
    }
    $id = $_GET['id'];
    ////// Edit Property to DB //////
    $lastedit  = date("Y:m:d H:i:s");
    $status = $_POST['status'];
    $sale_type = $_POST['sale_type'];
    $category = $_POST['category'];
    $name = addslashes($_POST['name']);
    $address = $_POST['address'];
    $village = $_POST['village'];
    $lng = $_POST['lng'];
    $lat = $_POST['lat'];
    $department = $_POST['department'];
    $telephone = $_POST['telephone'];
    $mobile = $_POST['mobile'];
    $email = $_POST['email'];
    $description_fr = addslashes($_POST['description_fr']);
    $description_en = addslashes($_POST['description_en']);
    $price = $_POST['price'];
    $bedrooms = $_POST['bedrooms'];
    $bathrooms = $_POST['bathrooms'];
    $view = $_POST['view'];
    $meters = $_POST['meters'];
    $terrain = $_POST['terrain'];
    $pool = $_POST['pool'];
    $garage = $_POST['garage'];
    $garage_size = $_POST['garage_size'];
    $front_garden = $_POST['front_garden'];
    $rear_garden = $_POST['rear_garden'];
    $int_features = $_POST['int_features']; 
    $ext_features = $_POST['ext_features']; 
    $fees = $_POST['fees'];
    $tax = $_POST['tax'];
    $sql = "UPDATE ".TBL_PROPERTIES." SET status = '$status', sale_type = '$sale_type', category ='$category', 
    name = '$name', address = '$address', village = '$village', lng = '$lng', lat = '$lat', 
    department = '$department', postcode = '$postcode', description_fr = '$description_fr', 
    description_en = '$description_en', price = '$price', price_change = '$pricechange', 
    bedrooms = '$bedrooms', bathrooms = '$bathrooms', view = '$view', meters = '$meters', 
    terrain = '$terrain', pool = '$pool', garage = '$garage', garage_size = '$garage_size', 
    front_garden = '$front_garden', rear_garden = '$rear_garden', int_features = '$int_features', 
    ext_features = '$ext_features', fees = '$fees', tax = '$tax', last_edit = '$lastedit', edit_mode = '0' WHERE property_id = '$id'";
    if(@mysql_query($sql)){
        $msg = "Your property <b>".$name."</b> has been updated! Click <a href='$SITEurl/listing.php?id=$id'>here</a> to view your property.";
    }else{
        $msg = "There was an error updating your property, please contact the administrators for help at " . ADMIN_EMAIL . " !";
    }
?>
 

Re: Update Multiple Checkbox Array In Database

Posted: Thu Sep 04, 2008 7:36 pm
by andyhoneycutt
The problem is that you actually are trying to post an array to the database. php will render that as the type of object when you try to echo the value directly. what you may want to do, if you want a comma delimited value that represents the array:

Code: Select all

$int_features = implode(',',$_POST['int_features']);
ETA: the implode function takes an array and combines it's values into a string, with the values separated by the first argument: the delimiter you wish to use.

Re: Update Multiple Checkbox Array In Database

Posted: Fri Sep 05, 2008 5:25 am
by admin2gd1
Sorry that is not what I need.

What you have given me does not serialize the data and insert it into the database like this:-

Code: Select all

a:7:{i:0;s:10:"Bread Oven";i:4;s:4:"Cave";i:5;s:7:"Cellier";i:10;s:7:"Cuisine";i:20;s:5:"Patio";i:23;s:10:"Pizza Oven";i:24;s:5:"Porch";}
What yours did was insert the date like this:-

Code: Select all

Bread Oven, Cave, Cellier, Cuisine
I need the data to be serialized for the rest of my function to work.

Re: Update Multiple Checkbox Array In Database

Posted: Fri Sep 05, 2008 5:31 am
by jayshields
Just a thought - you shouldn't ever need to store serialized data in a database. Serialization is for storing objects in text files.

If you really have to store an array in one table field (which shows a flaw in your database schema) it would use less space if you used comma seperated values.

Re: Update Multiple Checkbox Array In Database

Posted: Mon Sep 08, 2008 9:36 am
by admin2gd1
OK I got it to work, so thank you to all that helped me with your tips.