I have a form for adding products and their detail to a mysql db. I have another form that shows the products where I can remove the product from the db, I can also hide or show the product in the website.

I need to be able to edite the product details too. Is there any way that when I choose to edit a products I can get it details to appear in the first form again? The form is for adding details so can it be used to edit them too?

This is my code for first product adding form.

<form class='productsaddform' action='productsadd.php' method='post' enctype='multipart/form-data' name='image_upload_form' id='image_upload_form'>
                <?php
                   include '../inc/categorydropdown.php';
                    ?>
                   <span class='formheading'>Add Product</span><br /><br />
                   <p><b>Choose Image</b><br /><input name="image_up />
                   <br />
                   <b>Brand</b><br /><input type=text name="abrand" /><br />
                   <b>Code</b><br /><input type=text name="acode" /><br />
                   <b>Description</b><br /><textarea rows="12" cols="40" name="adescription"></textarea><br />
                   <b>Product Spec</b><br /><textarea rows="12" cols="40" name="aspec"></textarea><br />
                   <b>Price</b><br /><input type=text name="aprice" /><br />
                   <p><label for="cat">Category</label>
                   <select name="cat" id="cat">
                      <?php echo $op;?>   
                   </select><br />
                   <label for="subcat">Subcategory</label>
                  <select name="subcat" id="subcat"> </select></p>
                  <br />
                  <br />
                  <input type='submit' id='submit' name='submit' value='Add Product' />
                  <input type='hidden' value='new' /><br />
                 <?php include '../inc/add_products.php'; ?>

            </form>

This is my second form, where i can clicked edit on a product

<form class='productsremoveform' action='productsadd.php' method='post'>
            <?php include '../inc/categorydropdown.php'; ?>
            <?php include '../inc/remove_products.php'; ?>
            <span class='formheading'>Remove/Hide/Show Products</span><br /><br />
            <p><label for="cat">Category</label>
            <select name="cat" id="removecat"> <?php echo $op;?> </select><br />
            <label for="subcat">Subcategory</label>
            <select name="subcat" id="removesubcat"> </select>
            <input type='submit' name='show' value='Show' /> </p>
            <?php
               include '../inc/connect.php';
                  if(isset($_POST['show'])){
                  $subcat = intval($_POST['subcat']);
                  $q = "SELECT * FROM products WHERE subcat = $subcat";
                  $result = $link->query($q);
                  if($result){
                      while($row=mysqli_fetch_array($result)){
                      echo "<div class='removeproducts'>";

                         echo "<input type='checkbox' name='remove[{$row['id']}]'>Remove";
                         echo "<br />";
                              if ($row['status'] ==  1){
                                 echo"<input type='checkbox' name='hide[{$row['id']}]'>Hide";
                                 echo "<br />";
                              }
                              if ($row['status'] == 2){
                                 echo"<input type='checkbox' name='show[{$row['id']}]'>Show";
                                 echo "<br />";
                              }
                              echo "<input type='submit' name='edit[{$row['id']}]' value='Edit'>";
                         echo "<br />";
                          echo "<br />",
                          "<span class='productthumbcode'>",
                          "{$row['code']}",
                          "</span>",
                               "<div id='thumb'>",
                               "<img class='resizedimage' src='{$row['image']}' alt='{$row['name']}' />",
                               "</div>",
                               "</div>";
                        }
                        echo "<br style='clear:both' />";
                    }
                    else{
                       echo mysqli_error();
                    }
                    }
                   ?>
                   <input type='submit' id='submit' name='submit' value='Submit' />
            </form>

And this is the code to be used when edit is clicked.

if(isset($_POST['edit'])){
                 $chk = (array) $_POST['edit'];
                 $p = implode(',',array_keys($chk)); 
                 $t = "SELECT * FROM products WHERE id IN ($p)";
                 $result = $link->query($t);
                 $url=mysqli_fetch_array($result);

                  }

Anyone able to help me out?
Thanks

Member Avatar for diafol

The usual suspect here is to have a hidden field with the value set to empty (add/insert) or to the original id value (modify/update).

e.g.

<?php
if(!isset($row))
{
    $val = "";
}else{
    $val = $row['id'];
}
?>

So somewhere in the form...

<input type="hidden" name="mode" value="<?php echo $val;?>" />

Then check after submit:

if(isset($_POST['mode']))
{
    if($_POST['mode'] && is_int($_POST['mode']))
    {
        $sql = "UPDATE ...";
    }else{
        $sql = "INSERT INTO ...";
    }
}

For confirmation of what you're doing, you could change the value of the submit button too, to 'ADD' or 'CHANGE' (or similar):

<?php
if(!isset($row))
{
    $val = ""; $label = "ADD";
}else{
    $val = $row['id']; $label = "CHANGE";
}
?>
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.