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!
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..
$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'";
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:
$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?
$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.
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 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.
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";