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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.