У меня есть база данных с именем simple_stall
с таблицей order_detail
которой есть 4 столбца. ID
Name
Ordered_Item
Quantity
... в настоящее время после отправки пользователем своего заказа они будут перенаправлены на страницу с именем order_detail.php
... эта страница покажет весь упорядоченный элемент в таблица с заголовком ID
Name
Ordered_Item
Quantity
теперь, когда пользователь нажимает на имя кого-либо из таблицы, я хочу перенаправить пользователя на новую страницу с именем view_more.php
которая покажет элемент, упорядоченный пользователем, однако на странице ничего не отображается.
Это мой код: index.php
<div class="container">
<form action="insert_data.php" method="POST">
<div>
<input type="text" name="Name" placeholder="Name">
</div>
<div>
<input type="text" name="Order" placeholder="Order">
</div>
<div>
<input type="text" name="Quantity" placeholder="Quantity">
</div>
<div>
<button type="submit" name="submit">Send Order</button>
</div>
</form>
</div>
insert_data.php
if (isset($_POST['submit']))
{
include_once 'dbh.php';
// Escape user inputs for security
$name = mysqli_real_escape_string($connection, $_POST['Name']);
$order = mysqli_real_escape_string($connection, $_POST['Order']);
$quantity = mysqli_real_escape_string($connection, $_POST['Quantity']);
// attempt insert query execution
$sql = "INSERT INTO order_detail (Name, Ordered_Item, Quantity) VALUES ('$name', '$order', '$quantity')";
if(mysqli_query($connection, $sql))
header("Location: ./order_detail.php?status=ordered");
else
echo "ERROR: Could not able to execute $sql. " . mysqli_error($connection);
// close connection
mysqli_close($connection);
}
else
{
header("Location: ./index.php?status=failed");
exit();
}
order_detail.php
<body>
<table>
<tr>
<th width="30px">No</th>
<th width="30%">Name</th>
<th width="30%">Ordered Item</th>
<th width="50px">Quantity</th>
</tr>
<?php
include_once 'dbh.php';
$query = "SELECT * FROM order_detail"; //You don't need a ; like you do in SQL
$result = mysqli_query($connection, $query);
echo "<table border = 1px>"; // start a table tag in the HTML
while($row = mysqli_fetch_array($result))
{
$name = $row['Name'];
//Creates a loop to loop through results
echo "<tr><td style = 'width:30px;'>" . $row['ID'] . "</td>
<td style = 'width:30%;'>" . "<a href='view_more.php?id=$name'>" . $row['Name'] . "</td>
<td style = 'width:30%;'>" . $row['Ordered_Item'] . "</td>
<td>" . $row['Quantity'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysqli_close($connection); //Make sure to close out the database connection
?>
view_more.php
if (isset($_POST['Name']))
{
include_once 'dbh.php';
$name = $row['Name'];
$query = "SELECT * FROM order_detail WHERE Name = $name";
$result = mysqli_query($connection, $query);
echo "<table border = 1px>"; // start a table tag in the HTML
while($row = mysqli_fetch_array($result))
{
//Creates a loop to loop through results
echo "<tr><td style = 'width:30px;'>" . $row['ID'] . "</td>
<td style = 'width:30%;'>" . $row['Name'] . "</td>
<td style = 'width:30%;'>" . $row['Ordered_Item'] . "</td>
<td>" . $row['Quantity'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysqli_close($connection);
}
Он не покажет,
потому что на view_more.php
вас есть if (isset($_POST['Name']))
которое никогда не будет истинным, так как вы не используете $_POST
на view_more.php
, вы используете <td style = 'width:30%;'>". "<a href='view_more.php?id=$name'>". $row['Name']. "</td>
вы используете обычную ссылку, поэтому замените ее на этот код
if (isset($_GET['id']))
{
include_once 'dbh.php';
$name = $_GET['id'];
$query = "SELECT * FROM order_detail WHERE Name = '$name'";
$result = mysqli_query($connection, $query);
echo "<table border = 1px>"; // start a table tag in the HTML
while($row = mysqli_fetch_array($result))
{
//Creates a loop to loop through results
echo "<tr><td style = 'width:30px;'>" . $row['ID'] . "</td>
<td style = 'width:30%;'>" . $row['Name'] . "</td>
<td style = 'width:30%;'>" . $row['Ordered_Item'] . "</td>
<td>" . $row['Quantity'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysqli_close($connection);
}
и вам должно быть хорошо, однако, я настоятельно рекомендую вам использовать правильную инфраструктуру php.
При создании ссылки на страницу view_more.php
вы вводите параметр GET с именем id
:
<a href='view_more.php?id=$name'>
На странице view_more.php
вы тестируете параметр POST под name
:
if (isset($_POST['Name']))
исправить это
if (isset($_GET['id']))
Кстати, ваш код действительно, действительно уродлив. есть множество вещей, сделанных неправильно:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\simple_stall\view_more.php on line 14
какой причине? Кроме того, что вы подразумеваете под php framework? какmethod=post
?