Page 1 of 1
Problem on query
Posted: Fri Aug 18, 2006 4:02 pm
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
Posted: Fri Aug 18, 2006 4:10 pm
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.
Posted: Sat Aug 19, 2006 5:33 am
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
Posted: Sat Aug 19, 2006 6:17 am
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.
Posted: Sat Aug 19, 2006 10:26 am
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.
Posted: Sat Aug 19, 2006 12:07 pm
by blackbeard
If you make sure the form is posted before checking the individual form elements, it's not an issue.
Posted: Sat Aug 19, 2006 1:48 pm
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
Posted: Sat Aug 19, 2006 1:51 pm
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.
Posted: Sat Aug 19, 2006 2:45 pm
by genista
I get it and it works...
Thanks all!
Posted: Sat Aug 19, 2006 2:46 pm
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";