I have set up a shopping basket which is a session list is stored in a session array called ['list']. I want to extract the session data (information about list items) and insert them into a database table.
session_start();
Print_r ($_SESSION['list']);
if ( !isset($_SESSION['username']) && ($_SESSION['list']) )
{
header("Location:index.php");
exit();
}
Result:
Array ( [1] => Array ( [ISBN] => 0923184232 [bookname] => asodiaso [author] => lolstan [price] => 5.99 ) [2] => Array ( [ISBN] => 6677889900 [bookname] => The Templars Code [author] => C.M. Palov [price] => 18.99 ) )
^ I want this information inserted into a table! 🙂
By using this I can print off the session variables contained within the Array but what I need to do is to extract the variables and then insert them into another database table.
I have no idea how to go about extracting them so any suggestions?
I am familiar with how to insert information into the database tables (MYSQL).
The insert method looks like this:
$query = "INSERT INTO orderhistory VALUES ('','".$ISBN."','".$bookname."','".$author."','".$price."', '')";
At the moment my addtobasket.php looks like:
`
require "dbconn.php";
//Connects to database
$connect = mysql_connect($host, $user, $password )
or die ("Hey loser, check your server connection.");
//Make sure we are using the right database
mysql_select_db($database);
$ISBN = $_SESSION['list']['ISBN'];
$bookname = $_SESSION['list']['bookname'];
$author = $_SESSION['list']['author'];
$price = $_SESSION['list']['price'];
// Set up the query using the values that were passed via the URL from the form
$query = "INSERT INTO orderhistory
VALUES('','".$ISBN."','".$bookname."','".$author."','".$price."', '')";
Yes, I know it is wrong! :((
If you just want to dump the contents of your variable in a database field, you can use
serialize()orjson_encode()to convert your array to a string that you can easily convert back.By the way, I would strongly suggest switching to PDO and prepared statements to avoid the basic sql injection problems.