Jump to content

Problem with $_post and $query not working


genista

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?

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.