Hello to all,

I am trying to create an admin panel so that admin can insert data into database.
This is my code

if (isset($_POST['submit'])) {

$name = $_POST["name"];
$brand = $_POST['brand'];
$quantity = $_POST['quantity'];
$detail = $_POST['detail'];
$unit = $_POST['unit'];
$color = $_POST['color'];
$material = $_POST['material'];
$image = $_POST['image'];

$sql = "Insert into flood_light (Name, Brand, Quantity, Detail, Unit, Color, Material, Image) values('$name', '$brand', '$quantity', '$detail', '$unit', '$color', '$material', '$image')";
// $sql1 = "update flood_light set Name = $name "
$result = $conn->  query($sql);
if ($result === true) {
    echo "data inserted successfully."."<br>";
    echo "error"."<br>". $conn->error;


It work's fine.

I want to add another query also (update query). If I add one more query then how can i mention it in form?
I have designed a form in html and admin can insert data by filling the information.
I just want to set a condition "If admin enters different information with same image then information will be update for that image".

11 Months
Discussion Span
Last Post by diafol

Hello Shivya,
If you want to do update with the same.
You need to add a hidden field where you will put the ID of the data to update.
After you can do a conditon PHP to verify if the ID exist to switch beetwen the insert and update action.



Set unique key constraint image and then insert/update in one SQL e.g.
prepare("Insert into flood_light (Name, Brand, Quantity, Detail, Unit, Color, Material, Image) values(?,?,?,?,?,?,?,?) on duplicate key update set Name = VALUES(Name), Brand = VALUES(Brand), Quantity = VALUES(Quantity), Detail = VALUES(Detail), Unit = VALUES(Unit) Color = VALUES(Color), Material = VALUES(Material)"); execute([$name, $brand, $quantity, $detail, $unit, $color, $material, $image]);

Edited by AndrisP


You can use it by sepearting the submit button value.
For example, in case of insert
<input name="submit" value="ADD">
in case of update
<input name="submit" value="UPDATE">

Also to capture the post information you can write in this way.
if (isset($_POST['submit']) && $_POST['submit']=='ADD')
//your insert statement
$sql = "Insert into flood_light (Name, Brand, Quantity, Detail, Unit, Color, Material, Image) values('$name', '$brand', '$quantity', '$detail', '$unit', '$color', '$material', '$image')";
//your insert statement end and else statement start
else if (isset($_POST['submit']) && $_POST['submit']=='UPDATE')
//your update statement start here.
$sql = "update flood_light set Name='$name'";
//your update statement end here.
To get the default image value, you can use:
if (isset($_POST['image'])) $image = $_POST['image']; else $image ='';

Edited by ajitsinghji


Sorry comma missing in my previous post

prepare("Insert into flood_light (
VALUES(?,?,?,?,?,?,?,?) on duplicate key update
set Name = VALUES(Name)
,Brand = VALUES(Brand)
,Quantity = VALUES(Quantity)
,Detail = VALUES(Detail)
,Unit = VALUES(Unit)
,Color = VALUES(Color)
,Material = VALUES(Material)"); 

It's difficult to know which approach to apply with this limited info. All-in-one forms are not always the best approach. If you have a products html table in admin mode, usually, you'd have a "Edit | Delete" links in a column. When you click edit, you then retrieve the info (usu. via ajax) into a form and display that - and modify the content and update. An "Add New Product" link is usually included on the page too. That can usually just show a hidden "insert" form. Perhaps I've got you're use case wrong.

These forms can be inserted / shown in-page, or nowadays you often see modal forms being used - especially if using a front-end framework like Bootstrap.

With the update (edit) form - you can fashion the url in the action as something like: "products/{id}/edit" - and use a "controller" to deal with the update. The "insert new" form gets sent to something like "products/create". That's the usual MVC method, e.g. Laravel framework. Although you may not be using a back-end framework yourself, it's a pretty sound approach.

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.