More help needed!


#1

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]

#2

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…


#3

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


#4

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...

#5

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 …


#6

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…


#7

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


#8

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.


#9

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.


#10

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?


#11

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!