1

I am in the process of studying PHP and wondering if someone would be kind enough to help me with a bit I am stuck with.

In the following form it will pull a list of the users Addresses based on the users ID and show them by their 'Address_Name' field.

However, when they select say 'Home Address' I don't want to pull that value which is the 'Address_Name' but I want to pull the row value for 'Address_ID'.

Can anyone help me understand how I can do this, bearing in mind the 'Address_Name' will not be UNIQUE.

<?php 

$add=mysql_query("SELECT * FROM Address_Book WHERE User_ID = '$id'");

while ($row = mysql_fetch_array($add)){

?>

<form name="form1" onsubmit="return validate()">
<input type="hidden" name="command" />
<div align="center">
<h1 align="center">Confirm Order</h1>
<table border="0" cellpadding="2px">
<tr><td>Order Total:</td><td><?php echo $currency ?><?php echo get_order_total()?></td></tr>
<tr><td>Account_Number</td><td><input type="text" name="accountnumber" /></td></tr>
<tr><td>Delivery Address</td><td><select name="owner">
<option name="address" id="address" value="Address_Name"><?php echo $row['Address_Name']; ?></option>
</td></tr>
<tr></tr>
<tr><td>&nbsp;</td><td><input type="submit" value="Confirm Order" /></td>    
</tr>
</table>    
</div>
</form>

<?php

}
?>
  • 2
    Stop what you're doing NOW. You are using database functions that are deprecated, unmaintained, and insecure. I recommend switching to use PDO, but mysqli is also an option. Both have been available for more than a decade. http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – miken32 Feb 16 '17 at 22:24
  • 2
    What's the point of a drop-down with just one option in it? – Barmar Feb 16 '17 at 22:25
  • It seems like this is all wrong. You're creating a separate form for each of the user's addresses. It should be one form, and each address should be an option in the drop-down. The loop should just be around the part that adds the ` – Barmar Feb 16 '17 at 22:27
  • 1
    You should put `Address_ID` in the `value` attribute, and `Address_Name` in the option text. – Barmar Feb 16 '17 at 22:28
  • Miken32 is correct, and it is pretty easy to convert to mysqli < – Duane Lortie Feb 16 '17 at 22:29

2 Answers2

0

As have been said, you could improve your code further and secure it by using PDO or mysqli, but the short answer to your question is to put the Address_ID in the value attribute:

<option name="address" id="address" value="<?php echo $row['Address_ID']; ?>"><?php echo $row['Address_Name']; ?></option>
Michael Krikorev
  • 2,126
  • 1
  • 18
  • 25
-1

Miken32 said it already but MySQL is deprecated and insecure. You should be using PDO to make Database Calls

I have posted an updated version of your code with comments and reasoning

<?php
$database->query("SELECT * FROM Address_Book WHERE User_ID = :User_ID"); // Selecting from the database - Kind of like prepare
$database->bind(':User_ID', $UserID); // Binding values to prevent SQL Injection
$Addresses = $database->resultset(); // Returns and array of all the different rows
?>

<form name="form1" onsubmit="return validate()">
    <input type="hidden" name="command" />
    <div align="center">
        <h1 align="center">Confirm Order</h1>
        <table border="0" cellpadding="2px">
            <tr>
                <td>Order Total:</td>
                <td><?php echo $currency ?>
                    <?php echo get_order_total()?>
                </td>
            </tr>
            <tr>
                <td>Account_Number</td>
                <td>
                    <input type="text" name="accountnumber" />
                </td>
            </tr>
            <tr>
                <td>Delivery Address</td>
                <td>
                    <select name="owner">

                        <?php
                        foreach($Addresses as $Address) // Here for each Address in the Addresses array we echo an option with the value as the Address_ID and DisplayName as Address_Name
                        {
                            echo '<option name="address" id="address" value="'.$Address["Address_ID"].'>'.$Address['Address_Name'].'</option>';
                        }
                        ?>

                    </select> <!-- You were btw missing an ending </select> -->
                </td>
            </tr>
            <tr></tr>
            <tr>
                <td>&nbsp;</td>
                <td>
                    <input type="submit" value="Confirm Order" />
                </td>
            </tr>
        </table>
    </div>
</form>