i want to edit my data in database called simple_stall
with table order_detail
...currently i have done a page that shows a list of data with No
Name
Ordered_Item
Quantity
. When user click the No
, they'll be redirected to a new page that shows only the data of that he clicked.
Now, when user click on Edit
button, they'll be redirected to a new page called update_info.php
. Here is a form to change Name
Ordered_Item
and Quantity
...but now when i click update order
button, it will update all rows to be the data that user just put in...
What i want is to UPDATE only the data of that No
that user click
this is the code
order_detail.php
<?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))
{
$no = $row['No'];
//Creates a loop to loop through results
echo "<tr><td style = 'width:30px;'>" . "<a href='view_more.php?no=$no'>" .$row['No'] . "</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
echo "<button type='button'><a href='./index.php'>Back</a></button>";
view_more.php
if (isset($_GET['no']))
{
include_once 'dbh.php';
$no = $_GET['no'];
$query = "SELECT * FROM order_detail WHERE No = '$no'";
$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['No'] . "</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
echo "<button type='button'><a href='./update_info.php?no=$no'>Edit</a></button>";
echo "<button type='button'><a href='#'>Delete</a></button>";
echo "<button type='button'><a href='./order_detail.php'>Back</a></button>";
mysqli_close($connection);
update_info.php
<form action="update_data.php" method="POST">
<div>
<input type="text" name="NewName" placeholder="Name">
</div>
<div>
<input type="text" name="NewOrder" placeholder="Order">
</div>
<div>
<input type="text" name="NewQuantity" placeholder="Quantity">
</div>
<div>
<button type="submit" name="submit">Update Order</button>
</div>
</form>
update_data.php
if(isset($_POST['submit']))
{
include_once 'dbh.php';
$update = "UPDATE order_detail SET Name='$_POST[NewName]', Ordered_Item='$_POST[NewOrder]', Quantity='$_POST[NewQuantity]' ";
if (mysqli_query($connection, $update))
{
header("Location: ./order_detail.php");
exit();
}
else
{
header("Location: ./order_detail.php?update=failed");
exit();
}
}