Front end form which echoes data and allows user to update content through various input fields?

StackOverflow https://stackoverflow.com/questions/21907666

  •  14-10-2022
  •  | 
  •  

Question

I'm fairly new to PHP & server side script as most of my work has been in front end development, so I'd be really grateful for any guidance or advice.

I've started to develop a small-scale product kiosk system, where a user can look up various products and view price and availability information (no need for self service or any kind of purchasing set up as it's primary use will be to list the products). For this specific system there will only be a set number of products, no more or less and the only product info that will change will be the availability and price (so there will be no need to ADD or DELETE any of the products). I've been looking into CRUD but as I only need to update the existing data through a form I was a little unsure with how to approach it.

So far, I have a MySQL database set up with a products table with all the products and information inserted.

In my index.php I created a basic table which echoes the data into the table.

index.php:

<!DOCTYPE html>
<html>

<head>
<style>
body {font-family: verdana; font-size: 12px; text-transform:capitalize;} 
#content {margin: 20px;}
table,th,td {border:1px solid #d1d1d1; padding: 12px;}
</style>
</head>
<body>

<div id="content">

<h1>Product Details</h1>

<?php

$con=mysqli_connect("SERVERINFO","DATABASE","USER","PASS");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


$result = mysqli_query($con,"SELECT * FROM products");

echo "<table border='1' cell-padding='2'>
<tr>
<th>ID</th>
<th>Prodcut</th>
<th>Price</th>
<th>Availabitly</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['product'] . "</td>";
  echo "<td>" . $row['price'] . "</td>";
  echo "<td>" . $row['availabitly'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?> 

</div>
</body>
</html>

This works fine, but I'm aiming to progress by creating an admin section where a user can change the content by either entering a new price via an input field or changing the availability through a drop down option directly on the table.

I'm not asking anyone to write the code for me as the main reason I'm undergoing this is to learn more and hopefully be pointed in the right direction.

Thanks.

Was it helpful?

Solution

simply create a page with a form to make the changes:

<form method="post" action="update.php">
<select name="availabillity">
<option>available</option>
<option>not available</option>
</select>
<input type="text" name="price">
<button type="submit">Submit</button>
</form>

In update.php do something like this:

$new_price = $_POST['price'];
$availabillity = $_POST['availabillity'];

Then you simply run a new mysql query to update the data in the table:

"UPDATE YOURTABLE SET price = $new_price, availabillity = '".$availabillity."' WHERE id = yourid" 

For more information on how to handle a form look here: Form handling in php

How to UPDATE data in mysql tables.

This is just example code to give you a hint.

Hope this helps you to get started.

OTHER TIPS

As you have defined here, you are well know of retrieving data from mysql, now queries regarding insertion and updating you have to visit this link, this will help you for

Demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top