php - Inserting cart and user data into mysql for a order system -
hi there, had question similar one, seems i'm stuck again. thought got work until tried add more 1 product cart , sending database.
the user data (name, address, etc..) , cart data 1 item (product name, price, etc..) gets inserted without problem. however, if have lets say, 2 items in cart when try send data database, 2 rows there isn't problem, both rows same item, seem count same id twice, there 2 different id's in session.
any clues i've gone wrong?
i'm sending data through form!
here's code block:
$editformaction = $_server['php_self']; if (isset($_server['query_string'])) { $editformaction .= "?" . htmlentities($_server['query_string']); } if ((isset($_post["mm_insert"])) && ($_post["mm_insert"] == "form1")) { $insertsql = sprintf("insert ordermodul (orgnummer, namn, levadress, faktadress, telefon, email, datum, summa, nmr) values (%s, %s, %s, %s, %s, %s, now(), %s, %s)", getsqlvaluestring($_post['orgnum'], "text"), getsqlvaluestring($_post['namn'], "text"), getsqlvaluestring($_post['levadress'], "text"), getsqlvaluestring($_post['faktadress'], "text"), getsqlvaluestring($_post['telefon'], "text"), getsqlvaluestring($_post['email'], "text"), getsqlvaluestring($_post['summa'], "text"), getsqlvaluestring($_post['nmr'], "text")); mysql_select_db($database_vesundberg_eu, $vesundberg_eu); $result1 = mysql_query($insertsql, $vesundberg_eu) or die(mysql_error()); $order_id = mysql_insert_id(); if ($order_id) { $quantity = $each_item['quantity']; implode ( ',', array_values($each_item)); $line_cost = $price; foreach ($_session['cart_array'] $each_item) { $item_id = $each_item['item_id']; $sql = sprintf("select id, productname, price products id = %d;", '$item_id'); $result = mysql_query($sql); if(mysql_num_rows($result) > 0) { list($id, $price) = mysql_fetch_row($result); $line_cost = number_format($price * $quantity, 2); } $query = sprintf("insert cartorders (id, nmr, name, price, quantity) values (%s, %s, %s, %s, %s)", getsqlvaluestring("$artnummer", "int"), getsqlvaluestring("$nmr", "int"), getsqlvaluestring("$product_name", "text"), getsqlvaluestring("$line_cost", "int"), getsqlvaluestring("$quantity", "int")); mysql_select_db($database_vesundberg_eu, $vesundberg_eu); $result1 = mysql_query($query, $vesundberg_eu) or die(mysql_error()); }} $insertgoto = "orders.php"; if (isset($_server['query_string'])) { $insertgoto .= (strpos($insertgoto, '?')) ? "&" : "?"; $insertgoto .= $_server['query_string']; } header(sprintf("location: %s", $insertgoto)); }
here's form:
<form action="<?php echo $editformaction; ?>" method="post" name="form1" id="form1"> <table width="60%" border="0" align="center"> <tr align="center"> <td align="right">org. nummer:</td> <td align="left"><input name="orgnum" id="orgnum" type="text" /> *</td> </tr> <tr align="center"> <td align="right">företagsnamn:</td> <td align="left"><input name="namn" type="text" id="namn" /> *</td> </tr> <tr align="center"> <td align="right">lev. adress:</td> <td align="left"><input name="levadress" type="text" id="levadress" /> *</td> </tr> <tr align="center"> <td align="right">fakt. adress:</td> <td align="left"><input name="faktadress" type="text" id="faktadress" /> *</td> </tr> <tr align="center"> <td align="right">telefon:</td> <td align="left"><input name="telefon" type="text" id="telefon" /> *</td> </tr> <tr align="center"> <td align="right">e-mail:</td> <td align="left"><input name="email" type="text" id="email" /> *</td> </tr> <tr align="center"> <td align="right"></td> <td align="left"><input name="summa" type="hidden" id="summa" value="<?php echo $carttotal; ?>" /></td> </tr> <tr align="center"> <td align="right"></td> <td align="left"><input name="prodnum" type="hidden" id="prodnum" value="<?php echo $artnummer; ?>" /></td> </tr> <tr align="center"> <td align="right"></td> <td align="left"><input name="prodname" type="hidden" id="prodname" value="<?php echo $product_name; ?>" /></td> </tr> <tr align="center"> <td align="right"></td> <td align="left"><input name="price" type="hidden" id="price" value="<?php echo $price; ?>" /></td> </tr> <tr align="center"> <td align="right"></td> <td align="left"><input name="quantity" type="hidden" id="quantity" value="<?php echo $each_item['quantity']; ?>" /></td> </tr> <tr align="center"> <td align="right"></td> <td align="left"><input name="nmr" type="hidden" id="nmr" value="<?php echo $nmr; ?>" /></td> </tr> <tr align="center"> <td align="right"> </td> <td align="left"><input name="submit" type="submit" value="skicka order" /></td> </tr> </table> <input type="hidden" name="mm_insert" value="form1" /> </form>
i suspect id pulling products same in each pass because
sprintf("select id, productname, price products id = %d;", '$item_id');
should evaluate "select id, productname, price products id = 0 "
because '$item_id' evaluates string "$item_id" not number, rather string of number. this answer has nice break down of variable evalutation in strings
change :
$sql = sprintf("select id, productname, price products id = %d;", $item_id);
another issue is
list($id, $price) = mysql_fetch_row($result);
the returned row $results
made of ("id","product_name", "price")
, i'm guessing $price
has product name
in it, , price
going no where.
Comments
Post a Comment