1

I want to store array into mysql db something like this

item_row  = nike,adidas,puma
qty_row   = 1,3,2
total_row = 100,200,150

foreach

  foreach ($_SESSION['order'] as $values) {
      $item_name = $values['item-name'];
      $item_qty = $values['item-qty'];
      $item_price = $values['item-price'];
  }

Let me know how to do that?

update

  foreach ($_SESSION['order'] as $values) {
      $item_name[] = $values['item-name'];
      $item_qty[] = $values['item-qty'];
      $item_price[] = $values['item-price'];
  }

  $item_row = implode(",", $item_name);
  $qty_row = implode(",", $item_qty);
  $total_row = implode(",", $item_price);
1
  • 5
    Keep in mind that what you're doing here is circumventing the use of linking relational tables. So if down the line, you need scalability and speed, consider creating a few linking tables between the order and the quantities. Commented Dec 17, 2009 at 18:22

3 Answers 3

4
item_row = implode(',', $_SESSION['order']['item-name']);
qty_row = implode(',', $_SESSION['order']['item-qty']);
total_row = implode(',', $_SESSION['order']['item-price']);
Sign up to request clarification or add additional context in comments.

Comments

0

I'm using a class to manage the connection to the data base and the query execution let me add it to you:

class DbConnection
{
var $ReturnQuery;
function Connect()
{
    $connection = mysql_connect("serverName", "user", "password");
    $DbSelect = mysql_select_db("databaseName", $connection);
    if ($DbSelect)
        return true;
    else
        return false;
}
function Execute($Query)
{
    $ExecuteQuery = mysql_query($Query);
    $affected = mysql_affected_rows();
    if ($affected != -1)
    {
        if ($affected != 0)
        {
            if ($ExecuteQuery != 1)
            {
                while($row=mysql_fetch_assoc($ExecuteQuery))
                {
                    $ResulArray[] = $row;
                }
                $this->ReturnQuery = $ResulArray;
            }
            return 1;
        }
        else
        {
            $this->ReturnQuery = '';
            return 0;

        }
    }
    else
    {
        $this->ReturnQuery = '';
        return -1;
    }
}
}

and then you can create instances to execute your query:

require_once('Includes/DbConnection.php');
    $this->db = new DbConnection();
    $this->db->Connect();

$query = "insert into items (item_name, item_qty, item_price) values ('".$item_name."', '".$item_qty."', '"$item_price"');

$query_safe = mysql_real_escape_string($query);
$this->db->Execute($query_safe);

I hope it helps!!

1 Comment

Hi Alejandra. Your code looks neat. However, security best practice for SQL use in PHP is to use PDO and prepared statements. Have a look at this page: owasp.org/index.php/PHP_Top_5#P3:_SQL_Injection
-2
foreach ($_SESSION['order'] as $values) {

 mysql_query('INSERT INTO tablename (name, qty, price) VALUES("'.$values['item-name'].'", "'.$values['item-qty'].'", "'.$values['item-price'].'"');
}

3 Comments

This post is an answer to the question "How do I open myself to SQL injections?"
To be fair, the session is server-side and its contents could have already been sanitized :P Though prepared statements are always best.
@Lucas... you ALWAYS sanitize you data, nothing is ever safe, including data from the database itself. By assuming some piece of data is safe because it didn't come directly from user input is a sure way to get SQL injected from a more complex method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.