I understand basics of php and mysql but when it got too complicated I got a little lost.
To be specific, I used DW to create a login form which in db table contains userID, username, password, first name, last name etc.
I am building e commerce website and as to start I went all over the place to find and learn out of scripts that are in WWW how to build the "very basic store". I have another table with products which has product_id, product_name, price, category, subcategory, date_added.
I have fully functional cart.php which counts the total price etc. What I want to achieve is people being able to save their cart by their userID and when they come back and login into their session, they are able to find their products that they saved in their cart from last session but they never check out. Basically items "Saved For Later".
As far as I read people advice to create 1 more table with the same information just userID added into lets say new table 'savedcarthistory' that it would be assign to particular user that after saving the cart it would add all the info and assign it to the user. The questions that I am confused about is how to pull of the information assign to a particular userID?
To add and save cart I know I can use mysql query INSERT INTO 'savedcarhistoy' but I can't figure it out how to pull of the data that is assign for the user not including the user id but the product information that they saved to check out for later... What would be the best way to go about it and would someone point out mysql query syntax?
Any help would highly appreciated!
am glad i could offer some tips: to save the data in two different tables and display it by user id: 1. Learn the use of $_SESSION Like POST and GET; for each table you INSERT INTO using the Dreamweaver insert server behavior and hidden fields, make sure to capture the userid by::::::::::::::::::::
//1. in your page add hiddenfield to capture userID
<?php echo $_SESSION['MM_Username']; ?> //PHP to Caputure UserID
<input name="level" type="hidden" id="level" value="<?php echo $_SESSION['MM_Username']; ?>" />
//2. for other items create a recordset like product then also capture all items to first table and second tables like this:
<input name="level" type="hidden" id="level" value="<?php echo$row_passVar['accesslevel']; ?>" />
MySQL: In your recordset add a variable to pass the id of every user that is login so as to add the userID to all product items posted to those separate different tables.
1.SELECT *
FROM tb1, tb2
WHERE users.username=colname
Use the dreamweaver advanced recordset dialog box:
Create a variable like this:
Name: colname
Type: Text
Default Value:-1
RunTime value: $_SESSION['MM_username']
Finally remember to use a foreign key (FK) to relate the two tables together to generate a record output desire you need..
Hope this helps.
Please try to add some code you have done so far.
Anyway from what I understood, you want to insert the user_id and product_id to the table savedcarthistory.
Then you can fetch the details later from the user table by using user_id and from product_table using the product_id
Change insertion as below
// Adding product to db
$sql=mysql_query("INSERT INTO savedcarthistory (userID,product_id,price,details,category,subcategory,date_added)
VALUES ('$userID','$product_id','$price','$details','$category','$subcategory',now())") or die (mysql_error());
By this you will get the details of user table and products table by using the corresponding Ids
This is the session code after validating the the user is user.
<?php
if (!isset($_SESSION)) {
session_start();
}
$MM_authorizedUsers = "0,1";
$MM_donotCheckaccess = "false";
// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) {
// For security, start by assuming the visitor is NOT authorized.
$isValid = False;
// When a visitor has logged into this site, the Session variable MM_Username set equal to their username.
// Therefore, we know that a user is NOT logged in if that Session variable is blank.
if (!empty($UserName)) {
// Besides being logged in, you may restrict access to only certain users based on an ID established when they login.
// Parse the strings into arrays.
$arrUsers = Explode(",", $strUsers);
$arrGroups = Explode(",", $strGroups);
if (in_array($UserName, $arrUsers)) {
$isValid = true;
}
// Or, you may restrict access to only certain users based on their username.
if (in_array($UserGroup, $arrGroups)) {
$isValid = true;
}
if (($strUsers == "") && false) {
$isValid = true;
}
}
return $isValid;
}
$MM_restrictGoTo = "login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {
$MM_qsChar = "?";
$MM_referrer = $_SERVER['PHP_SELF'];
if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0)
$MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
$MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
header("Location: ". $MM_restrictGoTo);
exit;
}
?>
So what I need to do is that the session user with particular userID would be able to save their shopping cart and when they comeback they could click on the link and find their shopping items there. So for saving data I would do something like this, but it is for a separate product.
// Adding product to db
$sql=mysql_query("INSERT INTO savedcarthistory (userID,product_name,price,details,category,subcategory,date_added)
VALUES ('$userID','$product_name','$price','$details','$category','$subcategory',now())") or die (mysql_error());
My question would be how to add the whole cart information to the another table and how to display the cart to particular userID. Or what would be the best way to go about it? Would it be possible to save the whole cart as one column in db and display it by userID?
Dont know if I understood this correctly.
You are searching for a way how to store many producs for one customer right? You could set up a own encryption which could be saved into 1 filed of your database.
For example:
You got 3 products by ID and Quantity:
- ID:5 Q:2
- ID:3 Q:7
- ID:7 Q:1
Now go ahead and merge them together by seperating with symbols.
You string could be: '*5.2*3.7*7.1'
Save this string into your Database with the user ID. If the user comes back, get the string by seperating products by * and ID and quantity by '.'. You could use regex or split for this. Build up a loop that catches all your products out of the string and place them into an array or maybe a multidimensional-array (depending on the amout of data per product).
You can setup your string just as you want. Just be careful with using symbols. Maybe some simbols wont get accepted by the database (not sure about).
As you havnt provided any code I also cant go for it.
Good luck.