0

hi guys, i have 2 tables - tblproduct and tblretprod.
i insert my product details to tbl product with prod_id being auto increment
other fields are prod_name, prod_brand, prod_desc, prod_photo, cat, subcat

i hv another table tblretprod with fields id, user_id, prod_id, prod_price

i can add my products successfully

i am displaying all the products i added in a table i echo along with a textfield to enter the product price and an add button

on clicking the add button, user_id for the session, prod_id and prod_price should be inserted in the tblretprod

user_id and prod_price are being inserted correctly, but the prod_id which is unique to each product is not being added. only the first prod_id i.e 1 is being added everywhere

here are my codes to add the product when i click on add

<?php

session_start();

include('db_connect.php');

$username = $_SESSION['username'];

$prod_price = $_POST['prod_price'];

$url='retprod.php';

$sql=mysql_query("select user_id from tbllogin where username = '$username'");

$row = mysql_fetch_array($sql);

$sql1 = mysql_query("select * from tblproduct where prod_id='$prod_id'");

$row1 = mysql_fetch_array($sql1);

$sql2 = mysql_query("insert into tblretprod (user_id, prod_id, prod_price) values ('$row[user_id]', '$row1[prod_id]', '$prod_price')");


    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
    echo "<script>alert('This product has been added successfully.')</script>";

?>
3
Contributors
9
Replies
30
Views
4 Years
Discussion Span
Last Post by sanbhu2105
0

The key info here is the product ID which is supposed to be stored in $_prod_id. But where do you get the value of $prod_id from (you use it in the query on line 17)?

0

the prod_id is stored in tblproduct
each product i am displaying in the table echoed have their respective prod_id
on clicking on the add button, all products are being added with prod_id = 1
i dnt know why

0

On line 17 you have a query:

$sql1 = mysql_query("select * from tblproduct where prod_id='$prod_id'");

The $prod_id that you are using in the query has not ben defined anywhere before.

I think the $prod_id should be assigned from POST. It should get into POST through a hidden field or through the add button.

Edited by broj1

0

if you dnt mind, how can i do that please?

earlier i tried to write in this code

$prod_price=$_POST['prod_price'];

this did not work...

0

The $prod_id that you are using in the query has not ben defined anywhere before.

Where is the product ID coming from?

0

Can you post the code of the previous page (the one with the table that triggers insertion).

0
<?php

session_start();

include('db_connect.php');

$sql = mysql_query("select * from tblproduct");

$num = mysql_num_rows($sql);

if($num>0){

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

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

while($row = mysql_fetch_assoc($sql)){
     extract($row); 

     echo "<tr>";
     echo '<td style="text-align:center;"><img height="100" width="100" src="Images/Products/'.$row['prod_photo'].'"/></td>';
      echo "<td style='text-align: center;'>".$row['prod_name']."</td>";
      echo "<td style='text-align: center;'>".$row['prod_brand']."</td>";
      echo "<td>"."<form name=\"price\" method=\"post\" action=\"ret_addprod.php\">".
        "<input type=\"hidden\" name=\"prod_id\" value=".$row['prod_id']." />".
        "<input type=\"text\" name=\"prod_price\" />".
        "<input type=\"submit\" value=\"Add\" /></form>"."</td>";

}
echo "</table>";
}

else{
    echo "No products found.";
}

?>

as requested please find below where the products from tblproduct are echoed along with a textfield and an add button

0

i sorted it out with below code

<?php

session_start(); 


include('db_connect.php'); 

$username = $_SESSION['username']; 

$prod_price = mysql_real_escape_string($_POST['prod_price']); 

$prod_id = mysql_real_escape_string($_POST['prod_id']);

$url = 'retprod.php'; 

$user = mysql_fetch_assoc(mysql_query("select user_id from tbllogin where username = '{$username}'"));

$insert = mysql_query("insert into tblretprod (user_id, prod_id, prod_price) values ('{$user['user_id']}', '{$prod_id}', '{$prod_price}')"); 

if($insert){

     $_SESSION['INSERTSUCCESS'] = true;

}
else{

     $_SESSION['INSERTSUCCESS'] = false;

}

    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
    echo "<script>alert('This product has been added successfully.')</script>";

?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.