I have a CRUD Project.
For this I made a Database in MySQL using OOP method, also with HTML, PHP, Bootstrap and CSS.
In this Database there are 3 tables: Customers, Orders and Products, for my question it's only needed Customers and Orders.
The problem I'm having is in Read Customers (file), when there are more than 1 regist, only the first appear, and I can't find out why.
If someone can help I would appreciate :)
Code
<?php
$id_customer = '';
if (isset($_GET["id_customer"]) && !empty(trim($_GET["id_customer"]))) {
require_once "../connectDB.php";
$sql = "SELECT * FROM Customers
INNER JOIN Orders
ON Customers.id_customer = Orders.id_customer
WHERE Customers.id_customer = ?";
$columns = [
"ID Cliente" => "id_customer",
"Nome" => "fn_customer",
"Apelido" => "ln_customer",
"Email" => "email_customer",
"ID Compra" => "id_order",
"Data" =>"order_dt",
"Total" => "order_total"
];
if ($stmt = $mysqli->prepare($sql)) {
// Set parameters
$param_id = trim($_GET["id_customer"]);
// Bind variables to the prepared statement as parameters
$stmt->bind_param("i", $param_id);
// Attempt to execute the prepared statement
if ($stmt->execute()) {
$result = $stmt->get_result(); ?>
<table class="table table-bordered table-hover">
<thead class="thead-dark">
<tr>
<?php
foreach (array_keys($columns) as $heading) { ?>
<th scope="col"><?= $heading ?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php
$row = $result->fetch_array(MYSQLI_ASSOC); ?>
<?php foreach (array_values($columns) as $column) { ?>
<td><?= $row[$column] ?></td>
<?php } ?>
</table>
<?php
}
}
// Close statement
$stmt->close();
// Close connection
$mysqli->close();
}
?>
I tried removing and changing some lines of the code, not much, but it still didn't work.
You're not looping through your results. You want to wrap your foreach with one that goes through all the results:
while ($row = $result->fetch_array(MYSQLI_ASSOC)) { ?>
<tr>
<?php foreach (array_values($columns) as $column) { ?>
<td><?= $row[$column] ?></td>
<?php } ?>
</tr>
<?php } ?>