Jump to content

Archived

This topic is now archived and is closed to further replies.

genista

Checkbox update

Recommended Posts

Hi all,

I am trying to ensure that a user can update their details thoruhg a form. the problem is that when the checkboxes are not checked I get an undefined index error for them based of the update query as detailed below:

[code=php:0]
$strPassword = isset($_POST['password']) ? $_POST['password'] : "";
$strfirst_name = isset($_POST['first_name']) ? $_POST['first_name'] : "";
$strlast_name = isset($_POST['last_name']) ? $_POST['last_name'] : "";
$straddress_line1 = isset($_POST['address_line1']) ? $_POST['address_line1'] : "";
$straddress_line2 = isset($_POST['address_line2']) ? $_POST['address_line2'] : "";
$strtown = isset($_POST['town']) ? $_POST['town'] : ""; 
$strcounty = isset($_POST['county']) ? $_POST['county'] : "";
$strpostcode = isset($_POST['postcode']) ? $_POST['postcode'] : "";
$strdaytime_phone = isset($_POST['daytime_phone']) ? $_POST['daytime_phone'] : "";
$strmobile_phone = isset($_POST['mobile_phone']) ? $_POST['mobile_phone'] : "";
$stremail_address = isset($_POST['email_address']) ? $_POST['email_address'] : "";
                          $strinsurance = isset($_POST['insurance']) ? $_POST['insurance'] : "";
$strterms = isset($_POST['terms']) ? $_POST['terms'] : "";


$query = "UPDATE suppliers SET first_name='$first_name', last_name='$last_name', password='$password', address_line1='$address_line1', address_line2='$address_line2', town='$town' , county='$county', postcode='$postcode', daytime_phone='$daytime_phone', mobile_phone='$mobile_phone', email_address='$email_address', insurance='$insurance', terms='$terms' WHERE username='$username'";

[/code]

It is this query that is causing me issues, any ideas?

Thanks,


G

Share this post


Link to post
Share on other sites
use checkboxes like so:

[code]<?php

if (empty($_POST['checkbox']) {

?>
<form action="" method="post">
  <input type="checkbox" name="checkbox[]" value="check1" />
  <input type="checkbox" name="checkbox[]" value="check2" />
  <input type="checkbox" name="checkbox[]" value="check3" />
  <input type="checkbox" name="checkbox[]" value="check4" />
  <input type="checkbox" name="checkbox[]" value="check5" />
  <input type="submit" value="Submit" />
</form>
<?php

} else {

    foreach ($_POST['checkbox'] as $box) {
        echo "<p>Check box: {$box} was checked!</p>\n";
    }

}

?>[/code]

Share this post


Link to post
Share on other sites
Thanks for the response but this is not working, I need the code in a page that displays the success or failure of the update after the page where you fill in the html code.

So in the updateresults page I have taken the code like so, but it is not working:

[code=php:0]
<?php

if (!isset($_POST['submit']))


{
    if (isset($_POST['boxes']))
    {
        echo 'you checked ' . implode(',', $_POST['boxes']);
    }
    else
    {
        echo 'you did not check any boxes';
    }
}   
 
?>
<?php

    $query = "UPDATE suppliers SET first_name='$first_name', last_name='$last_name', password='$password', address_line1='$address_line1', address_line2='$address_line2', town='$town' , county='$county', postcode='$postcode', daytime_phone='$daytime_phone', mobile_phone='$mobile_phone', email_address='$email_address', checkbox1='$checkbox1', checkbox2='$checkbox2'";

        //now we pass the query to the database
       


       
    $result=mysql_query($query) or die("Could not insert data.".mysql_error());

    //$result represents the status of the operation true if it worked false if it failed
    if($result){
        print "Your details have been sucesfully updated. You can go back to the <a href=suppliers.php>index</a> page now.";
    }else{
        print "Could not update data.";
    }

    //finally close the connection
    mysql_close();



?>
[/code]

Share this post


Link to post
Share on other sites
I'm going to guess you are having problems with the insert and receiving a mysql error message?

Question (and hopefully you'll be able to figure out the problem from this..)

Where are your variables defined?

Share this post


Link to post
Share on other sites

×

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.