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? Link to comment https://forums.phpfreaks.com/topic/20671-problem-with-_post-and-query-not-working/ 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 Link to comment https://forums.phpfreaks.com/topic/20671-problem-with-_post-and-query-not-working/#findComment-91420 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.... Link to comment https://forums.phpfreaks.com/topic/20671-problem-with-_post-and-query-not-working/#findComment-91421 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 Link to comment https://forums.phpfreaks.com/topic/20671-problem-with-_post-and-query-not-working/#findComment-91432 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 Link to comment https://forums.phpfreaks.com/topic/20671-problem-with-_post-and-query-not-working/#findComment-91448 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] Link to comment https://forums.phpfreaks.com/topic/20671-problem-with-_post-and-query-not-working/#findComment-91532 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 Link to comment https://forums.phpfreaks.com/topic/20671-problem-with-_post-and-query-not-working/#findComment-91884 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.