hello guys, i have 2 tables - tblproduct and tblretprod. prod_id is common in both. i am retrieving records from the tables to display with edit and delete options beside each record displayed.
i created an update.php and updprod.php (for edit).

update.php

<!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/Delete Product</title>

<?php

include_once("db_connect.php");

$result=mysql_query("SELECT a.prod_name, a.prod_brand, b.prod_price FROM tblproduct a, tblretprod b where a.prod_id = b.prod_id");

?>

</head>

<body>

<?php

echo "<table width='80%' border=0>";

echo "<tr bgcolor='#CCCCCC'>";
echo "<td>Product</td>";
echo "<td>Brand</td>";
echo "<td>Price</td>";
echo "<td>Action</td>";
echo "</tr>";

while($res=mysql_fetch_array($result)){

echo "<tr>";
echo "<td>".$res['prod_name']."</td>";
echo "<td>".$res['prod_brand']."</td>";
echo "<td>".$res['prod_price']."</td>"; 
echo "<td><a href=\"edit.php?prod_id=$res[prod_id]\">Edit</a> | <a href=\"delete.php?prod_id=$res[prod_id]\">Delete</a></td>";
}

echo "</table>";
?>

</body>
</html>


</body>
</html>

error message being displayed for code above is - notice: Notice: Undefined index: prod_id in C:\wamp\www\buysmart_site\update.php on line 36

upd_prod.php

include_once("db_connect.php");

if(isset($_POST['update']))
{
$prod_id = $_POST['prod_id'];

$prod_name=$_POST['prod_name'];
$prod_brand=$_POST['prod_brand'];
$prod_price=$_POST['prod_price']; 

// checking empty field
if(empty($prod_price))
{
//if name field is empty
if(empty($prod_price))
{
echo "<font color='red'>Price field is empty.</font><br/>";
}

} 
else
{ 
//updating the table
$result=mysql_query("UPDATE tblretprod SET proc_price='$prod_price' WHERE prod_id=$prod_id");

//redirectig to the display page. In our case, it is index.php
//header("Location: index.php");
}
}
?>
<?php
//for displaying data of this particular data

//getting id from url
$prod_id = $_GET['prod_id'];

//selecting data associated with this particular id
$result=mysql_query("SELECT a.prod_name, a.prod_brand, b.prod_price FROM tblproduct a, tblretprod b where a.prod_id = b.prod_id");

while($res=mysql_fetch_array($result))
{
$prod_name = $res['prod_name'];
$prod_brand = $res['prod_brand'];
$prod_price = $res['prod_price'];
}
?>
<html>
<title>Edit Product</title>
<body>
<a href="#">Home</a>
<br/><br/>
<form name="edit" method="post" action="edit.php">
<table border="0">
   <tr> 
    <td>Product Name</td>
    <td>
        <input disabled="disabled" type="text" name="prod_name" value=<?php echo $prod_name;?>>     </td>
  </tr>
  <tr> 
    <td>Brand</td>
    <td>
        <input disabled="disabled" type="text" name="prod_brand" value=<?php echo $prod_brand;?>>     </td>
  </tr>
<tr> 
    <td>Product Price</td>
    <td>
        <input type="text" name="prod_price" value=<?php echo $prod_price;?>>     </td>
  </tr>
<tr>
    <td><input type="hidden" name="prod_id" value=<?php echo $_GET['prod_id'];?>>    </td>
    <td><input type="submit" name="update" value="Update"></td>
  </tr>
</table>
</form>

</body>
</html>

same error message as above

can anyone please help me in defining the prod_id?

You'll need to select one of the prod_id in your query. You can't use it if it's not in the result.

i added b.prod_id to line

$result=mysql_query("SELECT a.prod_name, a.prod_brand, b.prod_price FROM tblproduct a, tblretprod b where a.prod_id = b.prod_id");

it solved out part of my problem

in the second part of the code, when you click on edit beside each product being displayed, a page is displayed where product name, product brand and price is displayed. mine is not displaying at all.

any help to sort this out please?

any help to sort this out please?

Show code for edit.php (if that's the problem).

in fact i changed the name of the file to updprod.php but forgot to change it in the code.

still the problem persists at the moment.

i verified the codes again. now my issue is that the product name, brand and price being displayed is the last one in the list. when i click on edit beside any product, the product name, brand and price being displayed is the last product. if i change the price to another value, my list and table in mysql updates for the product. my main issue is the name and brand not being displayed for the product as it should be when i click on edit.

i managed to solve the issue but now all products are being displayed instead of the one beside which i clicked the edit link. find below the new codes

include_once("db_connect.php");

if(isset($_POST['update']))
{
$prod_id = $_POST['prod_id'];

$prod_name=$_POST['prod_name'];
$prod_brand=$_POST['prod_brand'];
$prod_price=$_POST['prod_price']; 

// checking empty field
if(empty($prod_price))
{
//if name field is empty
if(empty($prod_price))
{
echo "<font color='red'>Price field is empty.</font><br/>";
}

} 
else
{ 
//updating the table
//$result=mysql_query("UPDATE tblretprod SET prod_price='$prod_price' WHERE prod_id=$prod_id");
$result=mysql_query("UPDATE tblretprod SET prod_price='".$prod_price."' WHERE prod_id='".$prod_id."';");

//redirectig to the display page. In our case, it is index.php
header("Location: update.php");
}
}
?>
<?php

$prod_id = $_GET['prod_id'];

$result=mysql_query("SELECT a.prod_name, a.prod_brand, b.prod_price FROM tblproduct a, tblretprod b where a.prod_id = b.prod_id") or die(mysql_error());

?>
<html>
<title>Edit Product</title>
<body>
<a href="#">Home</a>
<br/><br/>
<form name="edit" method="post" action="updprod.php">
<table border="0">
<?php
while($res=mysql_fetch_array($result))
{
$prod_name = $res['prod_name'];
$prod_brand = $res['prod_brand'];
$prod_price = $res['prod_price'];

?>
   <tr> 
    <td>Product Name</td>
    <td>
        <input type="text" disabled="disabled" name="prod_name[]" value="<?php echo $prod_name;?>"> </td>
  </tr>
  <tr> 
    <td>Brand</td>
    <td>
        <input type="text" disabled="disabled" name="prod_brand[]" value="<?php echo $prod_brand;?>">     </td>
  </tr>
<tr> 
    <td>Product Price</td>
    <td>
        <input type="text" name="prod_price[]" value="<?php echo $prod_price;?>">
        <input type="hidden" name="prod_id[]" value="<?php echo $_GET['prod_id'];?>">
             </td>
  </tr>
   <?php } ?> 
   <tr>
    <td><input type="submit" name="update" value="Update"></td>
  </tr>
</table>
</form>

</body>
</html>

i sorted it out. thanks all...

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.