user11119483 user11119483 - 7 months ago 15
PHP Question

Why is this variable not being posted and giving an error?

Why is

bakeryid
variable not being posted in my form? The error I am receiving is


"Notice:Undefined variable bakeryid"


I have two pages, one displays the form and the second is the action for the form. The second form kept saying that it was undefined also. The
bakeryid
is the ID for each cakes order.

$sql = mysqli_query($con,"SELECT `firstname`, `bakeryid`, `order` FROM cakes");

$bakeryid = $_POST['bakeryid'];

?>
<table border='2'>
<th>First Name</th>
<th>Order</th>
<?php
echo '<form name="display" method="POST" action="cakephp.php">';
while($row = mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['order'] . "</td>";
echo '<td><input type="hidden" name="bakeryid" value="' . $bakeryid . '"/></td>';
echo '<td><input type="hidden" name="memid" value="' . $memid . '"/><input type="submit" name="takeorder" value="Take Order" ></td>';
echo "</tr>";
}
echo "</form>";
echo "</table>";


Answer:

echo '<td><input type="hidden" name="bakeryid" value="' . $row['bakeryid'] . '"/></td>';

Answer

You set $bakeryid with $_POST['bakeryid'], but then define bakeryid with $bakeryid.

Please try:

$sql = mysqli_query($con,"SELECT `firstname`, `bakeryid`, `order` FROM cakes c INNER JOIN members m ON c.memid = m.memid");

$bakeryid = $_POST['bakeryid']; // this line is unnecessary

    ?> <table border='2'>
            <th>First Name</th>
            <th>Order</th>
<?php
        echo '<form name="display" method="POST" action="cakephp.php">';
while($row = mysqli_fetch_array($sql))
            {
                    echo "<tr>";
                    echo "<td>" . $row['firstname'] . "</td>";
                    echo "<td>" . $row['order'] . "</td>";
                    echo '<td><input type="hidden" name="bakeryid" value="' . $row['bakeryid'] . '"/></td>'; // this line changed
                    echo  '<td><input type="hidden" name="memid" value="' . $memid . '"/><input type="submit" name="takeorder" value="Take Order" ></td>';
                echo "</tr>";
            }
echo "</form>";