Forum Moderators: coopster

Message Too Old, No Replies

Help with Forms using PHP & MySQL

         

anirudha30

6:41 am on Sep 10, 2007 (gmt 0)

10+ Year Member



I am new to PHP. I am trying to design a simple shopping cart of cars. I have created a database named "tbl_items" using phpmyadmin.
following is the code of my 1st page:

<html>
<head>
<title> ..::Welcome to Car Shop::..</title>
</head>
<body>
<?php
mysql_connect("localhost", "root","") or die(mysql_error());
// echo "Connected to MySQL<br />";
mysql_select_db("db_shoppingcart") or die(mysql_error());
// echo "Connected to Database";
$result = mysql_query("SELECT * FROM tbl_items WHERE stock!='0';")
or die(mysql_error());

echo "<form action='preview.php' method='post'>";
echo "<table border='1' align='center' cellpadding='10'>";
echo "<tr><th colspan='8' align='center'>!Select Your Cars Here!</th></tr>";
echo "<tr> <th>Name</th> <th>Type</th> <th>Make</th> <th>Price</th> <th>Stock</th> <th>Select Quantity</th> <th>Click Here</th></tr>";

while($row = mysql_fetch_array( $result ))
{

echo "<tr><td align='center'>";
echo $row['Name'];
echo "</td><td align='center'>";
echo $row['Type'];
echo "</td><td align='center'>";
echo $row['Make'];
echo "</td><td align='center'>";
echo $row['Price'];
echo "</td><td align='center'>";
echo $row['Stock'];
echo "</td>";
echo "<td align='center'><select name='qty[]'>";
for($i=0;$i<=$row['Stock'];$i++)
{
echo "<option value=".$i.">".$i."</option>";
}
echo "</select></td><td align='center'><input type='checkbox' name=".$row['Name']." value=".$row['Name'].">";
echo "</td></tr>";

}
echo "<tr><td colspan='7' align='center'><input type='submit' name='submit' value='Preview!'></td></tr>";
echo "</table>";
echo "</form>";

?>
</body>
</html>

after submitting the form.....the user should be able to preview what he has selected and then should checkout,after which the user would be displayed the page where he has the total no. of items he has selected with their types and total cost.

Can anyone lend a helping hand?

jatar_k

1:17 pm on Sep 10, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld anirudha30,

>> the user should be able to preview what he has selected and then should checkout

what exactly are you looking for help with?

the preview page?
the checkout process?

what code do you have so far for those components?
is there a specific piece of code that is giving you a problem?

anirudha30

4:23 am on Sep 11, 2007 (gmt 0)

10+ Year Member



i am having problem with preview page..I am not able to display the table dynamically using Select..Where clause. Can we use user defined variables in the WHERE condition?
just take a look at my preview.php code:

</head>
<body>
<?php
mysql_connect("localhost", "root","") or die(mysql_error());
// echo "Connected to MySQL<br />";
mysql_select_db("db_shoppingcart") or die(mysql_error());
// echo "Connected to Database";

$item=$_POST["qty"];

$car=$_POST['check1'];
// print_r($car);
// echo $car;
echo "Order Processed";
echo "<br>";
echo "<br>";
$z = implode (",", $item);
print $z;
echo "<br>";
echo "<br>";
// echo "<pre>";
// print_r($item);
// echo "</pre>";
//echo "You have selected total"." ".array_sum($item)." "."car(s).";
echo "Selected Cars";
echo "<br>";
echo "<br>";
$a = implode (",", $car);
print $a;
// echo "<pre>";
// print_r($car);
// echo "</pre>";

$result = mysql_query("SELECT Name,Price FROM tbl_items WHERE Id='$a';")
or die(mysql_error());
echo "<br>";
echo "<br>";
echo "You have selected"." ".$a." ";
echo "<table>";
while($row = mysql_fetch_array( $result ))
{
echo "<tr><td align='center'>";
echo $row['Name'];
echo "</td>";
echo "<td align='center'>";
echo $row['Price'];
echo "</td></tr>";
}
// echo "<br>";
// echo "<br>";

echo "</table>";
?>
</body>
</html>

jatar_k

12:06 pm on Sep 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



so we are looking at this

$result = mysql_query("SELECT Name,Price FROM tbl_items WHERE Id='$a';")
or die(mysql_error());

does it die?
when you print $a do you see the value of $a?
is it the query or the while loop that isn't working?