Click to See Complete Forum and Search --> : Problem with Post variables


genista
09-15-2006, 02:42 PM
Hi All,

I have a script that allows a user to update their details, the problem I am having is getting the update query to .. update. Its all because of checkboxes. I have managed to print the $POST variables and the checkboxes are populating true, which is what I want, but then when I print the update query they are coming out as null and obviously not updating the database...



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

?>



So that is the update script, the html that a user fills in for a checkbox looks like this:



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

here is the html in the form:


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



I would really appreciate somehelp on this, thanks,

G

bokeh
09-15-2006, 04:42 PM
if(isset($_POST['test']) != "true")isset() returns a boolean. That means your string (to the right of the if statement) will also be converted to a boolean before comparison. If $_POST['test'] is set the boolean value for isset() will be true, otherwise it will be false. The string "true" evaluates to boolean true meaning if the variable is set your if clause will always read as follows if(true != true) // false

genista
09-15-2006, 05:00 PM
Sorry I am not quite with you, what should I be changing in the isset, true to null?

bokeh
09-15-2006, 05:02 PM
Do you know what a boolean is? Or a string? You need to understand that true and "true" are very different things.

genista
09-15-2006, 05:14 PM
Ok I think I am getting somewhere in the html and isset I have "true" but in the query I am using single quotes, am I on the right lines?

I have just tried to change the html from " to ' but still no joy..

LimpBagel
09-15-2006, 05:16 PM
TRUE/FALSE are ideas, on or off, yin/yang. "true" is a string of 4 characters, not the same as true/false or 1/0.

Try this: if(isset($_POST['test']))

isset() will return TRUE or FALSE to your if statement.

genista
09-15-2006, 05:28 PM
Ok I have tried that, I still have the post variable set, but still null in the query result..

bokeh
09-16-2006, 03:02 AM
Well you either need to use empty() instead of isset() or negate isset(). if(!isset($variable))