Jump to content

Checkbox update


genista

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
Link to comment
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]
Link to comment
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]

Link to comment
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.