0

I've been having a rather irritating issue regarding capturing SQL information and then placing it into a PHP form (in theory, it should be kinda easy).

Here's the code for the SQL database information:

<?
$select = "SELECT * FROM beer WHERE country_id = 3";
$data = mysql_query($select) or die("Unable to connect to database.");

while($info = mysql_fetch_array($data)) {
echo '<center>';    
echo '<h2>'.$info['name'].'</h2>';
echo '<table style="padding:0px;"><tr>';
echo '<tr><td><b>ABV%:</b></td><td width="570">'.$info['abv'].'</td></tr>'; 
echo '<tr><td><b>Bottle Size:</b></td><td width="570">'.$info['bottleSize'].'</td></tr>'; 
echo '<tr><td><b>Case Size:</b></td><td width="570">'.$info['caseSize'].'</td></tr>'; 
echo '<tr><td><b>Price:</b></td><td width="570">$'.$info['price'].'</td>';
echo '</tr></table>';
echo '</center>';
echo '<br/>';

echo '<a href="'.$info['id'].'.php"><img src="" border="0"></a><br><br>';

echo '<form name="cart" method="post" action="cart.php"> <table border="0"> <tr>';
echo '<td><input type="hidden" name="bname" value="'.$info['name'].'"><input type="hidden" name="price" value="'.$info['price'].'"></td>';
echo '<td><b>Quantity:</b></td>'; 
echo '<td><input type="text" name="qty" size="3"></td>';
echo '<td><input type="submit" value="Add to Cart" a href="cart.php?name=foo&price=bar" /a></td>';
echo '</tr></table></form>';
}
?>

I want when the submit value is pressed to somehow transmit the price, quantity and name to a basic HTML form (so that all the user has to do is add name, address, etcetc). I am completely stumped on how to do this.

If anyone could help, it would be much appreciated.

5
  • do you want a form for every entry in the database or one form with all the entries? Commented Feb 28, 2012 at 20:49
  • So you're creating a separate form for every row you display and you want what to happen? Also, why do you have a link tag merged with your submit button? Commented Feb 28, 2012 at 20:50
  • I already have a form/PHP file I've made. I just want somehow (if possible) an entry for each item bought .. if that makes sense? Like an Amazon checkout. Commented Feb 28, 2012 at 20:56
  • you need to make your mind first. That's essential requirement. First of all you have to have exact HTML code you need. Without it it would make no sense to go for any PHP Commented Feb 28, 2012 at 21:01
  • I'm honestly just trying to figure out how to move input SQL data into a form on another page. Input QTY is then moved to a FORM which displays NAME/QTY/PRICE. Everything I've checked seems to be a form adding information to a SQL DB. Commented Feb 28, 2012 at 21:05

2 Answers 2

1

As you mentioned Amazon checkout, here is one thing you probably don't understand. Amazoin doesn't use the form to move items data between server and browser to and fro.
It is stored in a session on a server time. All you need is some identifier put into hidden field.

To use a session in PHP you need only 2 things:

  1. call session_start() function before any output to the browser on the each paghe where session needed.
  2. Use `$_SESSION variable.

That's all.

Say, page1.php

<?
session_start();
$_SESSION['var'] = value;

and page2.php

<?
session_start();
echo $_SESSION['var'];
Sign up to request clarification or add additional context in comments.

4 Comments

Right, I require sessions? Is this a tough thing to figure out? (I am quite new to PHP and this already has me pulling my hair out!)
Oh, no! Using sessions is extremely easy in PHP. As simple as just using a variable. Let me add an example to my answer.
Thank you very much :) I'll give it a shot now and see what happens!
Well I think it's done something different! However it doesn't seem to be pulling the data through correctly (I followed the session starts) Using: <input type='text' name='name' value='<?=$_POST['name']?>'> It just brings up a strange box with <?=$_POST[ inside, hah!
0

You wrote that code? because it's simply the same code as here.

You'll need to write an HTML form in your cart.php file and use the $_POST variable to show the values of the price , quanitity and name.

For example:

<form method='post'>
<input type='text' name='price' value='<?=$_POST['price']?>'>
<input type='text' name='quanitity' value='<?=$_POST['qty']?>'>

1 Comment

Yeah, wrote all the code myself (it's not hard to do some TD's and BR's). Also; that makes a hella lot more sense, I've been trying to do Sessions and $_POST for the past few days.

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.