genista Posted September 13, 2006 Share Posted September 13, 2006 I have a form that updates details, it first echo's and then posts back. The issue I am having is with checkboxes not working in the query, but working in the $POST array, read on...Here is the code:[code=php:0] if(isset($_POST["submit"])){ $first_name=$_POST['first_name']; $last_name=$_POST['last_name']; $address_line1=$_POST['address_line1']; $address_line2=$_POST['address_line2']; $town=$_POST['town']; $county=$_POST['county']; $postcode=$_POST['postcode']; $daytime_phone=$_POST['daytime_phone']; $mobile_phone=$_POST['mobile_phone']; $email_address=$_POST['email_address'];if (isset($_POST['test'])) { $test = "null";} $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', `test`='$test'WHERE `username` = '". mysql_real_escape_string($_SESSION['username']). "' LIMIT 1"; $result = mysql_query($query, $link) or die('Update failed: ' . mysql_error()); echo $query; //print_r($query); mysql_info($link) ; if(mysql_affected_rows($link) == 0) { //$link next to affected rows and mysql query echo ''; } else { echo 'Your profile has been updated successfully, please click <a href=suppliers.php>here</a> for other options.'; } }[/code]If I try and submit the page without 'test' being checked I get an undefined variable error. I have echo'd the post query and when I tick a tick box the array shows test => true. However, on echoing the query it shows null and kills the query for everything below 'test'I also have some other checkboxes that look like this:[code=php:0] if (isset($_POST['test2']) != "true"){ $test2 = "null";}[/code]These do not give me an undefined variable error, but neither do they get to be true when checked in the query.What would be the solution here? Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted September 13, 2006 Share Posted September 13, 2006 [quote]$_POST["submit"][/quote]Is that correct? Double quotes?RegardsRich Quote Link to comment Share on other sites More sharing options...
genista Posted September 13, 2006 Author Share Posted September 13, 2006 I have changed it to single quotes and it hasn't made a difference.... Quote Link to comment Share on other sites More sharing options...
HuggieBear Posted September 13, 2006 Share Posted September 13, 2006 Surely the first [code=php:0]if (isset($_POST['test']))[/code] should be [code=php:0]if (!isset($_POST['test']))[/code] or am I reading that wrong. If they tick the box, the value should be null?Rich Quote Link to comment Share on other sites More sharing options...
Destruction Posted September 13, 2006 Share Posted September 13, 2006 Yes, it should be if(!$_POST['test']) or if(!isset($_POST['test'])), either would work. If it's not been posted, there's no $test variable because it's only defined inside the loop, yet you try to use it in the query regardless.Dest Quote Link to comment Share on other sites More sharing options...
Zane Posted September 14, 2006 Share Posted September 14, 2006 if (isset($_POST['test'])) { $test = "null";}if you named any of you're form fields test in your HTML pagethis will return truewhat you need to do is compare $_POST['test'] to a value you gave it in the formeg..[b]HTML[/b][code]<input type='checkbox' name='test' value='1' />[/code]the default value is NULL...so $_POST['test'] will always be is[color=red]set[/color] if they submitted[b]PHP[/b][code]if($_POST['test'] == 1) //The checkbox is checked[/code] Quote Link to comment Share on other sites More sharing options...
genista Posted September 14, 2006 Author Share Posted September 14, 2006 Ok, maybe this has something to do with it, here is the code for the html part of all of this:[code=php:0]<p>test:</td><td><input type="checkbox" name="test" value="true" <?php if($test == "true"){echo "checked=\"checked\"";}?> /></p>[/code]Hope htis helps!G Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.