nothing showed up after click a link(from database) in php
nothing showed up after click a link(from database) in php
I have a database called simple_stall with table order_detail which have 4 columns ID Name Ordered_Item Quantity...currently after user submit their order, they'll be redirected to a page called order_detail.php...this page will show all ordered item in table with header ID Name Ordered_Item Quantity
simple_stall
order_detail
ID
Name
Ordered_Item
Quantity
order_detail.php
ID
Name
Ordered_Item
Quantity
now, when user click on someone's name from the table, i want to redirect user to a new page called view_more.php which will show the item ordered by the user however, nothing showed in the page.
view_more.php
This is my code:index.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
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
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
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);
}
3 Answers
3
It will not show,
because on view_more.php you have if (isset($_POST['Name'])) which will never be true since you are not using $_POST on view_more.php, you are using <td style = 'width:30%;'>" . "<a href='view_more.php?id=$name'>" . $row['Name'] . "</td> you are using normal link so replace it with this code
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);
}
and you should be good to go, however, I highly recommend you to use proper php framework.
When generating the link to the view_more.php page, you're are injecting a GET param named id:
id
<a href='view_more.php?id=$name'>
And on the view_more.php page, you are testing for a POST param called name:
name
if (isset($_POST['Name']))
fix this to
if (isset($_GET['id']))
By the way, your code is really, really ugly. there are a tons of things done wrong :
Try
<?php
if(isset($_GET['status']) && $_GET['status'] == 'ordered') {
header('location: LINK HERE');
}
?>
@WebCoder, please read the code carefully before you submit such answer
– Ray A
51 secs ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why should I "try" this code? Please instead of dumping code, explain why I should try this. Either in text around the code, or comments inside the code. It might be entirely obvious to you what this code does, but it might not be for future readers.
– Adriaan
6 mins ago