This is a discussion on Problem Inserting 2D array into mysql database. within the Coding Help forums, part of the Webmaster Help category; Hi guys, I have read a few threads on this but I'm still stuck ! I have an array which ...
| |||||||
| Register | Forum Rules | FAQ | Donate | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hi guys, I have read a few threads on this but I'm still stuck ! I have an array which is a shopping basket, and it holds details about each item, and a number of items. For example, one users basket may be.. Topside Joint Cotswold Edge beef 9 1 Pork Cotswold Edge sausages 6.00 1 which is the output as... $name = $_SESSION['basket'][$key]['name']; $supplier = $_SESSION['basket'][$key]['supplier']; $category = $_SESSION['basket'][$key]['category']; $price = $_SESSION['basket'][$key]['price']; $qty = $_SESSION['basket'][$key]['qty']; echo $id . "<br />"; echo $name . "<br />"; echo $supplier . "<br />"; echo $category . "<br />"; echo $price . "<br />"; echo $qty . "<br />"; I KNOW this is a bad method (and it doesn't even work!) but this is how my code currently stands: foreach($_SESSION['basket'] as $key => $another) { $name = $_SESSION['basket'][$key]['name']; $supplier = $_SESSION['basket'][$key]['supplier']; $category = $_SESSION['basket'][$key]['category']; $price = $_SESSION['basket'][$key]['price']; $qty = $_SESSION['basket'][$key]['qty']; $sql = "INSERT INTO orders VALUES(0,'".$id."','".$oid."','".$name."','".$pric e."','".$supplier."','".$category."')" $result = mysql_query($sql, $connection) or die("MySQL Error: ".mysql_error()); } How can I do this in a way thats a) better/more efficient and b) just works? Thanks in advance Dave |
| |||||
| can you print the array or the sql statement here? print_r($_SESSION); //or echo $sql;
__________________ :. Web Design & Development Web Design Ireland :. Search Engines Optimization Search Engines Optimization :. Car Parts & Accessories Car Parts :. Cars Ireland Cars Ireland :. I Have 2 Find It Directory SEF Directory |
| ||||
| Thanks of your reply louie, this is the print of $_SESSION['basket'] with 2 items in it. Array ( [0] => Array ( [id] => 1 [name] => Pork [price] => 6.00 [supplier] => Cotswold Edge [category] => sausages [qty] => 1 ) [1] => Array ( [id] => 2 [name] => Pork [price] => 6.00 [supplier] => Cotswold Edge [category] => sausages [qty] => 1 ) ) hopefully you can make sense of it! Dave |
| |||||
| You have a 3D array so you will need another foreach loop inside the "foreach" |
| |||||
| what exactly does the $key holds as value? |
| ||||
| I'm not sure, sorry a bit new to this. this is my addToBasket.php file which adds items the customer wants, into my array. <? //removed some irrelevant stuff $basketArray = array(); $basketArray['id'] = $_GET['id']; $basketArray['name'] = $_GET['name']; $basketArray['price'] = $_GET['price']; $basketArray['supplier'] = $_GET['supplier']; $basketArray['category'] = $_GET['category']; $basketArray['qty'] = 1; $price = $_GET['price']; $subtotal = $_SESSION['subtotal']; $found = false; if(isset($_SESSION['basket'])) { foreach($_SESSION['basket'] as $key => $another) { if($_SESSION['basket'][$key]['id'] == $_GET['id']) { $found = true; break; } } } if($found == false) { $subtotal = ($subtotal + $price); $_SESSION['subtotal'] = $subtotal; $_SESSION['basket'] [] = $basketArray; } header("Location: foodItems.php?category=".$category."" ); exit(); ?> |
| |||||
| Something to get you rolling: $sql = ""; $oid = "someid"; //don't know what that is so I gave it a value $id = !empty($_GET['id']) ? preg_replace("#[^0-9]#","",$_GET['id']) : "111"; $name = !empty($_GET['name']) ? preg_replace("#[^0-9a-zA-Z\- ]#","",$_GET['name']) : "name"; $price = !empty($_GET['price']) ? preg_replace("#[^0-9]#","",$_GET['price']) : "999"; $supplier = !empty($_GET['supplier']) ? preg_replace("#[^0-9a-zA-Z\- ]#","",$_GET['supplier']) : "SUPPLIER"; $category = !empty($_GET['category']) ? preg_replace("#[^0-9a-zA-Z\- ]#","",$_GET['category']) : "CATEGORY"; $basketArray = array(); $basketArray['id'] = $id; $basketArray['name'] = $name; $basketArray['price'] = $price; $basketArray['supplier'] = $supplier; $basketArray['category'] = $category; $basketArray['qty'] = 1; //$price = $_GET['price']; $subtotal = !empty($_SESSION['subtotal']) ? $_SESSION['subtotal'] : "0"; $found = false; //print_r($basketArray); if(!empty($_SESSION['basket'])) { foreach($_SESSION['basket'] as $key) { foreach($key as $x=>$y){ echo $x."-".$y."<br />"; if($y == $id) { $found = true; echo "<h1>found</h1>"; break; } } } } if(!$found) { $subtotal = ($subtotal + $price); $_SESSION['subtotal'] = $subtotal; $_SESSION['basket'][] = $basketArray; //insert data to DB $sql = "INSERT INTO orders VALUES(0,'".$id."','".$oid."','".$name."','".$pric e."','".$supplier."','".$category."')"; }else{ //update quantity $sql = "UPDATE orders....."; } //header("Location: foodItems.php?category=".$category."" ); //exit(); //print_r($_SESSION['basket']); if($sql != ""){ echo "<h1>".$sql."</h1>"; //$result = mysql_query($sql, $connection) //or die("MySQL Error: ".mysql_error()); } |
| |||||
| so, was the above code any help to you? |
| Tags |
| 2d array, array, arrays, database, inserting, mysql, php, problem, sql |
| Thread Tools | |
| Display Modes | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Periodically inserting data in to a database | jason | Coding Help | 4 | 24-09-2008 09:25 PM |
| Checkbox inserting in mysql | Joseph Grogan | Coding Help | 1 | 10-01-2008 10:40 AM |
| Ajax and mysql and PHP, pulling info from a database | Ciarán Mc Cann | Coding Help | 10 | 09-08-2007 12:26 PM |
| Inserting PHP multidimensional arrays into a mySQL database | Ciarán Mc Cann | Coding Help | 15 | 03-08-2007 06:15 PM |
| Connecting to 2 Mysql database in the same script?? | Ciarán Mc Cann | Coding Help | 11 | 24-07-2007 10:21 AM |
| ||||
| | ![]() | |||
| | ||||