0

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.

enter image description here

The ID changes to the corresponding product ID as I select a product from the dropdown

enter image description here

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);
}
bbowesbo
  • 885
  • 1
  • 7
  • 17
  • did you try to get `$_POST['id']`? provide your html form code – Alex Jun 10 '16 at 14:36
  • @Alex code added, and yeah im not sure if I was entering it wrong as it's php inside sql? – bbowesbo Jun 10 '16 at 14:40
  • Whatever you do - you should read [this question](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jurik Jun 10 '16 at 14:42
  • I'd assign a variable to `$row[ID]` and do `$id = $row[ID];` with an `isset()` and use that variable in your `WHERE` clause. That should work. Or a GET array. you have answers below to outline that – Funk Forty Niner Jun 10 '16 at 14:44
  • to me this part `select name='ID' OnChange='$` is not very smart or best way to proceed from user perspective. On selecting any value you just start to fill the form. Why do you submit? And why do you have 2 forms on your page? that should be just one form! – Alex Jun 10 '16 at 14:51

3 Answers3

0

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

Kld
  • 6,970
  • 3
  • 37
  • 50
0

Cant you set:-

$productID = $_GET['ID'];

And then change the 79 on the update script to $productID?

Hopefully I understood the question.

0

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.

Michael
  • 663
  • 1
  • 13
  • 28