davids_media Posted May 7, 2012 Share Posted May 7, 2012 Yesterday, I created a topic about how I could update records and I managed to achieve that successfully. Now I have another dilemma. When I have a specific record I want to update, I want to change a category ID of an product (e.g. change it from 1 to 2) but how do I go about doing this? Here is my code thus far: <?php require_once ('./includes/config.inc.php'); require_once (MYSQL); $id=$_GET['prodID']; $results = mysqli_query($dbc, "SELECT * FROM product WHERE prodID=".$_GET['prodID'].""); $row = mysqli_fetch_assoc($results); ?> <form action="" method='POST'> Product ID: <input type="text" value="<?php echo $row['prodID'];?>" name="prodID" /> <br /> Product: <input type="text" value="<?php echo $row['product'] ;?>" name="product" /> <br /> Product Description: <input type="text" value="<?php echo $row['prod_descr'] ;?>" name="prod_descr" /> <br /> Category: <select name="category"> <option value="<?php echo $row['catID'];?>"></option> </select> Price: <input type="text" value="<?php echo $row['price'] ;?>" name="price" /> <br /> In Stock: <input type="text" value="<?php echo $row['stock'] ;?>" name="stock" /> <br /> <br /><input type="submit" value="save" name="save"> </form> <?php if(isset($_POST['save'])) { $id = $_POST['prodID']; $product = $_POST['product']; $descr = $_POST['prod_descr']; $price = $_POST['price']; $stock = $_POST['stock']; // Update data $update = mysqli_query($dbc, "UPDATE product SET product='$product', prod_descr='$descr', price='$price', stock='$stock' WHERE prodID=".$_GET['prodID'].""); header( 'Location: update_save.php' ) ; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262229-update-a-field-in-a-record-when-value-selected-from-dropdown-list/ Share on other sites More sharing options...
Psycho Posted May 8, 2012 Share Posted May 8, 2012 It looks like your form is only populating a single option in the category select list - so the user would never be able to change it anyway. But, assuming you have multiple categories available and the VALUE of the options is the ID, then you would just add that POST value to your UPDATE query: $id = intval($_POST['prodID']); $product = mysql_real_escape_string(trim($_POST['product'])); $descr = mysql_real_escape_string(trim($_POST['prod_descr'])); $price = floatval($_POST['price']); $stock = $_POST['stock']; $catID= intval($_POST['category']); // Update data $query = "UPDATE product SET product='$product', prod_descr='$descr', price='$price', stock='$stock', $category='$catID' WHERE prodID = '$id'"; $update = mysqli_query($dbc, $query); header( 'Location: update_save.php' ) ; That is just an example since I don't know the name of your category field in the DB. Also, you need to sanitize user input before uysing in a query so I added some code for that. Quote Link to comment https://forums.phpfreaks.com/topic/262229-update-a-field-in-a-record-when-value-selected-from-dropdown-list/#findComment-1343869 Share on other sites More sharing options...
davids_media Posted May 8, 2012 Author Share Posted May 8, 2012 its called catID and acts as a foreign key, derived from another table i created called category with fields catID(pk) and cat Quote Link to comment https://forums.phpfreaks.com/topic/262229-update-a-field-in-a-record-when-value-selected-from-dropdown-list/#findComment-1343901 Share on other sites More sharing options...
Psycho Posted May 8, 2012 Share Posted May 8, 2012 Maybe I wasn't understanding your question then. Is your issue that you are unsure of how to create the options for the select list? Or, what specifically are you having problems with? Quote Link to comment https://forums.phpfreaks.com/topic/262229-update-a-field-in-a-record-when-value-selected-from-dropdown-list/#findComment-1344089 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.