0

i define below codes to show my products on website. I fetch my products details from Database.

When i click on the enquiry submit button it is not selecting the same id and inserting another product id in to database. Please see below codes which i use to send details in database using enquiry Submit Button

Please see below the updated codes, these are all codes which i am using to inserting data when clicking on enquiry submit button

<?php
session_start();
include'database.php';
$userid=$_SESSION['userid'];

$c=mysql_query("select 'productid','productprice' from products order by rand()");
while(list($productid,$productprice)=mysql_fetch_array($c)):
?>
<td align="center">
    <table class="newproducttd" align="center">
    <tr>
        <td class="code" align="center"><?php echo $productid; ?></td>
        <td class="price" align="center"><?php echo $productprice; ?></td>
    </tr>
    <tr>
        <td class="button" align="center"><input type="submit" class="enquiry" name="enquiry" value="ENQUIRY" /></td>
    </tr>    
    </table>
</td><br>
<?php
endwhile;
if($_REQUEST['enquiry'])
{
    mysql_query("insert into orders values('$userid','$productid','$productprice')");
}
?>
9
  • Where does $userid come from,should it not be (escaped) $_REQUEST['userid'] Commented Aug 2, 2013 at 10:45
  • $userid is coming from session. Its work like:- when $userid is blank it will goto reg.php otherwise will go head Commented Aug 2, 2013 at 10:49
  • have you checked that redirection is working? Commented Aug 2, 2013 at 10:52
  • yes everything is working, $userid is correct,query of inserting in db through enquiry is also correct the problem is "it is inserting different id". Like i have 4 products A, B, C, D. when i click on C's enquiry button it is inserting D's data. Commented Aug 2, 2013 at 10:56
  • And where's $productid coming from? - can you post more code? Commented Aug 2, 2013 at 10:56

2 Answers 2

1

Your table in your mysql database probably has the columns in a different order. To make sure, specify the columns in your insert query before specifying the values.

Sign up to request clarification or add additional context in comments.

4 Comments

my columns order is correct that why query is inserting data in db but with different id
@nikhilgarg the data can be inserted in the table even if the columns are not in the correct order. It is best to explicitly specify the columns in the query so that your code doesn't break if you add extra columns in your table... Anyway, try debugging by printing the sql statement before inserting to make sure the correct SQL is being executed. If not, you have to figure out why the $userid variable doesn't have data you expect it to have.
as i explained above to Johnny that everything is working, $userid is correct,query of inserting in db through enquiry is also correct the problem is "it is inserting different id". Like i have 4 products A, B, C, D. when i click on C's enquiry button it is inserting D's data.
Sorry man. You haven't explained it properly or given the full information for us to help you. You just need to debug each step properly, that is all I can say without further spoonfeeding. Good luck. @nikhilgarg
0

You're getting the wrong value for $productid because it's assigning the last value of it in your while loop, as your insertion code (and therefore your reference to it) is below that loop, you want to send the product id and price to the page accessible by $_REQUEST when the user clicks submit, to do this in your case you can use hidden form fields to store the values of each product, and a separate form for each element in your loop.

Also you want to put your submission code above your loop.

Depending where this data comes from you might want to escape it before inserting it in the database, I haven't in this example.

<?php
session_start();
include'database.php';
$userid=$_SESSION['userid'];

//check for submission and insert if set
if($_REQUEST['enquiry'])
{
    //use the userid session for userid, and submitted values for productid and productprice
    mysql_query("insert into orders values('$userid,'{$_REQUEST['productid']}','{$_REQUEST['productprice']}')");
}

$c=mysql_query("select 'productid','productprice' from products order by rand()");
while(list($productid,$productprice)=mysql_fetch_array($c)):
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<td align="center">
    <table class="newproducttd" align="center">
    <tr>
        <td class="code" align="center"><?php echo $productid; ?></td>
        <td class="price" align="center"><?php echo $productprice; ?></td>
    </tr>
    <tr>
        <td class="button" align="center"><input type="submit" class="enquiry" name="enquiry" value="ENQUIRY" /></td>
     //hidden form fields to store data to send to page 
     <input type="hidden" name="productid" value="<?php echo $productid;?>">
     <input type="hidden" name="productprice" value="<?php echo $productprice;?>">
    </tr>    
    </table>
  </form>
<?php
endwhile;
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.