I have a piece of code generating a FORM in a web shop. This code fetches order data from a database and outputs each order line. The idea was to have three arrays that I want to use later on, but only one of them works and I cannot figure out why.
<?php
# Fetch product ID and count
$mysqli = new mysqli('localhost', 'user', 'pass', 'db');
$stmt = $mysqli->prepare("SELECT pid, count FROM orders_row WHERE oid = ?");
$stmt->bind_param("i", $_GET['oid']);
$stmt->bind_result($pid, $count);
$stmt->execute();
while($stmt->fetch())
{
# Fetch more info about each product
$mysqli2 = new mysqli('localhost', 'user', 'pass', 'db');
$stmt2 = $mysqli2->prepare("SELECT price, stock, title, orginal_no FROM product_products WHERE pid = ?");
$stmt2->bind_param("i", $pid);
$stmt2->bind_result($price, $stock, $title, $orginal_no);
$stmt2->execute();
$stmt2->fetch();
$stmt2->close();
$mysqli2->close();
# We will need pid[] later...
echo "<input type=\"hidden\" name=\"pid[]\" value=\"".$pid."\" />";
echo "<tr>";
echo "<td><input type=\"checkbox\" name=\"checkboxpid[]\" value=\"".$pid."\"/></td>";
echo "<td>".$pid."</td>";
echo "<td>".$orginal_no."</td>";
echo "<td>".$title."</td>";
echo "<td><input type=\"text\" name=\"count[]\" value=\"".$count."\" style=\"width: 2em\"/></td>";
echo "<td>".$stock."</td>";
echo "<td><input type=\"text\" name=\"price[]\" value=\"".addVAT($price)."\" style=\"width: 5em\"/></td>";
echo "<td>".(addVAT($price)*$count)."</td>";
echo "</tr>";
$total_price += addVAT($price)*$count;
}
$stmt->close();
$mysqli->close();
?>
The three arrays, as you can see, are pid[], count[] and price[]. They all look fine in the HTML code, they have the "[]" in the names and correct values. But when I post the form I get this:
Array
(
[oid] => 8
[pid] => 1
[count] => 1
[price] => Array
(
[0] => 570
[1] => 482
[2] => 455
)
)
Price is the only array. WHY?! 🙂