I am attempting to code a nested if statement in PHP to check if a user has filled out all of the address fields on a HTML page. The if statement checks if the $addressLine has been filled out first, if not it should just ignore the rest of the address variables (these being: $townCity $postcode and $country) and move onto the next part of the code. If $addressLine has been filled out then it saves it to an array and then querys the other variables seeing if they are empty then it will output the user to fill them in, or if it is already filled in then it will be saved to an array.
The problem is, when I dont complete any of the address variables the code still requests the use to fill out the field.
As well as that problem, when echoing 'please fill out...' they are all on one line and not spaced out, is there a way to have the echos on separate lines ie in HTML <hr>, is there something like that I can add to the php echo?
Thank you very much for your time and any help :)
<?php
session_start();//creating session
$user=$_SESSION['firstName'];
if (isset($_POST['submit'])){
$connectDB = mysqli_connect("localhost","root","") or die("cant connect"); //proving the database connection details and saving it as a variable
mysqli_select_db($connectDB, "registration"); //table name
// BIRTHDAY
$updateDBvalues=array(); //creating array to be used in query
$updateArray=array(); //creating array
$dobday=$_POST['dobday']; //importing variables
//ADDRESS
$addressLine=$_POST['addressLine'];//importing variables
$townCity=$_POST['townCity'];//importing variables
$postcode=$_POST['postcode'];//importing variables
$country=$_POST['country'];//importing variables
//NUMBER
$mobNumber=$_POST['mobNumber'];//importing variables
//NUMBER BELOW
if(!empty($mobNumber))
$updateArray[]="mobNumber='".$mobNumber."'";
//ADDRESS BELOW
if (!empty($addressLine))//if the variables is NOT empty
$updateArray[]="addressLine='".$addressLine."'";{
if (empty($townCity)){//if the variables is empty
echo "Please fill out the Town/City field";
}else{
$updateArray[]="townCity='".$townCity."'";//add the variable to the array
}
if(empty($postcode)){//if the variables is empty
echo "Please fill out the Postcode field";
}else{
$updateArray[]="postcode='".$postcode."'";//add the variable to the array
}
if(empty($country)){//if the variables is empty
echo "Please fill out the Country field";
}else{
$updateArray[]="country='".$country."'";//add the variable to the array
}
}
// if(!empty($addressLine))//if the variables is NOT empty
// $updateArray[]="addressLine='".$addressLine."'";//add the variable to the array
// if(!empty($townCity))//if the variables is NOT empty
// $updateArray[]="townCity='".$townCity."'";//add the variable to the array
// if(!empty($postcode))//if the variables is NOT empty
// $updateArray[]="postcode='".$postcode."'";//add the variable to the array
// if(!empty($country))//if the variables is NOT empty
// $updateArray[]="country='".$country."'";//add the variable to the array
//BIRTHDAY BELOW
if(!empty($dobday))//if the variables is NOT empty
$updateArray[]="dobday='".$dobday."'";//add the variable to the array
if(!empty($dobmonth))//if the variables is NOT empty
$updateArray[]="dobmonth='".$dobmonth."'";//add the variable to the array
if(!empty($dobyear))//if the variables is NOT empty
$updateArray[]="dobyear='".$dobyear."'";//add the variable to the array
$updateDBvalues=$updateArray;
$updateDBvalues_imploded=implode(',',$updateDBvalues); // takes the array and makes one string
if(!empty($updateDBvalues)){ //if the array is NOT empty
$query="UPDATE users SET $updateDBvalues_imploded WHERE firstName='$user'";//sql query to save entries into table
$connQuery=mysqli_query($connectDB,$query);//querys database
die("Succesfully updated, return to <a href='accountPage.php'>Accounts page</a>");//successful query, provides link for user to return to account page
}else{
die ("Unable to save new infomation to the database, please try again. <a href='accountPage.php'>Accounts page</a>");//kills program, outputting that the query was unsuccessful, provides link for user to return to account page
}
}
?>
Aucun commentaire:
Enregistrer un commentaire