+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 12

Thread: Problem Inserting 2D array into mysql database.

  1. #1
    daveharding is offline Frontpage User daveharding will become famous soon enough
    Join Date
    Nov 2008
    Location
    England
    Posts
    6

    Smile Problem Inserting 2D array into mysql database.

    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

  2. #2
    daveharding is offline Frontpage User daveharding will become famous soon enough
    Join Date
    Nov 2008
    Location
    England
    Posts
    6

    Default

    p.s. not sure why, but in the above view $price has a space in it, this does not appear in my code. Cheers

  3. #3
    louie's Avatar
    louie is offline Senior Member louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,328

    Default

    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

  4. #4
    daveharding is offline Frontpage User daveharding will become famous soon enough
    Join Date
    Nov 2008
    Location
    England
    Posts
    6

    Default

    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

  5. #5
    louie's Avatar
    louie is offline Senior Member louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,328

    Default

    You have a 3D array so you will need another foreach loop inside the "foreach"
    :. 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

  6. #6
    daveharding is offline Frontpage User daveharding will become famous soon enough
    Join Date
    Nov 2008
    Location
    England
    Posts
    6

    Default

    when would I run the sql statement?

    Could you give me a rough idea of what the code would look like?

    Cheers!

  7. #7
    louie's Avatar
    louie is offline Senior Member louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,328

    Default

    what exactly does the $key holds as value?
    :. 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

  8. #8
    daveharding is offline Frontpage User daveharding will become famous soon enough
    Join Date
    Nov 2008
    Location
    England
    Posts
    6

    Default

    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();
    ?>

  9. #9
    louie's Avatar
    louie is offline Senior Member louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,328

    Default

    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());
    }
    :. 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

  10. #10
    louie's Avatar
    louie is offline Senior Member louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,328

    Default

    so, was the above code any help to you?
    :. 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

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Similar Threads

  1. Periodically inserting data in to a database
    By jason in forum Coding Help
    Replies: 4
    Last Post: 24-09-2008, 09:25 PM
  2. Checkbox inserting in mysql
    By Joseph Grogan in forum Coding Help
    Replies: 1
    Last Post: 10-01-2008, 11:40 AM
  3. Ajax and mysql and PHP, pulling info from a database
    By Ciarán Mc Cann in forum Coding Help
    Replies: 10
    Last Post: 09-08-2007, 12:26 PM
  4. Inserting PHP multidimensional arrays into a mySQL database
    By Ciarán Mc Cann in forum Coding Help
    Replies: 15
    Last Post: 03-08-2007, 06:15 PM
  5. Connecting to 2 Mysql database in the same script??
    By Ciarán Mc Cann in forum Coding Help
    Replies: 11
    Last Post: 24-07-2007, 10:21 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Optimization by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64