Hello to all,

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

<?php
include("connection.php"); 
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>";
}
else
{
    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?
Means,
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".

I want a single form which will perform both options.

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.

Thanks
Khadim

@shivya_1 post your form codings,,,

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]);

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

Also to capture the post information you can write in this way.
<?php
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:
<?php
if (isset($_POST['image'])) $image = $_POST['image']; else $image ='';
?>

Sorry comma missing in my previous post

<?php
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]
);
Member Avatar for diafol

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.