Jump to content

Recommended Posts

hi ,

 

i have a running shopping cart and i need to save the data in the cart to a orders database. the items in the cart are stored in a session by their id and then are displayed in the cart by sending a query to the items database finding their name, price and product code.

 

how can i put the carts data into a db called orders.

 

the items are stored into the session by...

 

 

PHP Code:

<? session_start(); 

if ( !isset( $_SESSION["cart"] ) ) { 
    $_SESSION["cart"] = array(); 
} 

$id = $_POST["id"]; 
$_SESSION["cart"][$id] = $id; 

header("location:index.php"); 

?> 

 

thanks a lot

ben

I asume you build the part where a costomer has to register and log in too.

You just make the user log in on his account then let him/her proceed on the shopping cart.

once the user wants to check out you just insert a record into a order table.

 

Things that are unclear are:

 

  • if you have experience with sql what kind of sql your using
  • if you have the user log on system
     

tell these things and you will get better help

then all your looking for is something like this:

<?php
        // make common variables for the session variables
        $id = $_POST["id"]; 
        $_SESSION["cart"][$id] = $id; 
        $common_variable = $_SESSION["cart"][$id];

$rs = mysql_connect( "localhost", "username", "password" );
$rs = mysql_select_db( "database" );

// SQL query  
$sql = "INSERT INTO `table` " . $common_variable . " WHERE id=$user_id";
echo $sql; // echo it for debugging purpose's and to make sure everything is ok.
$rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());

 

or somthing along those lines, MySQL queries are exactly the same when you use session variables, you can just put the session variables into common variables and then INSERT into the database.

 

Regards ACE

ok thanks....

 

 

i have modified it to suit, the id i dont think i need as the purpose of the script is to save the shopping cart into a db so the order can be completed.

 

<?
session_start();

$common_variable = $_SESSION["cart"];

$rs = mysql_connect( "xxx", "xxx", "xxx" );
$rs = mysql_select_db( "xxx" );

// SQL query  
$sql = "INSERT INTO `orders` " . $common_variable . " WHERE order_id=1";
echo $sql; // echo it for debugging purpose's and to make sure everything is ok.
$rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());

header("location:pay.php");

mysql_close($link );

?>

 

 

though i get

 

 

INSERT INTO `orders` WHERE order_id=1Query:

INSERT INTO `orders` WHERE order_id=1

 

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE order_id=1' at line 1

 

Thanks

sorry my fault had : from where i had copied and pasted it - stupid me !

 

but i still get this error...

 

INSERT INTO orders WHERE order_id='1' Query:

INSERT INTO orders WHERE order_id='1'

 

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE order_id='1'' at line 1

 

 

This is the code

 

<?
session_start();

$common_variable = $_SESSION["cart"];

$rs = mysql_connect( "xxx", "xxxx", "xxxx" );
$rs = mysql_select_db( "btronics" );


$sql = "INSERT INTO orders " . $common_variable . " WHERE order_id='1' ";
echo $sql; // echo it for debugging purpose's and to make sure everything is ok.
$rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());

header("location:pay.php");

mysql_close($link );

?>

try this:

INSERT INTO `orders` WHERE `order_id`='1' 

 

and change your MySQL info into a asterix's(*)  ;)

 

EDIT: I know what we've done wrong here. We've got INSERT INTO, then we go to WHERE, we are missing a part here lol.

 

<?php
$sql = "INSERT INTO `orders` (colum_your_inserting_into) VALUES ('$common_variable') WHERE order_id='1';

that should work then.

 

Regards ACE

just copy and paste the $sql variable over your current one, and for the first pair of brackets, put in the name of the column you are inserting into with single comma's on either side 'column_name'

 

so if your column is called 'order_number'

you would do this:

<?php
$sql = "INSERT INTO `orders` ('order_number') VALUES ('$common_variable') WHERE order_id='1';

 

I'll break it down for you. INSERT INTO 'what table you are editing', ('column') 'what column of the table you are editing' VALUES () 'what is actually being inserted into that column' WHERE 'who's account or particular entry in the table you are editing'

 

EDIT: is their any entries in this table in the moment?

 

Regards ACE

hi yes i understud that i messed the time up as i edited the db login infov lol!

 

is it possible you could give me the complete script i think i have a error somewhere

 

the table is orders and the colum is items

 

thanks for your help m8  ;D

<?php
session_start();

$common_variable = $_SESSION["cart"];

$rs = mysql_connect( "xxx", "xxxx", "xxxx" );
$rs = mysql_select_db( "btronics" );


$sql = "INSERT INTO `orders` ('items') VALUES ('$common_variable') WHERE order_id='1'";
echo $sql; // echo it for debugging purpose's and to make sure everything is ok.
$rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());

//  header("location:pay.php"); // commented out for now while we debug

mysql_close($rs ); // were closing the wrong connection, $link doesn't exist lol

?>

 

give that a go, leave the header function commented out for now. And tell me what is echo'd from $sql.

 

EDIT: I missed out a double comma sorry, copy and paste this code again.

INSERT INTO `orders` ('items') VALUES ('Array') WHERE order_id='1'Query:

INSERT INTO `orders` ('items') VALUES ('Array') WHERE order_id='1'

 

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''items') VALUES ('Array') WHERE order_id='1'' at line 1

we dont need where because i need it to creat a new one!

 

i now get

 

INSERT INTO `orders` ('items') VALUES ('Array')Query:

INSERT INTO `orders` ('items') VALUES ('Array')

 

Error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''items') VALUES ('Array')' at line 1

i thinks its how the id is saved when an item is added to the carts session???

this is my total code now

 

add to cart

 

<? session_start();

if ( !isset( $_SESSION["cart"] ) ) { 
    $_SESSION["cart"] = array(); 
} 

$id = $_POST["id"]; 
$_SESSION["cart"][$id] = $id; 

header("location:index.php"); 

?> 

 

 

then the order bit

 

<?php
session_start();

$common_variable = $_SESSION["cart"];

$rs = mysql_connect(xxx);
$rs = mysql_select_db(xxx );


$sql = "INSERT INTO orders ('items') VALUES ('$common_variable')";
echo $sql; // echo it for debugging purpose's and to make sure everything is ok.
$rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());

//  header("location:pay.php"); // commented out for now while we debug

mysql_close($rs ); // were closing the wrong connection, $link doesn't exist lol

?>

done a bit of editing

 

now get this

 

INSERT INTO orders (items) VALUES ('Array')

 

<?php
session_start();

$common_variable = $_SESSION['cart'];

$rs = mysql_connect( xxx);
$rs = mysql_select_db(xxx);


$sql = "INSERT INTO orders (items) VALUES ('$common_variable')";
echo $sql; // echo it for debugging purpose's and to make sure everything is ok.
$rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error());

//  header("location:pay.php"); // commented out for now while we debug

?>

ok, lets try a couple tests.

 

on the order page.

type this after $common_variable = $_SESSION["cart"];

<?php // leave out the <?php of course, just for displaying code correctly on the forum ^^
echo $common_variable;
echo "<br>";

 

then comment out all $rs variables on the page. And comment out mysql_close at the bottom their.

 

and then hopefully, it will display what is in the session variable and what is in $sql, if both those variables are holding the correct values, we'll move onto the next step.

Array

INSERT INTO orders (items) VALUES ('Array')

 

this is displayes, there is no connection problem because in my mysql controlpanel their are entries with the word array in the items colum!

 

this is where i am testing the code

 

http://tom.btronics.co.uk/cart/order.php

 

Thanks

 

 

hmmm, this one has me stumped lol

 

ok, uncomment everything, except for the header() function.

 

what exactly is it suppose to put into the database? I know $_SESSION["cart"] is going into the database, but what is your expected result?

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.