Page 1 of 1

isset not working on checkboxes

Posted: Wed Sep 13, 2006 9:04 am
by genista
Hi all,


I have an update users form, the simple problem I am having is setting the checkboxes as variables and then getting them into my query. I really cannot see from my code why this should not be already happening:

Code: Select all

$id = $_SESSION['username'];
$query = "select * from suppliers where username='$id'";

    //now we pass the query to the database 
	$result=mysql_query($query, $link) or die("MySQL query $query failed.  Error if any: ".mysql_error());

    //get the first (and only) row from the result 
    $row = mysql_fetch_array($result, MYSQL_ASSOC); 

$first_name=$row['first_name'];
	$last_name=$row['last_name'];
    $address_line1=$row['address_line1']; 
    $address_line2=$row['address_line2']; 
$test=$row['test'];
In this part I have retrieved the results from the database, now I want the users update to be sent back:

Code: Select all

if(isset($_POST["submit"])){ 
  $first_name=$_POST['first_name'];
	$last_name=$_POST['last_name']; 
    $address_line1=$_POST['address_line1']; 
    $address_line2=$_POST['address_line2']; 
 if (isset($_POST['test']) != "true"){
   $test = "null";
}

 $query = "UPDATE suppliers SET `first_name`='$first_name', 
	`last_name`='$last_name', 
	`password`='$password', 
	`address_line1`='$address_line1', 
	`address_line2`='$address_line2', 
                `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.'; 
    }           
 

?>

By echoing the query all the values for the checkboxes are null.

Any help would be much appreciated.


Thanks,

G

Posted: Wed Sep 13, 2006 9:27 am
by GM
Try doing a print_r($_POST) to your screen to see what values you are getting in your post array.

Also, you've got some "strange" code here - it probably isn't doing what you expect it to:

Code: Select all

if (isset($_POST['test']) != "true"){ 
   $test = "null"; 
}
isset() returns boolean true or false, which you are then comparing to a string "true". In this case, the string "true" is converted to a boolean true, since it is not empty.

Thus, you are saying if $_POST['test'] is set, don't set $test = "null". If $_POST['test'] is not set, then set $test = "null"

Maybe you were trying to do:

Code: Select all

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

Posted: Wed Sep 13, 2006 10:14 am
by genista
Ok, I have made the change you suggested and print the POST array, it seems that it sets test as: [test] => true, however in the update query which I have echo'd test='null.' Would it have something to do with the '>'?


Thanks so far.

Posted: Wed Sep 13, 2006 10:38 am
by GM
no, the => construct is php's way of showing the key/value pairs of an array: key => value

Following your code, if $_POST['test'] is not "true" then set $test = 'null'...

Since you can see that $_POST['test'] is "true", then the code should not pass the line that sets $test = 'null'...

Is $test being set to 'null' somewhere else in the code? Is there a difference in the case?

do something like this as a quick and dirty debug:

Code: Select all

if($_POST['test'] != "true") { 
   echo 'Entered if: $test = '.$test;
   echo 'Entered if: $_POST[test] = '.$_POST['test'];
   $test = "null"; 
}
EDIT: changed code sample slightly.

Posted: Wed Sep 13, 2006 11:01 am
by genista
Sorry just noticed something, when I first load the page, I get an undefiedn index on the code you gave me, when I tick the box I then get the test=> true.

Code: Select all

if($_POST['test'] != "true") { 
   $test = "null"; 
}
Now its getting confusing...

Posted: Wed Sep 13, 2006 11:10 am
by genista
Should I be closing the brackets on non checkbox related fields like so:
Instead of:

Code: Select all

if(isset($_POST["submit"])){ 
  $first_name=$_POST['first_name']; 
        $last_name=$_POST['last_name']; 
    $address_line1=$_POST['address_line1']; 
    $address_line2=$_POST['address_line2']; 
if($_POST['test'] != "true") { 
   $test = "null"; 
}
Change it to:

Code: Select all

if(isset($_POST["submit"])){ 
  $first_name=$_POST['first_name']; 
        $last_name=$_POST['last_name']; 
    $address_line1=$_POST['address_line1']; 
    $address_line2=$_POST['address_line2'];[b] }[/b]
if($_POST['test'] != "true") { 
   $test = "null"; 
}
Or am I on the wrong track?

Posted: Wed Sep 13, 2006 11:38 am
by GM
OK... let's take a deep breath :)

If you close the brace, then your update query will run even if the form is not being submitted. I'm assuming that you'd want to avoid this...

The basic structure needs to be:

Code: Select all

<?php

// do all queries to extract data to use later on in the form fields
...
...

// check whether the form is being submitted
if(isset($_POST['submit'])) {
    // then the form is being submitted
    // do everything you need to do to save the data within this if statement

    $query = "UPDATE blah blah blah"...

    //die or redirect here:
    die('Profile Updated');
}

// if the execution reaches this point, then the form is not being submitted,
// display the form

?>
<form name="blah"...
... 
...
</body>
</html>
I'm on my way home now, so can't do anymore tonight. Some kind person will pick up the baton, hopefully :)