Jump to content


Photo

Checkbox update


  • Please log in to reply
5 replies to this topic

#1 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 16 August 2006 - 12:23 PM

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:

$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'";


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

Thanks,


G

#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 16 August 2006 - 12:33 PM

use checkboxes like so:

<?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";
    }

}

?>


#3 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 17 August 2006 - 09:21 AM

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:

<?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(); 



?> 



#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 17 August 2006 - 01:26 PM

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?

#5 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 17 August 2006 - 01:34 PM

Thats exactly what I have done, problem solved!

#6 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 17 August 2006 - 01:36 PM

Glad to be of service. :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users