Just to create some context: I'm a PHP rookie. I'm creating an account system for an ecommerce website where an 'account' can be associated with 1 or many 'addresses'. I want the user to be able to add, update and delete addresses and I'm struggling with the latter two functions due to me being unable to figure out how to pass a specific variable through a while loop.
Here's what I mean:
while ($row = mysqli_fetch_array($result)) {
//address data variables
$addrId = $row['id'];
$accId = $row['account_id'];
$addrName = $row['full_name'];
$addr1 = $row['address1'];
$addr2 = $row['address2'];
$townCity = $row['towncity'];
$countyState = $row['countystate'];
$postZip = $row['postzip'];
$country = $row['country'];
$phone = $row['phone'];
//$count++;
//$_SESSION['address']=array();
$_SESSION['address'] = $addrId;
?>
<div class="addrWrapper">
<div class ="large-3 columns callout row">
<?php
echo $addrId."</br>";
echo $accId."</br>";
echo $addrName."</br>";
echo $addr1."</br>";
echo $addr2."</br>";
echo $townCity."</br>";
echo $countyState."</br>";
echo $postZip."</br>";
echo $country."</br>";
echo $phone."</br>";
//echo $count;
?>
<div class ="large-3 columns">
<a href="PHP/backend/account/addressDelete.php" class="button">Delete</a>
</div>
The above is the front-end, in reality I won't be displaying all of this information to the user, it's just for testing.
<?php
session_start();
//require_once('config.php');
$dbserver = "localhost";
$dbusername = "root";
$dbpassword = "";
$db = "TheNameHere";
$conn = new mysqli($dbserver, $dbusername, $dbpassword, $db);
//Check connection
if ($conn->connect_error){
die("connection failed".$conn->connect_error);
}
$address = $_SESSION['address'];
//$query1=mysqli_query($conn,"DELETE FROM useraddress WHERE id = $addrId");
echo $address;
//header("Location: ../../../index.php?page=address");
die();
?>
This is the script that will be used for deleting an address being debugged. Basically when the "delete" button next to one of the addresses is clicked, it will always pass the variable of the address with the largest 'id' (obviously, because there's one session and it gets updated each time the loop plays, always resulting in '$address' being the final address in the list).
I've kept coming back to this for a couple of days now, I've tried a lot of different solutions and I've came to the conclusion that I'm probably going to have to structure this very differently to how I have done.
Any pointers will be greatly appreciated.
PS. I don't want to pass the address 'id' through the URL due to security concerns, this passing of the variable needs to be hidden from the user.
$_SESSION['address'][$addrId] = $accIdor similair to that?