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

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 diafol

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 diafol

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 diafol

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.

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

commented: good shout +14
Member Avatar for diafol

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.

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.