Jump to content

Need Help with php and mysql


andrej13

Recommended Posts

Need help with php/sql. When I add a drink to the cart, only the quantity increases, but the item does not go to the cart. Check it out on my website http://fhcs.be/cart-demo2/

 

This is a part of my index.php

 

<h1>Drinks</h1>
<?
$Link = mysql_connect("xxxx","xxxx","xxxx");
$Query = "SELECT * FROM products";
$DBName = "xxxx";
$results = mysql_db_query($DBName, $Query, $Link);
?>
<select name = "drop1" size="1" id="drop1">
<Option Value=" ">Select Drink:</option>
<? //Begins PHP
for($u=0;$u<mysql_num_rows($results); $u++) {
$id=mysql_result($results,$u,'name');
?>
<option value="<? echo($id); ?>"><? echo($id); ?></option><? //Begins PHP
//Begins PHP
}
?>
</select>
<? //Begins PHP
if (mysql_db_query ($DBName, $Query, $Link)) {
}
else {
print ("FAILURE<BR>\n");
}
mysql_close ($Link);
?>
<a href="cart.php?action=add&id='.$row['id'].'">Add to cart</a> //button to add to chart

 

 

And this is a part of my cart.php

<?php
// Include MySQL class
require_once('inc/mysql.class.php');
// Include database connection
require_once('inc/global.inc.php');
// Include functions
require_once('inc/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;

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.