0

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.

5
  • Did you try $_SESSION['address'][$addrId] = $accId or similair to that? Commented Dec 16, 2016 at 11:31
  • My Suggestion is to pass the Id trough html to a php back-end page that will delete that address. That said better check for permission and do a Logical Delete, not a physical one. This way you can think of a multi-select delete with checkbox :) Commented Dec 16, 2016 at 11:32
  • $_SESSION['address'] = $addrId; in your loop will overwrite every time it loops over, so it will always be the last element in your array / results. Commented Dec 16, 2016 at 11:34
  • @Dainis Abols, yeah I tried messing with session arrays, but didn't get any further than I did using the standard 'session'. The loop always overwrites the session each time as Farkie said. Commented Dec 16, 2016 at 11:46
  • @Goiku I'll try doing some research on the things you mentioned, cheers. Commented Dec 16, 2016 at 11:47

1 Answer 1

1

Using session is the wrong answer, and you are overwriting the session value every time you loop, the best solution is using query strings, so the delete link should be like this

<a href="PHP/backend/account/addressDelete.php?id={$addrId}" class="button">Delete</a>

Then in the other file you read the address ID using

$addressId = $_GET['id'];
Sign up to request clarification or add additional context in comments.

4 Comments

Won't this will make the address ID visible in the URL bar? I don't want that, thanks though :)
Oh my bad, didn't see that in your question in the first time! are you find with having the addrId somewhere in the HTML? note that the user can still see it if he viewed the source of the html page in his browser.
No worries :). The '$addrId' and '$accId' will not be visible when the site goes live. As far as I'm aware you can't view PHP code in the browser..? If you're talking about where I echo all the values, that's just for testing
You need to inform the server using HTTP request which ID you want to delete, you can do that using GET request the way I said in my answer or make a form for each row with a hidden field with that id, but again this will be echoed as HTML and user can view the HTML source from any modern browser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.