More help needed!

Ok, so ErnieAlex here I am again :slight_smile:

If anyone wants to see what Im trying to do here have a look at this:
http://www.phphelp.com/forum/index.php?topic=16113.15

OK, so I am creating a dvd shop, when someone clicks on the buy now button beside every product I want their details to be sent to a transactions table. The transactions table will have the customerID, productID, product_name and price. Instead of them having to fill in a form, I am wanting the data to be sent to the table automatically when they click the buy button.

I will put the code below that is on the product page for each individual product.

Thanks.

[php]<?php
session_start();
if(!isset($_SESSION[ā€œmemberā€])){
header(ā€œlocation: memberlogin.phpā€);
exit();
}
$customerID = preg_replace (ā€™#[^0-9]#iā€™, ā€˜ā€™, $_SESSION[ā€œcustomerIDā€]); //filter everything but numbers and letters
$member = preg_replace (ā€™#[A-Za-z0-9]#iā€™, ā€˜ā€™, $_SESSION[ā€œmemberā€]); //filter everything but numbers and letters
$password = preg_replace (ā€™#[A-Za-z0-9]#iā€™, ā€˜ā€™, $_SESSION[ā€œpasswordā€]); //filter everything but numbers and letters
//check to see the url variable is set and it exists in database
if(isset($_GET[ā€˜idā€™])){
$id = preg_replace(ā€™#[^0-9]#iā€™, ā€˜ā€™, $_GET[ā€˜idā€™]);
include ā€œstorescripts/connect_to_mysql.phpā€;

$sql = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1");
$productCount = mysql_num_rows($sql);
if ($productCount > 0){
	while($row = mysql_fetch_array($sql)){
	$product_name = $row['product_name'];
	$price = $row['price'];
	$product_details = $row['product_details'];
	$category = $row['category'];
	$subcategory = $row['subcategory'];
	
	}
} else {
	echo "No product with that id";
	exit();
}

} else {
echo ā€œNo product with that idā€;
exit();
}

mysql_close();
?>

Store Homepage
<?php include_once("template_header.php"); ?>

<?php echo $product_name; ?>

Price: Only Ā£<?php echo $price; ?>

Category: <?php echo $category; ?>

Subcategory: <?php echo $subcategory; ?>

Product Details: <?php echo $product_details; ?>

ADD TO CART

Ā 


  <p>&nbsp; </p></td>
</tr>
<?php include_once("template_footer.php"); ?>
[/php]

Well, one error that jumps out at me is that you select products , limited to one (1). This means that there is only one row in the list. ( RowCount=1 )ā€¦ So, you do not need to do the while, just process the one row of info. It would make it faster. (Unless you are planning on allowing multiple products later onā€¦)

Now, I see the page display an item and has an HREF link to send it to a cart page. So, this will not post the data on the page that you already have. You should add a form to this page and have the prices and details of the one product that was displayed placed into fields. Then, these values would be available to the next page without having to use session variables. Basically, the next page, the cart.php page would re-read the values needed and post the info into the transaction table. Then, the cart.php file would redirect back to the products page to show more products.

Did that make sense to you, Michael??? If not, ask awayā€¦ If you get it, make your changes and ask if you get stuckā€¦ Good luckā€¦

Unfortunately no, I donā€™t really get it :frowning: Iā€™m sure you can guess now by the amount of questions I have asked that Iā€™m not the best at this. If it doesnā€™t bother you too much or take too much time, would you by any chance be able to post some code as an.example. if it takes too long then not to worry.

Thanks again , Michael

Well, looking at your code againā€¦ Assuming you want this to post correctly, I changed your HREF to a button.
And, added a form around the displayed section. Then, inserted some fields into the form around the data you were displaying. So, now this will pass the data onto the cart.php page. In that page, you have to read all of the data again and insert it into the database.

A couple of quick notesā€¦ First, in the cart section, you do not care about things like product description. (This can be pulled out of the database, so you would not pass this. You only need the info that is important to store the transaction. I see these as customerID, productID, price (this could change so it needs to be stored) and the date of the transaction. All other info you can pull from the tables. Some programmers store ALL the info into the transaction records. This would be because you could change a product price tomorrow, a product description or any other detailsā€¦ That is, of course your decision. And, these are the details that are important to figure out ahead of timeā€¦
And, the three you need for creating the transaction is customerID which is pulled from the session variable, the productid which is in the form $_POST[ā€˜productidā€™] and product price in the form $_POST[ā€˜productpriceā€™]ā€¦

So, here is the newer version, hope it helps!
[php]

<?php session_start(); if(!isset($_SESSION["member"])){ header("location: memberlogin.php"); exit(); } $customerID = preg_replace ('#[^0-9]#i', '', $_SESSION["customerID"]); //filter everything but numbers and letters $member = preg_replace ('#[A-Za-z0-9]#i', '', $_SESSION["member"]); //filter everything but numbers and letters $password = preg_replace ('#[A-Za-z0-9]#i', '', $_SESSION["password"]); //filter everything but numbers and letters //check to see the url variable is set and it exists in database if(isset($_GET['id'])){ $id = preg_replace('#[^0-9]#i', '', $_GET['id']); include "storescripts/connect_to_mysql.php"; $sql = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1"); $productCount = mysql_num_rows($sql); if ($productCount > 0){ while($row = mysql_fetch_array($sql)){ $product_name = $row['product_name']; $price = $row['price']; $product_details = $row['product_details']; $category = $row['category']; $subcategory = $row['subcategory']; } } else { echo "No product with that id"; exit(); } } else { echo "No product with that id"; exit(); } mysql_close(); ?>
<?php include_once("template_header.php"); ?>

<?php echo $product_name; ?>

Price: Only Ā£<?php echo $price; ?>

Category: <?php echo $category; ?>

Subcategory: <?php echo $subcategory; ?>

Product Details: <?php echo $product_details; ?>


<?php include_once("template_footer.php"); ?>
[/php] Just realized I reformatted a little of this to make it easier for me to read and understand. You can piece it back for you use however you wish. If you noticed, I just send the two items you needed by use of hidden fields. These are loaded in the values using the SQL data you pull earlier and they are just passed on thru the form. But, in the cart.php, you need to pull the hidden fields just like any other field to get that data. Have fun with it all...

Thanks for all your help.

But, Im still confused ???

Say I click to add a certain product with id as 7 and price as 19.99, then when I click the ā€œadd to cartā€ button, then it brings me to a blank page with url:

http://localhost/shop/cart.php?submit=ADD+TO+CART&productid=7&productprice=19.99

This all seems correct. But I really dont know what kind of thing I have to put in the cart.php page. What I would like is instead of an ā€œadd to cartā€ button to just have something like ā€œbuy nowā€, and then when they click the buy now button they will be asked to confirm they want to buy that particular product, and thats it bought. The data would then send to the database.

Im probably doing your head in now Alex, but any help is greatly appreciated ā€¦

Yes, now you have me mixed upā€¦

First, you talked about sending the product info and price to a ā€œcartā€. That means a shopping cart, which means that you are planning to have several items in the list on the shopping cart. (That is standard in the world)

Then, you mention:
http://localhost/shop/cart.php?submit=ADD+TO+CART&productid=7&productprice=19.99
Which is a URL that sends arguments, not, posted fields from a form. (Should pick one or the other)

Sooooo, recapping, I went back to your original post and I think I figured out what you need. Perhaps I was over thinking your request. You can use the original post and change this one line:

ADD TO CART


to something like this:
        <p><a href="cart.php?productid=<?php echo $id; ?>&productprice=<?php echo $price; ?>">BUY NOW!</a></p>

Then, inside of cart.php, you would use code something like this to access the dataā€¦
[php]
// Get id and price from Buy-Now buttonā€¦
$id = $_GET[ā€˜productidā€™];
$price = $_GET[ā€˜productpriceā€™];
$customer = $_SESSION[ā€œcustomerIDā€];
// Create SQL query INSERTing into cart the data needed for your usesā€¦
[/php]
Is that what you are looking for? Hopefully it will help. Sorry if I got you off-targetā€¦

Yeah thats it, Cheers.

Should be sorted now, but just lastly. I was gonna add a dateofpurchase field to the transactions table, but do you know how I could send the date to the database?

Thanks again

Oh, and one more thing definitely :):slight_smile:

I am wanting the product name to appear in database as well.

I have this in my cart.php
[php]// Get id and price from Buy-Now buttonā€¦
$id = $_GET[ā€˜idā€™];
$price = $_GET[ā€˜priceā€™];
$product_name = $_GET[ā€˜product_nameā€™];
$customer = $_SESSION[ā€œcustomerIDā€];

// Create SQL query INSERTing into cart the data needed for your usesā€¦
$sql = mysql_query(ā€œINSERT INTO transactions(id, price, product_name, customerID)VALUES(ā€™$idā€™,ā€™$priceā€™,ā€™$product_nameā€™,ā€™$customerIDā€™)ā€) or die (mysql_error());
echoā€™You have bought ā€™ . $product_name . ā€˜. You will be charged Ā£ā€™ . $price . ā€™ from your credit card.ā€™;
exit();[/php]

In my product page I have this as a line now:
[php]

BUY NOW!

Ā 


[/php]

But when I try to run this I am getting the following error:
Notice: Undefined index: product_name in C:\xampp\htdocs\store\cart.php on line 17
You have bought . You will be charged ƂĀ£1.00$product_name=Grand Theft Auto 4 from your credit card.

Great! Glad to hear that is behind usā€¦ Sorry I didnā€™t catch your needs earlierā€¦

So, yes on the datestamp. It is fairly simple. Just add the field to your database, letā€™s call it ā€œdateofpurchaseā€.
Make this a text fieldā€¦

So, to add this date to the table, when you insert the purchase into the database just add dateofpuchase and use this as the value: date(ā€˜Y-m-d H:i:sā€™)

This will save something like ā€œ2012-April-11-12:46:29ā€ to the database. Date/Time down to the second.
Very useful for going back and reviewing issues. Actually, I call this a timestamp as others do and I use it
for every (EVERY) part of my systems. I record transactions, log-ins, log-outs, page changes, everything
using this ā€œtimestampā€! In this way, if someone says, ā€œwell, I never went to your site!ā€, I can pull a log of
their IP, what day they were there, what pages they viewed, mostly everything. Very nice security for any
possible conflictsā€¦ Hope that helpsā€¦

*** Oh, just saw you posted againā€¦ Well, make sure on your product page that you put the product inside the variable $product_nameā€¦
NEVER MIND, found it: ice; ?>$product_n should not be a question mark, should be an ampersandā€¦ &

That should do it allā€¦ LOLā€¦ Good luck.

Got it all working.

Thanks again.

Just out of interest when someone buys a product this appears:

You have bought Grand Theft Auto 4. You will be charged ƂĀ£1.00 from your credit card.

Do you know what the symbol is before the Ā£ sign?

I looked back and I do not see your cart.php where it prints that line. Post it and we will see what is causing the odd extra character. I will guess you are printing a character that is uni-code version.
This is what I found:

The pound sign has Unicode code point U+00A3 Ā£ pound sign (inherited from Latin-1).[1] It has a HTML entity reference of Ā£ and has an XML decimal entity reference of Ā£.
So, to print it, you use Ā£ in HTML. So, replace your char with Ā£ and it should print normally...

Let me know if it doesnā€™t!

Sponsor our Newsletter | Privacy Policy | Terms of Service