hance2105 Posted June 28, 2013 Share Posted June 28, 2013 hello guys, error undefined_variable prod_price when submitting the price value inserted in a text field <?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 prod_id 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>"; ?> the text field has been echoed in a table using below code [code]echo "<td> Rs ".'<input type="text" name="prod_price"/></td>'; [/code] Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/ Share on other sites More sharing options...
denno020 Posted June 28, 2013 Share Posted June 28, 2013 (edited) With the lack of error information that you've given, I can only guess that the variable $prod_price isn't being set to the value from the form. Either the value hasn't come through the form correctly, or the field has a different name. You can try this to see if that's the source of the problem: if(isset($_POST['prod_price'])){ $prod_price = $_POST['prod_price']; }else{ throw new \Exception("prod_price wasn't set correctly in the form"); } Give that a try.. Edited June 28, 2013 by denno020 Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/#findComment-1438332 Share on other sites More sharing options...
hance2105 Posted June 28, 2013 Author Share Posted June 28, 2013 below error message is displayed when i try that Fatal error: Uncaught exception 'Exception' with message 'prod_price wasn't set correctly in the form' in C:\wamp\www\buysmart_site\ret_addprod.php on line 12 ( ! ) Exception: prod_price wasn't set correctly in the form in C:\wamp\www\buysmart_site\ret_addprod.php on line 12 Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/#findComment-1438336 Share on other sites More sharing options...
hance2105 Posted June 28, 2013 Author Share Posted June 28, 2013 well the thing is like that, when i am querying my table and displaying the results in a table...along with that am echoing a textfield where i can insert price of my product and when i click on the add button it adds to the database i created the file above to add the product price to the table..user id and prod id are being inserted but not the price Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/#findComment-1438338 Share on other sites More sharing options...
denno020 Posted June 28, 2013 Share Posted June 28, 2013 So the Faral Error that was just thrown is exactly what I had in my code, so that means $_POST['prod_price'] isn't set. You will need to check the id of the text field that you're putting the price into. Can you show that code here? Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/#findComment-1438339 Share on other sites More sharing options...
hance2105 Posted June 28, 2013 Author Share Posted June 28, 2013 here it is - <?php session_start(); include('db_connect.php'); $sql = mysql_query("select * from tblproduct"); $num = mysql_num_rows($sql); if($num>0){ echo "<center><table 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 "<td><b><center>Action</td>"; echo "</tr>"; 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><center> Rs ".'<input type="text" name="prod_price" id="prod_price"/></td>'; echo "<td><center><a href=\"ret_addprod.php?id=$row[prod_id]\">Add</a></td>"; } echo "</table>"; } else{ echo "No products found."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/#findComment-1438340 Share on other sites More sharing options...
denno020 Posted June 28, 2013 Share Posted June 28, 2013 You need to put your input field inside form tags, give that form and make sure that you set the method for that form as POST Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/#findComment-1438341 Share on other sites More sharing options...
hance2105 Posted June 28, 2013 Author Share Posted June 28, 2013 then it should be like this echo "<td><center> Rs ".'<form method="post"><input type="text" name="prod_price" id="prod_price"/></form></td>'; Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/#findComment-1438342 Share on other sites More sharing options...
denno020 Posted June 28, 2013 Share Posted June 28, 2013 Put the add button inside the form as well. You'll obviously sacrifice putting the add button in it's own table row, but it'll make it easier to submit the form, without having to play around with javascript. And also, make the Add button an actual button, and not a link.. (unless you really want to keep it a link, in which case you'll need some javascript to be able to submit the form). So the add button will be like this <input type="submit" value="add" id="add"/> Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/#findComment-1438344 Share on other sites More sharing options...
Solution hance2105 Posted June 28, 2013 Author Solution Share Posted June 28, 2013 woowww that issue is solved now with the following code 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>"; now my issue is that when i insert the product price and click on add, the add page is displayed again how can i display the page with the field of the product that has already been added is disabled so that the same product cannot be added again? Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/#findComment-1438346 Share on other sites More sharing options...
denno020 Posted June 28, 2013 Share Posted June 28, 2013 I'm not really sure what you mean, but if you just want it to go to the same page as it was before, when the price wasn't being added, then just make the action of the form the same as your href that you had original for your add link. So <form action="ret_addprod.php?id=$row[prod_id]" ......> Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/#findComment-1438350 Share on other sites More sharing options...
hance2105 Posted June 28, 2013 Author Share Posted June 28, 2013 hmmmm ok....can i create a generic add button? i mean all fields for prices are filled and i have only one add button which when clicked inserted all the data at once to my sql table? Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/#findComment-1438352 Share on other sites More sharing options...
hance2105 Posted June 28, 2013 Author Share Posted June 28, 2013 well what i want is to disable the text field after the product has been added can that be done with php? Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/#findComment-1438365 Share on other sites More sharing options...
denno020 Posted June 28, 2013 Share Posted June 28, 2013 Oh sorry didn't see that you had replied.. You wouldn't do that with PHP, you would do that with javascript.. As for a generic add button, you could do that. If you surround your entire table with the form tags and place your add button at the bottom of the table. Then you will have to loop through each text field in your php once you submit the form (by pressing the add button). (I'm intentionally being a little bit vague with instructions on how to do things, as I want to try and get you going first, and then I'll help you with whatever code you come up with ). Hopefully that will give you the right idea! Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/#findComment-1438366 Share on other sites More sharing options...
hance2105 Posted June 28, 2013 Author Share Posted June 28, 2013 ok i will look into that...just let me know if something....i dnt want to delete records from my table...i want to disable the record so that it is not involved in any mysql select statements that possible? well to be truthful i dnt know how to do that Quote Link to comment https://forums.phpfreaks.com/topic/279655-error-at-line-prod_price-_postprod_price/#findComment-1438370 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.