-
Posts
780 -
Joined
-
Last visited
Posts posted by mikebyrne
-
-
I assume I need a piece of sql around the purchase button something like:
UPDATE $table SET stockamount = stockamount - 1 WHERE ProductNo = $id
Just not sure how to code it correctly
-
My products table looks like this
CREATE TABLE `product` (
`Producttype` varchar(4) collate latin1_general_ci default NULL,
`ProductName` varchar(80) collate latin1_general_ci default NULL,
`ProductNo` double(50,0) NOT NULL auto_increment,
`Stockamount` decimal(5,0) default NULL,
`Display` varchar(3) collate latin1_general_ci default NULL,
`Description` varchar(1000) collate latin1_general_ci default NULL,
`Price` decimal(6,2) default NULL,
`Image` varchar(50) collate latin1_general_ci default NULL,
`Imgae2` varchar(50) collate latin1_general_ci default NULL,
PRIMARY KEY (`ProductNo`)
) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
-
I've setup a working shopping cart but wanted to add a "Purchase" button that would reduce the ammount of items purchased from my field Stocklevel
Cart.php
<?php // Include MySQL class require_once('mysql.class.php'); // Include database connection require_once('global.inc.php'); // Include functions require_once('functions.inc.php'); // Start the session session_start(); // Process actions $cart = $_SESSION['cart']; $action = $_GET['action']; switch ($action) { case 'add': if ($cart) { $cart .= ','.$_GET['id']; } else { $cart = $_GET['id']; } break; case 'delete': if ($cart) { $items = explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($_GET['id'] != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } $cart = $newcart; } break; case 'update': if ($cart) { $newcart = ''; foreach ($_POST as $key=>$value) { if (stristr($key,'qty')) { $id = str_replace('qty','',$key); $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart); $newcart = ''; foreach ($items as $item) { if ($id != $item) { if ($newcart != '') { $newcart .= ','.$item; } else { $newcart = $item; } } } for ($i=1;$i<=$value;$i++) { if ($newcart != '') { $newcart .= ','.$id; } else { $newcart = $id; } } } } } $cart = $newcart; break; } $_SESSION['cart'] = $cart; ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>PHP Shopping Cart Demo · Cart</title> <link rel="stylesheet" href="styles.css" /> </head> <body> <div id="shoppingcart"> <h1>Your Shopping Cart</h1> <?php echo writeShoppingCart(); ?> </div> <div id="contents"> <h1>Please check quantities...</h1> <?php echo showCart(); ?> <p><a href="login.php">Back to shop...</a></p> </div> </body> </html>
Functions.php
<?php function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p>You have no items in your shopping cart</p>'; } else { // Parse the cart session variable $items = explode(',',$cart); $s = (count($items) > 1) ? 's':''; return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>'; } } function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table>'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM product WHERE ProductNo = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr>'; $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $output[] = '<td>'.$ProductName.' by '.$Productinfo.'</td>'; $output[] = '<td>€'.$Price.'</td>'; $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; $output[] = '<td>€'.($Price * $qty).'</td>'; $total += $Price * $qty; $output[] = '</tr>'; } $output[] = '</table>'; $output[] = '<p>Grand total: <strong>€'.$total.'</strong></p>'; $output[] = '<div><button type="submit">Update cart</button></div>'; $output[] = '</form>'; } else { $output[] = '<p>You shopping cart is empty.</p>'; } return join('',$output); } ?>
-
Thats geat, thanks for your help!!
-
So what would i have to do to dispaly the information in product.php?
-
At present I have a searchfield that dispalys the result in searchresults.php. Is it possible to have the "ProductName" linked so that it would return the results in a page called product.php? Something like product.php?id= (id= being the ProductNo)
login
<form name="ex2" method="post" action="searchresults.php"> <select name="cat" onChange="setAction(this.options[this.selectedIndex].value);"> <option value="CD" selected="selected" >CDs</option> <option value="DVD" >DVDs</option> <option value="Game" >Games</option> </select> <input type="text" name="searchfield" size="22" maxlength="40" id="srchdrop" /> <input type="submit" name="searchme" value="Search Now! »" id="gosrch" /> </td></tr> </form> </table>
searchresults.php
<?PHP if (isset($_POST['searchme'])) { include("adminconnect.php"); $tbl_name = "product"; $cat = mysql_real_escape_string($_POST['cat']); $input =mysql_real_escape_string($_POST['searchfield']); $query = "SELECT * FROM product WHERE Producttype = '$cat' AND Productname LIKE '%$input%'"; $result = mysql_query($query) or die ("error in the query" . mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result)) { echo "Title ".$row['ProductName']."\n<br> <img src=\"".$row['Image']."\"></img>\n<br> Amount in stock ".$row['Stockamount']."\n<br> Price ".$row['Price']."\n<br> Description: ".$row['Description']."\n<br>"; } } else { echo "No search results found"; } } else { echo "No search terms entered."; } ?>
-
The output I want to use should look like this but I dont know how to apply it to my varibles
<tr class="alternate" id="pc1035249"> <td> <div class="quickview" onclick="new QuickView(10, 1035249, this);" title="Open product details"></div> <div class="prodimage" style="display:none;"> <a href="/detail_results_10.php?product_code=1035249"><img src="http://images-cache.cd-wow.com/images/../EUK/CD/awaiting75.jpg" alt="Music From Midnight Mass (Duffy, Liverpool Met. Brass Ens.)" /></a> </div> <a href="/detail_results_10.php?product_code=1035249" class="t11bb">VARIOUS COMPOSERS</a> </td> <td> <a href="/detail_results_10.php?product_code=1035249" class="t11ns">Music From Midnight Mass (Duffy, Liverpool Met. Brass Ens.)</a> </td> <td> <a href="/detail_results_10.php?product_code=1035249" class="t11br">EUR 14.94</a> </td> <td> <img src="http://images-cache.cd-wow.com/images/testdev/cdw_tdev_atc_v1_red.gif" class="cartimg" border="0" title="VARIOUS COMPOSERS - Music From Midnight Mass (Duffy, Liverpool Met. Brass Ens.) - Add to cart!" alt="VARIOUS COMPOSERS - Music From Midnight Mass (Duffy, Liverpool Met. Brass Ens.) - Add to cart!" height="16" width="60" onclick="window.open('product_warning.php?product_code=10:1035249&PAGE_REFERRER=%2Fsearchresults_cd.php%3FPHPSESSID%3D2c1671dd717974e685d46218c8fa862f%26searchfield%3Dduffy', '_blank','location=no,menubar=no,resizable=yes,scrollbars=no,toolbar=no,titlebar=no,width=360,height=520')" onmouseover="style.cursor='pointer';window.status='/?product_code=product_warning.php&PAGE_REFERRER=%2Fsearchresults_cd.php%3FPHPSESSID%3D2c1671dd717974e685d46218c8fa862f%26searchfield%3Dduffy'" onmouseout="window.status=''" /> </td> </tr>
-
At present I have a search facility that displays the results on a page called searchresults.php
login.php
<label for="searchfield" class="t10nw"></label><br /><br /><br /><br /><br /><br /> <form name="ex2" method="post" action="searchresults.php"> <select name="cat" onChange="setAction(this.options[this.selectedIndex].value);"> <option value="CD" selected="selected" >CDs</option> <option value="DVD" >DVDs</option> <option value="Game" >Games</option> </select> <input type="text" name="searchfield" size="22" maxlength="40" id="srchdrop" /> <input type="submit" name="searchme" value="Search Now! »" id="gosrch" /> </td></tr> </form>
searchresults.php
<?PHP if (isset($_POST['searchme'])) { include("adminconnect.php"); $tbl_name = "product"; $cat = mysql_real_escape_string($_POST['cat']); $input =mysql_real_escape_string($_POST['searchfield']); $query = "SELECT * FROM product WHERE Producttype = '$cat' AND Productname LIKE '%$input%'"; $result = mysql_query($query) or die ("error in the query" . mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result)) { echo "Title ".$row['ProductName']."\n<br> <img src=\"".$row['Image']."\"></img>\n<br> Amount in stock ".$row['Stockamount']."\n<br> Price ".$row['Price']."\n<br> Description: ".$row['Description']."\n<br>"; } } else { echo "No search results found"; } } else { echo "No search terms entered."; } ?>
Would it be possible to display only the resulting names and have links from these name that will display every detail like searchresults.php currently does.
The primary key in my table is ProductNo
An example of what I mean can be seen here:
http://www.cdwow.ie/searchresults_cd.php?searchfield=duffy
-
Problem solve. It was pointing to the wrong folder
-
Thats great!
My image files arent getting displayed though????
-
if fixed this line but still getting the error
$input = mysql_real_escape_string($_POST['searchfield']);
-
I've changed login.php to:
<input type="submit" name="searchme" value="Search Now! »" id="gosrch" />
searchresults
<?PHP if (isset($_POST['searchme'])) { $tbl_name = "product"; $cat = mysql_real_escape_string($_POST['cat']); $input = mysql_real_escape_string($_POST['searchfield']); $query = "SELECT * FROM product WHERE Producttype = '$cat' AND Productname LIKE '%$input%'"; $result = mysql_query($query) or die ("error in the query" . mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result)) { echo "Title ".$row['ProductName']."\n<br> <img src=\"".$row['Image']."\"></img>\n<br> Amount in stock ".$row['Stockamount']."\n<br> Price ".$row['Price']."\n<br> Description: ".$row['Description']."\n<br>"; } } else { echo "No search results found"; } } else { echo "No search terms entered."; } ?>
The problem now is im getting the error "No search terms entered." whatever I type into the searchfield
-
I have a serchfild on my page and want the results to be on my searchresults.php page
My code for login.php is
<label for="searchfield" class="t10nw"></label><br /><br /><br /><br /><br /><br /> <?PHP $tbl_name = "product"; $cat = mysql_real_escape_string($_POST['cat']); $input = mysql_real_escape_string($_POST['searchfield']); $query = "SELECT * FROM product WHERE Producttype = '$cat' AND Productname LIKE '%$input%'"; $result = mysql_query($query) or die ("error in the query" . mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result)) { $product = $row['product']; echo "$product was found<br>"; } } else { echo "No search results found"; } ?> <select name="cat" onChange="setAction(this.options[this.selectedIndex].value);"> <option value="cd" selected="selected" >CDs</option> <option value="dvd" >DVDs</option> <option value="game" >Games</option> </select> <form name="ex2" method="get" action="searchresults.php"> <input type="text" name="searchfield" size="22" maxlength="40" id="srchdrop" /> <input type="submit" value="Search Now! »" id="gosrch" /> </td></tr> </form> </table>
Searchresults.php
<?php print_r($_GET); echo $query; echo $product; echo "Title ".$row['ProductName']."\n<br> <img src=\"".$row['Image']."\"></img>\n<br> Amount in stock ".$row['Stockamount']."\n<br> Price ".$row['Price']."\n<br> Description: ".$row['Description']."\n<br>"; ?>
The sql for my product table is
CREATE TABLE `product` (
`Producttype` varchar(4) collate latin1_general_ci default NULL,
`ProductName` varchar(80) collate latin1_general_ci default NULL,
`ProductNo` double(50,0) NOT NULL auto_increment,
`Stockamount` decimal(5,0) default NULL,
`Display` varchar(3) collate latin1_general_ci default NULL,
`Description` varchar(1000) collate latin1_general_ci default NULL,
`Price` decimal(6,2) default NULL,
`Image` varchar(50) collate latin1_general_ci default NULL,
`Imgae2` varchar(50) collate latin1_general_ci default NULL,
PRIMARY KEY (`ProductNo`)
) ENGINE=MyISAM AUTO_INCREMENT=17 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
If I type Duffy into the searchfield I get the output
Array ( [searchfield] => Duffy ) Title
Amount in stock
Price
Description:
As you can see the varibles dont seem to have passed
-
It has to be something to do with the placment of the php rather than the actual code?
-
No, still getting the same error and tried Everything!!!!
I can pm people my complete code if you'd like to take a look?
-
Well it would be empty because the page wont load it just give me the error messege
-
Ive tried this but it doesnt seem to be showing the varible but the page wont load so the problem seems to be with the line
$query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'";
<?PHP include("adminconnect.php"); $tbl_name = "product"; $cat = mysql_real_escape_string($_POST['cat']); $input = mysql_real_escape_string($_POST['searchfield']); $query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'"; $result = mysql_query($query) or die ("error in the query" . mysql_error()); echo $query; if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array) { $product = $row['product']; echo "$product was found<br>"; } } else { echo "No search results found"; } ?> <select name="cat" onChange="setAction(this.options[this.selectedIndex].value);"> <option value="cd" selected="selected" >CDs</option> <option value="dvd" >DVDs</option> <option value="game" >Games</option> </select> <form name="ex2" method="get" action="searchresults.php"> <input type="text" name="searchfield" size="22" maxlength="40" id="srchdrop" /> <input type="submit" value="Search Now! »" id="gosrch" /> </td></tr> </form> </table>
-
Would that be echo "['$query']";?
-
Im getting the error and cant spot my mistake
error in the queryYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIKE '%%'' at line 1
<?PHP include("adminconnect.php"); $tbl_name = "product"; $cat = mysql_real_escape_string($_POST['cat']); $input = mysql_real_escape_string($_POST['searchfield']); $query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'"; $result = mysql_query($query) or die ("error in the query" . mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array) { $product = $row['product']; echo "$product was found<br>"; } } else { echo "No search results found"; } ?> <select name="cat" onChange="setAction(this.options[this.selectedIndex].value);"> <option value="cd" selected="selected" >CD</option> <option value="dvd" >DVD</option> <option value="game" >Game</option> </select> <form name="ex2" method="get" action="searchresults.php"> <input type="text" name="searchfield" size="22" maxlength="40" id="srchdrop" /> <input type="submit" value="Search Now! »" id="gosrch" /> </td></tr> </form>
-
Im getting the error
error in the queryYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIKE '%%'' at line 1
<?PHP include("adminconnect.php"); $tbl_name = "product"; $cat = mysql_real_escape_string($_POST['cat']); $input = mysql_real_escape_string($_POST['searchfield']); $query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'"; $result = mysql_query($query) or die ("error in the query" . mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array) { $product = $row['product']; echo "$product was found<br>"; } } else { echo "No search results found"; } ?> <select name="cat" onChange="setAction(this.options[this.selectedIndex].value);"> <option value="cd" selected="selected" >CD</option> <option value="dvd" >DVD</option> <option value="game" >Game</option> </select> <form name="ex2" method="get" action="searchresults.php"> <input type="text" name="searchfield" size="22" maxlength="40" id="srchdrop" /> <input type="submit" value="Search Now! »" id="gosrch" /> </td></tr> </form>
-
When login.php loads i get the error
arning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\MainPage\login.php on line 136
No search results found
Line 136 is:
if (mysql_num_rows($result) > 0) {
-
Well at present searchresults.php is blank
so if I wanted to echo out the array, would it be?
<?PHP
print_r($_GET[$row]);
print_r($_Get[$product]);
?>
-
At present ive coded a serchfield on my login page which should pass the results to searchresults.php. I presume i need to use the $_GET [Result];
I'm just not sure how to code searchresults.php to see whats getting passed over
login.php
<table width="100%" cellpadding="0" cellspacing="0" border="0" class="b1sw2" style="background:url(Pictures/searchbox_bg.gif) no-repeat #003;"> <col width="1%"> <col width="99%"> <tr> <td> </td> <td> <label for="searchfield" class="t10nw"></label><br /><br /><br /><br /><br /><br /> <?PHP include("adminconnect.php"); $tbl_name = "product"; $cat = mysql_real_escape_string($_POST['cat']); $input = mysql_real_escape_string($_POST['searchfield']); $query = "SELECT * FROM $tbl_name WHERE $cat LIKE '%$input%'"; $result = mysql_query($result); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array) { $product = $row['product']; echo "$product was found<br>"; } } else { echo "No search results found"; } } ?> <select name="cat" onChange="setAction(this.options[this.selectedIndex].value);"> <option value="cd" selected="selected" >CDs</option> <option value="dvd" >DVDs</option> <option value="game" >Games</option> </select> <form name="ex2" method="get" action="/searchresults_cd.php"> <input type="text" name="searchfield" size="22" maxlength="40" id="srchdrop" /> <input type="submit" value="Search Now! »" id="gosrch" /> </td></tr> </form> </table>
-
I know, no problem.
Any idea how I can ger $result to show on show on searchresults.php??
[SOLVED] Help with registration form
in PHP Coding Help
Posted
Im creating a registration for and im not sure how to get vaible from the various dropdown boxes ie Country, Birthdate, Birthmonth, Birthdate and Sex
The code I have so far is