On a web app i'm making, I have an edit page which is obviously where users edit info on already existing products, it uses PHP/ MySQL. My edit page is ALMOST working exactly how I want it too. I have a drop-down populated from the SQL database, and I can edit the product info via the text-boxes. The only problem I have is that I do not know how to edit the products via the ID which changes as the products are selected.. I instead have to manually set a static value in the PHP code, I was just wondering how I could do this? I'm thinking GET could be used but not entirely sure.. I'll post pictures and code below.. thanks.
The ID changes to the corresponding product ID as I select a product from the dropdown
As you can see I have to type in the ID in the php..
so basically i was trying to get it to change the product to edit as i select them from the dropdown..
hope i wasnt too confusing, thanks!
$conn = new mysqli('', '', '', '')
or die ('Cannot connect to db');
$result = $conn->query("select ID, NAME from PRODUCTS");
?><div align="left"><?
print "<h3>EDIT PRODUCT</h3>";
print "<strong>SELECT PRODUCT:</strong>";
print "<br><br>";
print "<form method='GET' id='frm_product'>";
print "<select name='ID' OnChange='$(\"#frm_product\").submit();'>";
while ($row = $result->fetch_assoc())
{
print "<option value='$row[ID]'>$row[NAME]</option>";
}
print "</select>";
print "</form>";
?>
<form method='POST'>
<h3>PRODUCT:</h3>
<input type='textbox' name='product' value='<?php echo $product['product'] ? >'>
<h3>BARCODE:</h3>
<input type='textbox' name='barcode' value='<?php echo $product['barcode']; ?>'>
<h3>TYPE:</h3>
<select name="type">
<option value=""></option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
<br><br>
<input type='submit' value='Save Changes' name=submitform>
</form>
<?
if (isset($_POST['submitform']))
{
$namechange = $_POST[product];
$barcodechange = $_POST[barcode];
$typechange = $_POST[type];
$sql = "UPDATE PRODUCTS SET
NAME='$namechange'
,BARCODE='$barcodechange'
,TYPE='$typechange'
WHERE ID='79'";
if (mysqli_query($conn, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . mysqli_error($conn);
}
mysqli_close($conn);
}
Cant you set:-
$productID = $_GET['ID'];
And then change the 79 on the update script to $productID?
Hopefully I understood the question.
You can do something like this in javascript. redirect to the product page each time the user selects an option.
<select id="products" onchange="myFunction()">
<option value="Audi">Audi
<option value="BMW">BMW
<option value="Mercedes">Mercedes
<option value="Volvo">Volvo
</select>
<script>
function myFunction() {
var id = document.getElementById("products").value;
window.location.href = "/linkto/product.php?"+id;
}
</script>
If you don't want to reload the page each time you can use jQuery Ajax and JSON
If I get you right you want to edit the entries in the databse which belong to a special ID, right? As you tried with $_GET
you must add this to your sql query aswell:
[...]
$typechange = $_POST['type'];
// end of your old code
// start of new code
$id = $_GET['ID'];
// modified sql query
$sql = "UPDATE PRODUCTS SET NAME='$name', BARCODE='',TYPE='' WHERE ID='$id'";
// rest of your old code
[...]
Note: It´s not recommended to use user input without validating. A user can change the values for any existing ID by just manipulating the $_GET
parameter in your url. You should validate the users input before processing.