-3
<?php
session_start();
function printTable() {
$server = "XXXXXXXX";
$user = "XXXXXXXXX";
$password = "XXXXXXXX";
$database = "XXXXXXXX";
$conn = mysql_connect($server, $user, $password);
mysql_selectdb($database, $conn);
$query = "SELECT Image, ISBN, Name, Vol, Release_date, publisher, price  FROM products p";
$resultset = mysql_query($query, $conn); // retrieve data from database
if ($resultset == null || $resultset == 1) {
    echo mysql_error(); // print SQL error
    die(); // exit PHP program
}
$numFields = mysql_num_fields($resultset);
echo "<table border=2 align=center><tr>";
echo "</tr>";
for ($i=0; $i<(mysql_num_rows($resultset)); $i++) { // print records
    $fields = mysql_fetch_row($resultset);
    echo "<tr>";
    echo "<tr class=$color><td><img width=100px src=$fields[0]></td>";
    echo "<td> Name: " . $fields[2] . " (vol.". $fields[3] . ")</br>";
    echo "<br> ISBN: " . $fields[1] . "</br>";
    echo "<br> Publisher: " . $fields[5] . "</br>";
    echo "<br> Release Date: " . $fields[4] . "</br>";
    echo "<br> Price: HK$ " . $fields[6] . "</td>";
    echo "<td><input type=\"submit\" value=\"Add to Cart\" onclick=\"combine($fields[1], '$fields[2]', $fields[3]);\"/></td>";
    $fields = mysql_fetch_row($resultset);
    if ($fields == null) break;
    echo "<td><img width=100px src=$fields[0]></td>";
    echo "<td> Name: " . $fields[2] . " (vol.". $fields[3] . ")</br>";
    echo "<br> ISBN: " . $fields[1] . "</br>";
    echo "<br> Publisher: " . $fields[5] . "</br>";
    echo "<br> Release Date: " . $fields[4] . "</br>";
    echo "<br> Price: HK$ " . $fields[6] . "</td>";
    echo "<td><input type=\"submit\" value=\"Add to Cart\" onclick=\"combine($fields[1], '$fields[2]', $fields[3]);\"/></td>";
    echo "</tr>";
}
echo "</table>";
}
mysql_close();
?>
<html>
<head>
<script>    
function combine(value1, value2, value3) {
alert (value1 + value2 + value3);
//setcookie(value1);
}
</script>
</head>
<title>
Product Page
</title>
<body>
<body style="background:#A2A2AE">
<h1> <center> Product Page </center> </h1>
<p><center>-----------------------------------------------------------------------------------------------------------------------------------------------------------</center></p>
<?php printTable(); ?>
</body></html>

how I can run the function addcookies() to save value 1 as the cookies... Thank you!

function addcookies(value) {
$pid = $_POST['pid'];
$expiry = time() + 60 * 60 * 24 * 30;

// Update the number of items
if(isset($_COOKIE['count']))
$count = $_COOKIE['count'];
else
$count = 0;

// Put the item into shopping cart
$key = "item: ".$count;
setcookie($key, $pid, $expiry);
setcookie("count", $count+1, $expiry);
}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

RileyTse
  • 1
  • 5

1 Answers1

0

First of all, your html code isn't correct. You oppened body tag 2 times and your title tag is out of the head tag, it's not even in the body.

As Biffen said in the comment, you cannot run the php after the page has loaded. Instead, you can use ajax. Ajax Documentation

You have to create a file with your php code inside it, and then execute it with ajax without reloading the page. And as I see you have written in a comment: "Put the item into shopping cart". Do not store shopping cart items in, neither, browser cookies nor session. Use your database instead because data stored in either cookies or session will be vulnerable.

valek
  • 1,365
  • 16
  • 27