Problem on query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Problem on query

Post by genista »

Hi,

I have a page where a user updates their details and it includes a load of checkboxes, the results of this page are passed to another to do error checking and pass it off to the database.

I have a problem with the query updating the database (I think), I keep getting undefined variables on the query for the checkboxes, I have used isset but still this problem..

Code: Select all

$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['test1'])) {
    // you know that test1 was checked
} else {
    // you know that test1 was either not checked or never existed
} 

$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', test1='$test1' where username = "$username'";
Any ideas?

Thanks,


G
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Checkboxes pass as either 'On' or empty. So if there is nothing checked, you cannot reference the checkbox directly.

As a general rule, I usually check isset() before referencing the var to make sure that it is initialized.
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

Ok, if I post the code from the form that passes the results to the page posted above maybe that can shed some light on this for me as I am not sure I understand:

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); 
    

$username=$row['username']; 
    $password=$row['password'];
    $first_name=$row['first_name'];
	$last_name=$row['last_name'];
    $address_line1=$row['address_line1']; 
    $address_line2=$row['address_line2']; 
    $town=$row['town'];
    $county=$row['county'];
    $postcode=$row['postcode'];
    $daytime_phone=$row['daytime_phone'];
    $mobile_phone=$row['mobile_phone'];
    $email_address=$row['email_address'];
    $test1 = $row['test1'];

below this we have the html.
Should I be modifyin ghtis code or focussing on the results page as in my first post?

Thanks,

G
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

From the code in your first code, make the changes noted below:

Code: Select all

$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'];

    /*  Change this :
    if (isset($_POST['test1'])) {
    // you know that test1 was checked
} else {
    // you know that test1 was either not checked or never existed
} */

//  To this:

  if ($_POST['test1'] == "on")
    $test1 = "true";  //  I don't know what $test1 is supposed to be, change as needed.
  else
    $test1 = "false";

/* You can do this if you really want to
$test1 = $_POST['test1']=="on"?"true":"false";
*/

$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', test1='$test1' where username = "$username'";
The problem I saw with this code is that you didn't give $test1 a value, so it'll always come up as NULL in the database when you insert/update the test1 field.

I don't know if using isset($_POST['test1']) will work correctly to set the $test1 variable, but I do know that using $_POST['test1'] == "on" will work.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If the var setting to $_POST['something'] is not inside an isset() check you are going to get a heap load of undefined index notices if the page is loaded without being posted. FYI.
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

If you make sure the form is posted before checking the individual form elements, it's not an issue.
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

Ok I have used the code you described, but now instead of undefined variable I get an undefined index:

Code: Select all

if ($_POST['test1'] == "on")
    $test1 = "true";  //  I don't know what $test1 is supposed to be, change as needed.
  else
    $test1 = "false";
I do not understand your last posts, so am unsure how to get this as a defined index...

G
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

genista wrote:Ok I have used the code you described, but now instead of undefined variable I get an undefined index
Everah wrote:If the var setting to $_POST['something'] is not inside an isset() check you are going to get a heap load of undefined index notices if the page is loaded without being posted. FYI.
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

I get it and it works...

Thanks all!
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

genista wrote:Ok I have used the code you described, but now instead of undefined variable I get an undefined index:

Code: Select all

if ($_POST['test1'] == "on")
    $test1 = "true";  //  I don't know what $test1 is supposed to be, change as needed.
  else
    $test1 = "false";
I do not understand your last posts, so am unsure how to get this as a defined index...

G
Change it to this:

Code: Select all

if (isset($_POST['test1']) && ($_POST['test1'] == "on"))
    $test1 = "true";  //  I don't know what $test1 is supposed to be, change as needed.
  else
    $test1 = "false";
Post Reply