0

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';?>
<p><b>Choose Image</b><br /><input name="image_upload_box" type="file" 
id="image_upload_box"  /></p>
<b>Name</b><br /><input type=text name="aname" /><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>

And this is the form to display the products

<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']}]' value='Remove'>Remove";
echo "<br />";
echo "<input type='checkbox' name='edit[{$row['id']}]' value='Edit'>Edit";
echo "<br />";
if ($row['status'] ==  1){
echo"<input type='checkbox' name='hide[{$row['id']}]' value='Hide'>Hide";
echo "<br />";
}
if ($row['status'] == 2){
echo"<input type='checkbox' name='show[{$row['id']}]' value='Show'>Show";
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>

EDIT; This is the form on my new edit page.How do i get the relevent details from the db into the input values?

<?php if (isset($_GET['pid'])) { ?>
<form class='productsaddform' action='edit_products.php' method='post' 
enctype='multipart/form-data' name='image_upload_form' id='image_upload_form'>
<span class='formheading'>Edit Product</span><br /><br />
<p><b>Choose Image</b><br /><input name="image_upload_box" type="file" 
                             id="image_upload_box"  /></p>
<b>Name</b><br /><input type="text" name="aname" />
<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 />
<input type='submit' id='submit' name='submit' value='Add Product' />
<input type='hidden' value='new' /><br />
<?php include '../inc/edit_products.php'; ?>
</form>
<?php } ?>

This is what i have now, but the form doesnt display now

<?php if (isset($_GET['pid'])) {
$q = mysqli_query($link, "SELECT *  FROM products WHERE id = '".$_GET['pid']."'") or 
die (mysqli_error());
$row = mysqli_fetch_array($q);   ?>
<form class='productsaddform' action='edit_products.php' method='post' 
enctype='multipart/form-data' name='image_upload_form' id='image_upload_form'>
<span class='formheading'>Edit Product</span><br /><br />
<p><b>Choose Image</b><br /><input name="image_upload_box" type="file" 
                             id="image_upload_box"  /></p>
<b>Name</b><br /><input type="text" name="aname" value=<?php"$row['name'];"?> />
<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 />
<input type='submit' id='submit' name='submit' value='Add Product' />
<input type='hidden' value='new' /><br />
<?php include '../inc/edit_products.php'; ?>
</form>
<?php } ?>

EDIT: Now I want to be able to search products by code in my first form then have that products details displayed in the form in my edit_product page as it is when i edit a product as we sorted. Any idea how to go about this?

GlenR
  • 95
  • 1
  • 2
  • 13
  • I wonder if this is one of those times where using ajax calls might work a bit better. You populate a large table with products or use autocomplete to find the product. An ajax call pulls the product data, you edit the data, then "UPDATE" or "DELETE" the row. – TimSPQR Dec 16 '13 at 22:02
  • sounds good but i have no idea how to go about it! – GlenR Dec 17 '13 at 11:39

1 Answers1

2

This wouldn't be the necessarily the cleanest way to do it, but you could get the information from the db (as you would normally), put it into an array and for each input have something like this:

<input type="text" name="aname" value="<?php echo (isset($array['name'])) ? $array['name'] : ''; ?>" />

Basically, it's an inline if statement that checks to see if the variable is set and if it is, it sets the value of the input to that.

Hope this helps!

If you have any questions on this, let me know :)

EDIT

To answer your question from the comments you could have a link for each product which then takes you to an edit product page e.g.

Link:

<a href="edit-page.php?productId=$row['id']"></a>

AND then just have a page similar to you first page that checks to see if the get variable is set.

Rwd
  • 34,180
  • 6
  • 64
  • 78
  • i have done this in my first form but what do i need to do to my second form? – GlenR Dec 17 '13 at 11:38
  • What do you want to to get/change with your second form? Sorry, your question isn't very clear at all :) – Rwd Dec 17 '13 at 12:07
  • Sorry! My second form is where i can choose categories and subcategories to show the products. Once the products are shown they can be deleted, hidden, or unhidden. I also need to be able to click an 'edit details' button, then have the details for that product appear back in the first forms fields for me to edit. Then i will just update the row with the edited details. That any clearer? – GlenR Dec 17 '13 at 12:14
  • thanks a lot! im getting there now! could you read my edit in my op please. im lost here!........... – GlenR Dec 17 '13 at 16:20
  • @user2620338 What are the fields names from your product table? – Rwd Dec 17 '13 at 16:36
  • Im away from my pc now but of the top of my head they are id name brand description spec price – GlenR Dec 17 '13 at 16:49
  • It would be very similar to what you did in your second page, except using `$_GET` rather than `$_POST`. Then you wouldn't need a while loop you could just have `$row=mysqli_fetch_array($result)` and then your inputs would have `value="$row['id']"` or `value="$row['name']"` etc. – Rwd Dec 17 '13 at 19:08
  • can you show me an example of one of your links to the edit_products.php – Rwd Dec 17 '13 at 20:04
  • Edit is that what you mean? – GlenR Dec 17 '13 at 21:36
  • Just realised what the issue is: `value=` should be `value=` remove the double quotes and add echo. :) – Rwd Dec 17 '13 at 21:41
  • Btw, are you using MAMP or WAMP on your computer? – Rwd Dec 17 '13 at 21:42
  • $q = mysqli_query($link, "SELECT * FROM products WHERE id = '".$_GET['pid']."'") or die (mysqli_error()); $row = mysqli_fetch_array($q); ?> if i take these 2 lines out then it show the form without values – GlenR Dec 17 '13 at 21:46
  • If you find a file called php_error.log in your wamp directory ... look at this post: http://stackoverflow.com/questions/9230984/error-logging-with-wamp-server-in-php . Also, the only other thing I can see that would be causing this issue is have you included you connect.php?? Either way, try and get your error logs up and running as these will help you out a lot!!! I use a program called textwrangler to view the logs. – Rwd Dec 17 '13 at 22:02
  • i feel silly now, the path to connect.php was wrong! i will look into getting the error log up and running too. thanks for all your help @ross, i really do appreciate it! – GlenR Dec 17 '13 at 22:23
  • Cool, cool. Glad you've got it working! :) Would you mind marking the answer as correct? – Rwd Dec 18 '13 at 08:49
  • ive accepted your answer @ross. one more thing you could maybe help with. could you read my latest edit? – GlenR Dec 18 '13 at 19:23
  • its ok, i got it going! – GlenR Dec 18 '13 at 21:03