Jump to content

update a field in a record when value selected from dropdown list


davids_media

Recommended Posts

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' ) ;
}
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.