Jump to content

Archived

This topic is now archived and is closed to further replies.

genista

Problem with $_post and $query not working

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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..

[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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.