i have the below code which is populating my drop down with the product names stored in tblproduct. the price of the products are stored in another table called tblretprod. the common field between the tables is the prod_id.
when i am selecting the product name, the price related to the product is not being displayed.
how can i update the price in tblretprod for each product as well when i change the price displayed to another one?

here is the code populating the select drop down.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Update product</title>

<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.prod_name.options[form.prod_name.options.selectedIndex].value;
//var description1=form.description.value;
self.location='upd_prod.php?prod_name=' + val;
//+'&description=' +description1;
}

</script>

<link href="CSS/update.css" rel="stylesheet" type="text/css" />
</head>

<body>

<?php

include('db_connect.php');

@$prod_name=$_GET['prod_name'];

$query=mysql_query("select p.prod_name, pr.prod_price from tblproduct p INNER JOIN tblretprod pr ON p.prod_id = pr.prod_id");

?>

<div id="stylized" class="myform">

<form id="form" name="upd_prod" method="post" action="updprod.php">

<h2 align="center"><b>- Update Product -</b></h2>

<table width="1000" border="0">
  <tr>
    <td><div align="right">Product Name</div></td>
    <td>
    <?PHP 

         echo "<select name='prod_name' onchange=\"reload(this.form)\"><option value=''>select one</option>";
while($row1 = mysql_fetch_array ($query)) { 
if($row1['0']==@$prod_name){echo "<option selected value='".$row1['0']."'>".$row1['0']."</option>";}
else{echo  "<option value=\"".$row1['0']."\">".$row1['0']."</option>";}
}
echo "</select>";
        ?>

    </td>
  </tr>
  <tr>
    <td><div align="right">Product Price (MRU)</div></td>
    <td><input type="text" name="prod_price" id="prod_price" value = "<?PHP print $row['prod_price'] ?>"/></td>
  </tr>
 </table>

<p align="center">
  <input type="submit" class="button" name="update" id="update" value="<-- Update product -->" />
</p>

</form>

</div>

</body>
</html>

error message received is

Notice: Undefined variable: row in C:\wamp\www\buysmart_site\upd_prod.php on line 61

Dani AI

Generated

A short, practical summary and checklist for the problem described: the price field was empty because the page never guaranteed a single, defined row for the currently selected product before printing the price. correctly flagged a variable-mismatch issue in the loop, and correctly called attention to the GET/POST/querystring flow and suggested AJAX as an alternative. The reliable approach is: use a stable identifier (prod_id) as the <option> value, read that identifier from the request on page load, fetch exactly one row for that id, store it in a single variable (for example $selected_product), and only then echo the price (guarded with isset or a default).

Checklist (concrete, ordered):

  1. Make each <option> use the product id as its value to avoid duplicate-name ambiguity.
  2. After the onchange-generated reload, read the id from $_GET (or from $_POST if posting) and confirm the querystring actually contains the id in the browser address bar.
  3. Run a single SELECT WHERE prod_id = ? and assign that row to a clearly named variable before rendering the price input.
  4. When echoing into the value attribute, wrap with a existence check and htmlspecialchars to avoid notices and XSS.
  5. Ensure the form that performs the update includes the prod_id (hidden field) so the server knows which row to update.

A minimal, secure server-side update pattern (use PDO or mysqli prepared statements):

$stmt = $pdo->prepare("UPDATE tblretprod SET prod_price = :price WHERE prod_id = :id");
$stmt->execute([':price' => $newPrice, ':id' => $prodId]);

Validate that $newPrice is numeric before executing.

Extra notes: migrate off deprecated mysql_* functions to PDO/mysqli, cast/validate numeric prices, and consider an AJAX call to fetch the price dynamically (as suggested) so the page does not need a full reload.

Recommended Answers

All 12 Replies

Just a note: A Notice is not a fatal error message, and should not stop script execution. If you're still not getting the result you expect, the issue is probably elsewhere.

On line 57 you try to access $row. The variable you used in your loop was $row1, $row doesn't exist.

Member Avatar for Member #120589

Yo're asking for $_GET['prod_name'] but you're sending the form via post, so it will be $_POST['prod_name']

There again, you're intercepting the form submission with js and using that to create a querystring. It's a little confused.

Ideally, you'd use ajax maybe for the change selectbox value.

You could also use ajax to update the price.

Yo're asking for $_GET['prod_name'] but you're sending the form via post, so it will be $_POST['prod_name']

I don't think this is upd_prod.php. I think he's using the $_GET to get the product name from the URL created when onchange=reload() runs from the SELECT menu.

Member Avatar for Member #120589

I think he's using the $_GET to get the product name from the URL created when onchange=reload() runs from the SELECT menu.

I realise that, as I mentioned:

you're intercepting the form submission with js and using that to create a querystring

So the obvious question now is, does the querystring show the value it's supposed to show?

Hello guys, even if i use

$row1[prod_price]

am still having the issue. the price is not being displayed

Member Avatar for Member #120589

Yes well, it won't as you've hijacked the form submission with js to reload with a querystring. Like I said it's confused. I can understand using post as a fallback if js not implemented, but the php doesn't reflect that.

i tried below code as well but this time the drop down is not being displayed and am having an undefined variable for selected_product

it would be of great help if i knew where the issue is and how to sort it out...either with the code below or the previous one i provided

thank you....am stuck with that since some days now and would like to sort this out please

<!-- you beginning HTML -->
<?php


include('db_connect.php');

$prod_name = isset($_GET['prod_name'])?$_GET['prod_name']:"";

$sql = "select p.prod_name, pr.prod_price from tblproduct p INNER JOIN tblretprod pr ON p.prod_id = pr.prod_id";

$result = mysql_query($sql);

if(mysql_num_rows($result) < 1) {} //No records, do something (exit, die, return, whatever)

?>
<!-- Continue your HTML -->
<?php 

while($row = mysql_fetch_array($result){
if($row['prod_name'] == $prod_name) {
$selected_product = $row;
echo "<option selected value='".$row['0']."'>".$row['0']." </option>";
} else {
echo "<option value=\"".$row['0']."\">".$row['0']."</option>";
}
}
?>
<!-- Continue your HTML -->
<td><input type="text" name="prod_price" id="prod_price"

value = "<?php print $selected_product['prod_price'] ?>"/></td> 
<!-- Rest of HTML -->

Sorry, I'm missing something. I don't get how OP was hijacking the form at the top. The "reload" function only happens when you change the SELECT. It seems to me like the point is that on reload it correctly updates the prod_price field with the value from the db. Then the form gets submitted to updprod.php (as opposed to upd_prod.php), where I assume the price is updated in the DB.

, what happened to your <form>, and more importantly, the <select> element?

commented: good shout +14
Member Avatar for Member #120589

Good shout EF - I totally got the wrong end of that one :)

@sanbhu - ignore my ramblings - I misinterpreted the code :( Sorry.

It's ok don't worry. Here is the full code. Please kindly help me sort this out. The first code i had is above and the new one am posting now below

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Update product</title>

<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.prod_name.options[form.prod_name.options.selectedIndex].value;
//var description1=form.description.value;
self.location='upd_prod.php?prod_name=' + val;
//+'&description=' +description1;
}

</script>

<link href="CSS/update.css" rel="stylesheet" type="text/css" />
</head>

<body>

<?php
include('db_connect.php');
$prod_name = isset($_GET['prod_name'])?$_GET['prod_name']:"";
$sql = "select p.prod_name, pr.prod_price from tblproduct p INNER JOIN tblretprod pr ON p.prod_id = pr.prod_id";
$result = mysql_query($sql);
if(mysql_num_rows($result) < 1) {} //No records, do something (exit, die, return, whatever)
?>

<div id="stylized" class="myform">

<form id="form" name="upd_prod" method="post" action="updprod.php">

<h2 align="center"><b>- Update Product -</b></h2>

<table width="1000" border="0">
  <tr>
    <td><div align="right">Product Name</div></td>
    <td>
    <?php 
while($row = mysql_fetch_array($result)){
if($row['prod_name'] == $prod_name) {
$selected_product = $row;
echo "<option selected value='".$row['0']."'>".$row['0']." </option>";
} else {
echo "<option value=\"".$row['0']."\">".$row['0']."</option>";
}
}
?>
    </td>
  </tr>
  <tr>
    <td><div align="right">Product Price (MRU)</div></td>
    <td><td><input type="text" name="prod_price" id="prod_price"
value = "<?php print $selected_product['prod_price'] ?>"/></td> </td>
  </tr>
  </table>

<p align="center">
  <input type="submit" class="button" name="update" id="update" value="<-- Update product -->" />
</p>

</form>

</div>

</body>
</html>

hello guys i managed to sort it out. some codes had to be tampered with in the one provided above.
thank you all for your help and guidance.

Glad it worked out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.