只有产品的第一个id被添加到mysql表中[关闭]
first time am using this, so i hope to have as much help as needed
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");
$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>";
?>
i tried this:-
$prod_id = $_POST['prod_id'];
// or
$prod_id = $_GET['prod_id'];
$sql1 = mysql_query("select * from tblproduct where prod_id='$prod_id'");
this didn't work either
what i want to do in short is that when the user fills in the text field with the product price and clicks on the add button, the relative user_id, prod_id of the product and the price entered are stored in tblretprod. user_id and price are being saved but not the relative prod_id
i am trying the below code and i know it can work but i don't know where there is an error in the code. Can anyone help me out please? I know am using a deprecated mysql codes but my project is nearly complete.
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'].'" alt="'.$row['prod
_name'].'" /></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'].'\" />.
"<center><input type=\"text\" name=\"prod_price\" />.
"<input type=\"submit\" value=\"Add\" /></form>"."</td>";
}
your not putting a filter on your tblproduct
query. So it will always be the first row of that table that will be inserted.
change $sql1 = mysql_query("select * from tblproduct");
to something like $sql1 = mysql_query("select * from tblproduct WHERE id = '$filter'");
You have to use while
loop to iterate all values.
while($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')");
}
In your current code you have used $row1 = mysql_fetch_array($sql1)
which will give you only 1st value not all values.
Note: Switch to MySQLi
OR PDO
because mysql_*
is deprecated.