Jump to content

Error at line $prod_price = $_POST['prod_price'];


Go to solution Solved by hance2105,

Recommended Posts

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]

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 by denno020

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

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

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?

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.";
}
 
?>

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"/>
  • Solution

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?

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]" ......>

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!

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.