hance2105 Posted June 28, 2013 Share Posted June 28, 2013 hi guys, i have 2 tables - tblproduct and tblretprod.i insert my product details to tbl product with prod_id being auto incrementother fields are prod_name, prod_brand, prod_desc, prod_photo, cat, subcati hv another table tblretprod with fields id, user_id, prod_id, prod_pricei can add my products successfullyi am displaying all the products i added in a table i echo along with a textfield to enter the product price and an add buttonon clicking the add button, user_id for the session, prod_id and prod_price should be inserted in the tblretproduser_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 everywherehere 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>"; ?> Quote Link to comment Share on other sites More sharing options...
Solution chriscloyd Posted June 29, 2013 Solution Share Posted June 29, 2013 (edited) You are selecting one product from tblproduct and you keep re-entering the same prod_id. What you need to do is on your form make a hidden value with the products id <?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; } header("Location: /" . $url); ?> Edited June 29, 2013 by chriscloyd Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted June 29, 2013 Share Posted June 29, 2013 You could even make it more fancy and if a product price didnt update you can pass numbers through the session insertsuccess like 0 if it does not fail and the prod_id if it fails so you can put and error next to the one that had the problem updating.... if you do not understand let me show you your script <?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'] = 0; } else{ $_SESSION['INSERTSUCCESS'] = $prod_id; } header("Location: /" . $url); ?> your form <?php //you can also make it that if the price is already set it overrides this one.... //I used sample id's ?> <form action='' method='post'> {Product Info Here} <input type="text" name="prod_price" value="" /> <input type="hidden" name="prod_id" value="37498" /> <input type="submit" name="prod_submit" value="add" /> <?php if($_SESSION['INSERTSUCCESS'] == '37498'){ echo 'Error Updating Price!'; } ?> Quote Link to comment Share on other sites More sharing options...
hance2105 Posted June 29, 2013 Author Share Posted June 29, 2013 the prod id is found in another table tblproduct...which i want to insert in tblretprod price and user id are being inserted to the table but not the relative prodid Quote Link to comment Share on other sites More sharing options...
hance2105 Posted June 29, 2013 Author Share Posted June 29, 2013 can smthng be done with the code i provided with the first thread? i tried the first one provided but the prod_id is not being sent to the database table tblretprod Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 29, 2013 Share Posted June 29, 2013 you missed this suggestion - What you need to do is on your form make a hidden value with the products id it's your form that is sending the prod_id and prod_price. how else would you know which product the retailer selected and entered his price for? Quote Link to comment Share on other sites More sharing options...
hance2105 Posted June 30, 2013 Author Share Posted June 30, 2013 never did this before so is this right? <input type="hidden" name="prod_id" value="' . $row['prod_id'] . '" /> and where i insert it? should i echo it here? while($row=mysql_fetch_assoc($sql)){ extract($row); echo "<tr>"; echo '<td><center><img height="100" width="100" src="Images/Products/'.$row['prod_photo'].'"/></td>'; echo "<td><center>".$row['prod_name']."</td>"; echo "<td><center>".$row['prod_brand']."</td>"; echo "<td>"."<form name=\"price\" method=\"post\" action=\"ret_addprod.php\">" . "<center><input type=\"text\" name=\"prod_price\" />" . "<input type=\"submit\" value=\"Add\" /></form>"."<td>"; Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted June 30, 2013 Share Posted June 30, 2013 (edited) while($row = mysql_fetch_assoc($sql)){ extract($row); //you are calling extract, but your not using it... //in you <img you should have an alt always. //that tells the google spider what the image is echo <<<EOT <tr> <td style="text-align: center;"> <img height="100" width="100" src="Images/Products/{$prod_photo}" alt="{$prod_name}" /> </td> <td style="text-align: center;"> {$prod_name} </td> <td style="text-align: center;"> {$prod_brand} </td> <td> <form name="price" method="post" action="ret_addprod.php"> <input type="hidden" name="prod_id" value="{$prod_id}" /> <input type="text" name="prod_price" /> <input type="submit" value="Add" /> </form> </td> </tr> EOT; } I just cleaned it up a little bit and threw in the hidden value, and used the extract function Edited June 30, 2013 by chriscloyd Quote Link to comment Share on other sites More sharing options...
hance2105 Posted June 30, 2013 Author Share Posted June 30, 2013 thanks for the help but i am using the deprecated SQL codes i know i have to learn the new one but my project nearly over so won't have time for that for now i tried to rewrite code this way but there is some error somewhere and am not able to find it 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>"; } some help would be a welcome guys Quote Link to comment Share on other sites More sharing options...
hance2105 Posted June 30, 2013 Author Share Posted June 30, 2013 am having this error Parse error: syntax error, unexpected 'text' (T_STRING), expecting ',' or ';' in C:\wamp\www\buysmart_site\retprod.php on line 44 line 44 is - echo "<td style="text-align: center;">".$row['prod_name']."</td>"; Quote Link to comment Share on other sites More sharing options...
hance2105 Posted June 30, 2013 Author Share Posted June 30, 2013 ok i managed to sort this one out i have below code but still only the first prod_id is being added everywhere 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>"; } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 30, 2013 Share Posted June 30, 2013 i have below code but still only the first prod_id is being added everywhere okay, so when you were debugging what your code is doing, what did you find out? where are the prod_id values what you expect and at what point do they all change to the first prod_id value? the error in your logic would be at the point where they all change to the first prod_id. Quote Link to comment Share on other sites More sharing options...
hance2105 Posted June 30, 2013 Author Share Posted June 30, 2013 well i have a products table with product details stored. i am echoing the product photo, name and brand from that table and am also echoing a text field with an add button to be able to insert the product prices. the prod_id values are stored in that products table. i have another table called tblretprod which will store user_id, the prod_id of the product being echoed and the price that will be set. for now, only the user_id of the user is being stored and the price i am inserting in the text field. but the prod_id being stored is only that of the first product am unable to know where the error is occurring for only the first prod_id is being inserted in the myql table Quote Link to comment Share on other sites More sharing options...
hance2105 Posted June 30, 2013 Author Share Posted June 30, 2013 hmmm am thinking if i wrote the add code well, can anyone please verify and let me know? perhaps this is the cause of my issue <?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>"; ?> Quote Link to comment Share on other sites More sharing options...
hance2105 Posted June 30, 2013 Author Share Posted June 30, 2013 the code was right before me but i was blind not to have seen it i was provided the solution but how cud i hv ignored that sorry....but a great thanks to u guys for the help provided this forum is really of great help Quote Link to comment Share on other sites More sharing options...
hance2105 Posted June 30, 2013 Author Share Posted June 30, 2013 hello guys instead of having an add button for each field, how can i have a single add button to add all data at one go instead of adding one by one? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.