Jump to content


Photo

Problem with $_post and $query not working


  • Please log in to reply
6 replies to this topic

#1 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 13 September 2006 - 10:47 PM

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:

 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.'; 
    }           
 }

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:

 if (isset($_POST['test2']) != "true"){
    $test2 = "null";
}

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?



#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 13 September 2006 - 11:00 PM

$_POST["submit"]


Is that correct?  Double quotes?

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 13 September 2006 - 11:03 PM

I have changed it to single quotes and it hasn't made a difference....

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 13 September 2006 - 11:29 PM

Surely the first
if (isset($_POST['test']))
should be
if (!isset($_POST['test']))
or am I reading that wrong.  If they tick the box, the value should be null?

Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 Destruction

Destruction
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 13 September 2006 - 11:51 PM

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

#6 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 14 September 2006 - 06:19 AM

if (isset($_POST['test'])) {
    $test = "null";
}

if you named any of you're form fields test in your HTML page
this will return true
what you need to do is compare $_POST['test'] to a value you gave it in the form

eg..

HTML
<input type='checkbox' name='test' value='1' />
the default value is NULL...so $_POST['test'] will always be isset if they submitted
PHP
if($_POST['test'] == 1)
   //The checkbox is checked

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#7 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 14 September 2006 - 06:41 PM

Ok, maybe this has something to do with it, here is the code for the html part of all of this:

<p>test:</td><td><input type="checkbox" name="test" value="true" <?php if($test == "true"){echo "checked=\"checked\"";}?> />
</p>

Hope htis helps!

G




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users