Я делаю корзину покупок, и у меня проблема с отображением продуктов. Вот часть кода, где я думаю, проблема. Я использую xampp. Что-то не так с запросом? Пожалуйста, помогите, спасибо
<?php include("includes/db.php"); include("includes/functions.php"); if($_REQUEST['command']=='add' && $_REQUEST['productid']>0){ $pid=$_REQUEST['productid']; addtocart($pid,1); header("location:shoppingcart.php"); exit(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" / > <title>Products</title> <script language="javascript"> function addtocart(pid){ document.form1.productid.value=pid; document.form1.command.value='add'; document.form1.submit(); } </script> </head> <body> <form name="form1"> <input type="hidden" name="productid" /> <input type="hidden" name="command" /> </form> <div align="center"> <h1 align="center">Products</h1> <table border="0" cellpadding="2px" width="600px"> <? $result=mysql_query("select * from products"); while($row=mysql_fetch_array($result)){ ?> <tr> <td><img src="<?php echo $row['picture']; ?>" /></td> <td> <b><?php echo $row['name']; ?></b><br /> <?php echo $row['description']; ?><br /> Price:<big style="color:green"> $<?php echo $row['price']; ?></big><br /><br /> <input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row['serial']?>)" /> </td> </tr> <tr><td colspan="2"><hr size="1" /></td> <? } ?> </table> < /div> </body> </html>
[edit] Вот db.php, который включен в скрипт сверху:
<? @mysql_connect("localhost","root","") or die("Cannot connect"); @mysql_select_db("shopping") or die("Cannot connect"); session_start(); ?>
И вот функция functions.php также включена в скрипт из приведенного выше кода:
<? function get_product_name($pid){ $result=mysql_query("select name from products where serial=$pid"); $row=mysql_fetch_array($result); return $row['name']; } function get_price($pid){ $result=mysql_query("select price from products where serial=$pid"); $row=mysql_fetch_array($result); return $row['price']; } function remove_product($pid){ $pid=intval($pid); $max=count($_SESSION['cart']); for($i=0;$i<$max;$i++){ if($pid==$_SESSION['cart'][$i]['productid']){ unset($_SESSION['cart'][$i]); break; } } $_SESSION['cart']=array_values($_SESSION['cart']); } function get_order_total(){ $max=count($_SESSION['cart']); $sum=0; for($i=0;$i<$max;$i++){ $pid=$_SESSION['cart'][$i]['productid']; $q=$_SESSION['cart'][$i]['qty']; $price=get_price($pid); $sum+=$price*$q; } return $sum; } function addtocart($pid,$q){ if($pid<1 or $q<1) return; if(is_array($_SESSION['cart'])){ if(product_exists($pid)) return; $max=count($_SESSION['cart']); $_SESSION['cart'][$max]['productid']=$pid; $_SESSION['cart'][$max]['qty']=$q; } else{ $_SESSION['cart']=array(); $_SESSION['cart'][0]['productid']=$pid; $_SESSION['cart'][0]['qty']=$q; } } function product_exists($pid){ $pid=intval($pid); $max=count($_SESSION['cart']); $flag=0; for($i=0;$i<$max;$i++){ if($pid==$_SESSION['cart'][$i]['productid']){ $flag=1; break; } } return $flag; } ?>
с<? function get_product_name($pid){ $result=mysql_query("select name from products where serial=$pid"); $row=mysql_fetch_array($result); return $row['name']; } function get_price($pid){ $result=mysql_query("select price from products where serial=$pid"); $row=mysql_fetch_array($result); return $row['price']; } function remove_product($pid){ $pid=intval($pid); $max=count($_SESSION['cart']); for($i=0;$i<$max;$i++){ if($pid==$_SESSION['cart'][$i]['productid']){ unset($_SESSION['cart'][$i]); break; } } $_SESSION['cart']=array_values($_SESSION['cart']); } function get_order_total(){ $max=count($_SESSION['cart']); $sum=0; for($i=0;$i<$max;$i++){ $pid=$_SESSION['cart'][$i]['productid']; $q=$_SESSION['cart'][$i]['qty']; $price=get_price($pid); $sum+=$price*$q; } return $sum; } function addtocart($pid,$q){ if($pid<1 or $q<1) return; if(is_array($_SESSION['cart'])){ if(product_exists($pid)) return; $max=count($_SESSION['cart']); $_SESSION['cart'][$max]['productid']=$pid; $_SESSION['cart'][$max]['qty']=$q; } else{ $_SESSION['cart']=array(); $_SESSION['cart'][0]['productid']=$pid; $_SESSION['cart'][0]['qty']=$q; } } function product_exists($pid){ $pid=intval($pid); $max=count($_SESSION['cart']); $flag=0; for($i=0;$i<$max;$i++){ if($pid==$_SESSION['cart'][$i]['productid']){ $flag=1; break; } } return $flag; } ?>
-<? function get_product_name($pid){ $result=mysql_query("select name from products where serial=$pid"); $row=mysql_fetch_array($result); return $row['name']; } function get_price($pid){ $result=mysql_query("select price from products where serial=$pid"); $row=mysql_fetch_array($result); return $row['price']; } function remove_product($pid){ $pid=intval($pid); $max=count($_SESSION['cart']); for($i=0;$i<$max;$i++){ if($pid==$_SESSION['cart'][$i]['productid']){ unset($_SESSION['cart'][$i]); break; } } $_SESSION['cart']=array_values($_SESSION['cart']); } function get_order_total(){ $max=count($_SESSION['cart']); $sum=0; for($i=0;$i<$max;$i++){ $pid=$_SESSION['cart'][$i]['productid']; $q=$_SESSION['cart'][$i]['qty']; $price=get_price($pid); $sum+=$price*$q; } return $sum; } function addtocart($pid,$q){ if($pid<1 or $q<1) return; if(is_array($_SESSION['cart'])){ if(product_exists($pid)) return; $max=count($_SESSION['cart']); $_SESSION['cart'][$max]['productid']=$pid; $_SESSION['cart'][$max]['qty']=$q; } else{ $_SESSION['cart']=array(); $_SESSION['cart'][0]['productid']=$pid; $_SESSION['cart'][0]['qty']=$q; } } function product_exists($pid){ $pid=intval($pid); $max=count($_SESSION['cart']); $flag=0; for($i=0;$i<$max;$i++){ if($pid==$_SESSION['cart'][$i]['productid']){ $flag=1; break; } } return $flag; } ?>
-<? function get_product_name($pid){ $result=mysql_query("select name from products where serial=$pid"); $row=mysql_fetch_array($result); return $row['name']; } function get_price($pid){ $result=mysql_query("select price from products where serial=$pid"); $row=mysql_fetch_array($result); return $row['price']; } function remove_product($pid){ $pid=intval($pid); $max=count($_SESSION['cart']); for($i=0;$i<$max;$i++){ if($pid==$_SESSION['cart'][$i]['productid']){ unset($_SESSION['cart'][$i]); break; } } $_SESSION['cart']=array_values($_SESSION['cart']); } function get_order_total(){ $max=count($_SESSION['cart']); $sum=0; for($i=0;$i<$max;$i++){ $pid=$_SESSION['cart'][$i]['productid']; $q=$_SESSION['cart'][$i]['qty']; $price=get_price($pid); $sum+=$price*$q; } return $sum; } function addtocart($pid,$q){ if($pid<1 or $q<1) return; if(is_array($_SESSION['cart'])){ if(product_exists($pid)) return; $max=count($_SESSION['cart']); $_SESSION['cart'][$max]['productid']=$pid; $_SESSION['cart'][$max]['qty']=$q; } else{ $_SESSION['cart']=array(); $_SESSION['cart'][0]['productid']=$pid; $_SESSION['cart'][0]['qty']=$q; } } function product_exists($pid){ $pid=intval($pid); $max=count($_SESSION['cart']); $flag=0; for($i=0;$i<$max;$i++){ if($pid==$_SESSION['cart'][$i]['productid']){ $flag=1; break; } } return $flag; } ?>
-<? function get_product_name($pid){ $result=mysql_query("select name from products where serial=$pid"); $row=mysql_fetch_array($result); return $row['name']; } function get_price($pid){ $result=mysql_query("select price from products where serial=$pid"); $row=mysql_fetch_array($result); return $row['price']; } function remove_product($pid){ $pid=intval($pid); $max=count($_SESSION['cart']); for($i=0;$i<$max;$i++){ if($pid==$_SESSION['cart'][$i]['productid']){ unset($_SESSION['cart'][$i]); break; } } $_SESSION['cart']=array_values($_SESSION['cart']); } function get_order_total(){ $max=count($_SESSION['cart']); $sum=0; for($i=0;$i<$max;$i++){ $pid=$_SESSION['cart'][$i]['productid']; $q=$_SESSION['cart'][$i]['qty']; $price=get_price($pid); $sum+=$price*$q; } return $sum; } function addtocart($pid,$q){ if($pid<1 or $q<1) return; if(is_array($_SESSION['cart'])){ if(product_exists($pid)) return; $max=count($_SESSION['cart']); $_SESSION['cart'][$max]['productid']=$pid; $_SESSION['cart'][$max]['qty']=$q; } else{ $_SESSION['cart']=array(); $_SESSION['cart'][0]['productid']=$pid; $_SESSION['cart'][0]['qty']=$q; } } function product_exists($pid){ $pid=intval($pid); $max=count($_SESSION['cart']); $flag=0; for($i=0;$i<$max;$i++){ if($pid==$_SESSION['cart'][$i]['productid']){ $flag=1; break; } } return $flag; } ?>
Таблица продуктов:
CREATE TABLE IF NOT EXISTS `products` ( `serial` int(11) NOT NULL auto_increment, `name` varchar(20) collate latin1_general_ci NOT NULL, `description` varchar(255) collate latin1_general_ci NOT NULL, `price` float NOT NULL, `picture` varchar(80) collate latin1_general_ci NOT NULL, PRIMARY KEY (`serial`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=7 ; INSERT INTO `products` (`serial`, `name`, `description`, `price`, `picture`) VALUES (1, 'View Sonic LCD', '19" View Sonic Black LCD, with 10 months warranty', 250, 'images/lcd.jpg'), (2, 'IBM CDROM Drive', 'IBM CDROM Drive', 80, 'images/cdrom-drive.jpg'), (3, 'Laptop Charger', 'Dell Laptop Charger with 6 months warranty', 50, 'images/charger.jpg'), (4, 'Seagate Hard Drive', '80 GB Seagate Hard Drive in 10 months warranty', 40, 'images/hard-drive.jpg'), (5, 'Atech Mouse', 'Black colored laser mouse. No warranty', 5, 'images/mouse.jpg');
Сокращение эха зависит от конфигурации сервера. Попробуйте следующее, чтобы узнать, работает ли это, я обычно отклоняюсь от коротких тегов
<table border="0" cellpadding="2px" width="600px"> <?php $result=mysql_query("select * from products"); while($row=mysql_fetch_array($result)){ ?> <tr> <td><img src="<?php echo $row['picture']; ?>" /></td> <td> <b><?php echo $row['name']; ?></b><br /> <?php echo $row['description']; ?><br /> Price:<big style="color:green"> $<?php echo $row['price']; ?></big><br /><br /> <input type="button" value="Add to Cart" onclick="window.location='shoppingcart.php'" /> </td> </tr> <tr><td colspan="2"><hr size="1" /></td> <?php } ?> </table>
в<table border="0" cellpadding="2px" width="600px"> <?php $result=mysql_query("select * from products"); while($row=mysql_fetch_array($result)){ ?> <tr> <td><img src="<?php echo $row['picture']; ?>" /></td> <td> <b><?php echo $row['name']; ?></b><br /> <?php echo $row['description']; ?><br /> Price:<big style="color:green"> $<?php echo $row['price']; ?></big><br /><br /> <input type="button" value="Add to Cart" onclick="window.location='shoppingcart.php'" /> </td> </tr> <tr><td colspan="2"><hr size="1" /></td> <?php } ?> </table>
<table border="0" cellpadding="2px" width="600px"> <?php $result=mysql_query("select * from products"); while($row=mysql_fetch_array($result)){ ?> <tr> <td><img src="<?php echo $row['picture'] ?>" /> </td> <td> <b><?php echo $row['name'] ?></b><br /> <?php echo $row['description'] ?><br /> Price:<big style="color:green"> $<?php echo $row['price'] ?></big><br /><br /> <input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row['serial']?>)" /></td> </tr> <?php } ?> </table>
в<table border="0" cellpadding="2px" width="600px"> <?php $result=mysql_query("select * from products"); while($row=mysql_fetch_array($result)){ ?> <tr> <td><img src="<?php echo $row['picture'] ?>" /> </td> <td> <b><?php echo $row['name'] ?></b><br /> <?php echo $row['description'] ?><br /> Price:<big style="color:green"> $<?php echo $row['price'] ?></big><br /><br /> <input type="button" value="Add to Cart" onclick="addtocart(<?php echo $row['serial']?>)" /></td> </tr> <?php } ?> </table>