Hi there. I am trying to create a simple DVD shopping cart using the latest
version of PHP on an Apache server and MySQL db. I can list all of the
titles from my db but want something that stores the fact that the user
wishes to purchase a particular DVD when they click on it.
I have just tried hacking a system which allows the user to select a radio
button and then selects an input button and this adds the dvdid to the
sessions variable. I also tried providing a link to a php processing page
which I wanted to contain the dvdid. I could get the link to work but it
didnt have a dvdid attached to it.
The session/radio button currently doesn't work but I have left what I
attempted in the code as there may be simple errors in it and may be clearer
as to what I am trying to achieve.
Any help would be great.
<?php
error_reporting(1); // suppress warning messages
session_start(); // Initialise session
session_register("trolleyContents"); // Register our variables
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-UK">
<head><title>PHP MySQL Query Form</title>
<meta http-equiv="Expires" content="Tue, 10 Sep 2002 00:00:00 GMT"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="Author" content="K.McManus"/>
<style>
body { background:#eeeebb }
tr { background:#ddeeff }
th { background:#ccddee }
td { background:#eeffee }
.idx { background:#ccddcc; text-align:right }
</style>
</head>
<body>
<h2>James' DVDs</h2>
<?php
require 'mysql.php';
function printError($errorMesg) {
printf("<br /> %s <br />\n", $errorMesg);
}
if ( !($link=mysql_connect($host, $user, $passwd)) ) {
printError(sprintf("Error connecting to database from %s, by user %s",
$host, $user));
} else {
mysql_select_db($dbName);
$query = 'SELECT * FROM dvd';
if ( !($result = mysql_query($query,$link)) ) {
printError(sprintf("Error %s : %s", mysql_errno(), mysql_error()));
} else {
if ( $result == 0 ) {
echo "Error " . mysql_errno() . ":" . mysql_error();
} else {
echo "<table bgcolor=\"ddeeff\"><thead><tr><th
bgcolor=\"eeddff\">Row</th>";
for ( $i = 0 ; $i < mysql_num_fields($result) ; $i++ ) {
echo "<th bgcolor=\"abcdef\">" . mysql_field_name($result,$i) .
"</th>\n";
}
echo "</tr></thead>\n<tbody>";
for ( $i = 0 ; $i < mysql_num_rows($result) ; $i++ ) {
echo "<tr><td bgcolor=\"eeddff\">" . ($i + 1) . "</td>";
$row = mysql_fetch_row($result);
for ( $j = 0 ; $j < mysql_num_fields($result) ; $j++ ) {
echo "<td> " . $row[$j] . "</td>";
}
?>
<FORM><form method="post" action="front.php?<?=SID?>">
<?PHP
echo "<td>
?>
<input type="radio" name="choice" value="Select"/>
<?php </td>";
echo "</tr>\n";
}
echo "</tbody></table>";
}
}
}
?>
<input type="submit" name="submit" value="Add to the trolley"/>
<input type="submit" name="clear" value="Empty the trolley"/>
</FORM>
<?php
if ((!$trolleyContents && !$choice) || $clear) {
$trolleyContents = "";
echo "<p>Trolley currently empty</p>";
} else {
if ( $choice ) $trolleyContents = $trolleyContents . "<li>$choice</li>";
echo "<p>Trolley contains:</p>";
echo "<ul>$trolleyContents</ul>";
}
?>
</body></html>