phphtmlmysqlloopspurchase-order

Php array looping, creating dynamic variable for purchasing form


Still getting used to stackoverflow excuse my rookie-ness.. :)

Have an SQL query that returns a data put into a table in php. I want this table to be used for purchasing.

My idea was to use the product id, meaning i would use a dynamic php variable (not sure if I'm doing that right now) believe I saw a post something like $.$varaible$.$ it wasn't very clear and was a different subject.

My code is as follows:

$result = mysqli_query($con, "SELECT * FROM Product WHERE Type = 'Game'");
?>

<div class="wrapper">
    <h1 class="headGame">Buy Some Games Man</h1>
</div>
<br />
<div class="wrapper">

<?php
    echo 
    "<table border='1'>
    <tr>
    <th> Name </th>
    <th> Picture </th>
    <th> Console </th>
    <th> Description </th>
    <th> Price </th>
    <th> Amount </th>
    </tr>";

    echo '<form id="gamesOrder" action="purchase.php">';

    while($row = mysqli_fetch_array($result)) {
        $id = $row['Pd_Key'];
        echo"<tr>";
        echo"<td>" . $row['Name'] . "</td>";
        echo"<td>" . '<img class="prdPic" src="'. $row['Picture']. '">' . "</td>";
        echo"<td>" . $row['Type2'] . "</td>";
        echo"<td>" . $row['Description'] . "</td>";
        echo"<td>" . $row['Price'] . "</td>";
        echo"<td>"  . '<input type="number" min="0" max="100"; name="'.$id.'" value=0>' . "</td>";
        echo"</tr>";
    }
    echo '<input type="submit" value="  BUY  ">';
    echo '</form>';
?>

When I click the submit it changes the url but nothing happens, it doesn't redirect.

Any advice on how to get this entire process to work. The variable being used in a purchasing form, via a php file ie (purchase.php) and the variable to use for this.

EDIT - Had minor errors, but still not 100% on variable %id, won't that get redefined each loop, how can I have it dynamic so it can be used in a form to identify what the user wants to buy.

Now redirects but not to purchase.php

URL is ~/purchase.php?1=0&2=0&3=0&4=0&5=0&6=0&7=0&8=0&9=0&10=0&11=0&12=0&13=0&14=0&15=0&16=0&17=0&18=0&19=0&20=0&21=0

Thanks you legends you!! =D


Solution

  • You are missing the closing form caret:

    echo '<form id="gamesOrder" action"purchase.php"';
    

    should be:

    echo '<form id="gamesOrder" action="purchase.php">';
    

    Also you concatenation for ID is incorrect:

    echo"<td>"  . '<input type="number" min="0" max="100"; name=".$id." value=0>' . "</td>";
    

    should just be:

    echo"<td>"  . '<input type="number" min="0" max="100"; name="id" value="' .$id. '" value=0></td>';
    

    To access the id in purchase.php use the following code:

    $id = isset($_GET['id']) ? $_GET['id'] : null;
    

    And you need to assign the action to the form with an equals sign:

    action="myaction.php"