문제

I'm stuck with this and I think I'm pretty close but the idea just wont come up! I have searched a lot but I can't find anyone with the solution for my specific situation.

I'm making a shopping cart in php with mysql. It's a simple one, you loggin, then go to the store, click the button 'Add to Cart' and the items appear on a gridview with the price and below it, you see the total of the purchase. I'm having problems when I need to save multiple lines of data (saleid, username, itemid, etc) from the current session to the database, I just don't know why I can't figured it out, because it's (kinda) the same method of when the user registers in the page.

I'm thinking of making a for loop that saves the content of eache SESSION row into the database, like these (it's just an example, I'm awhate that this might be incorrect)

if (mysql_num_rows($result)!=0){
while ($_SESSION = mysql_fetch_array($result)){
    $array[]= $_SESSION;

    for($i=,$i<$array_rows;$i++){
    mysql_query ("INSERT INTO purchase(username,gameid,amount_purchased) VALUES
    ('".$array[$i]."', '".$array[$i]."', '".$array[$i].")");
    $array[$i]=$i+1;
    }
}

}

username, gameid, purchasedamount are just some examples! I'm really sorry is this is a stupid question, but I find .php really hard to understand :/

도움이 되었습니까?

해결책

If you store the values of the cart in an a session variable, lets say $_SESSION['cart'] to save the data from the session cart to the database you would normally do something like

    $data = $_SESSION['cart'];

if(empty($data)) return false;

$table = 'purchase';
$implode_values  = array();
$implode_fields = array();

foreach($data as $key => $value) {
    $implode_fields[] = (string)$key;
    $implode_values[] = Your_db_escape_method($value);  
}

if( mysql_query ("INSERT INTO " . $table . " (".implode(", ", $implode_fields).") VALUES (".implode(", ",$implode_values).")")) {
    //echo 'Data was inserted';
} else {
    //echo 'Data was not inserted';
}

or as single query as per your example

        foreach($data as $value) {

    mysql_query (
    "INSERT INTO 
        purchase(
        username,
        gameid,
        amount_purchased

    ) VALUES (
        '".$value['username']."', 
        '".$value['gameid']."',
        '".$value['amount_purchased']."')"
     );
}   

Set the values from your object and store them like this;

$_SESSION['cart'] = array (
   'username' => 'Usertest_1', // eg 'username' => $obj->username,
   'gameid' => 1234,
   'amount_purchased' => 200
);

The above is the same as;

$_SESSION['cart']['username'] = 'Usertest_1';
$_SESSION['cart']['gameid'] = 1234;
$_SESSION['cart']['amount_purchased'] = 200;

// for multiple records;

$_SESSION['cart'][] = array (
  'username' => 'Username1',
  'gameid' => 1234,
  'amount_purchased' => 200
);

$_SESSION['cart'][] = array (
  'username' => 'username2',
  'gameid' => 1234,
  'amount_purchased' => 200
);

save example

$data = $_SESSION['cart'];
$result = mysql_query ("INSERT INTO purchase(username, gameid, amount_purchased) VALUES ('".$data['username']."','".$data['gameid']."','".$data['amount_purchased']."')");
  if($result && mysql_num_rows($result)>0) {
// all ok
 }

다른 팁

If you are expecting to keep the value of $_SESSION between requests, you shouldn't trash it's value like that.

while ($_SESSION = something) { ... 

See what you are doing with $_SESSION here?

$_SESSION works just like any other variable, if you overwrite it, you drop its original value.

Also, for($i=,$i<$array_rows;$i++) ?

Does this even compile? Try this instead: for($i=0;$i<$array_rows;$i++)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top