0

I have the array-ed session....

$_SESSION['Names'] = array (11,15,26);
$_SESSION['Location'] = array (35,42,10);

and I want to store them in my database...

$que = "Insert into tblpeople (DateTimePosted, first, second, third) VALUES(now(),'$_SESSION['Names'][0], $_SESSION['Location'][0])','$_SESSION['Names'][1], $_SESSION['Location'][1])','$_SESSION['Names'][2], $_SESSION['Location'][2])')";
$exec = mysql_query($que);

After Saving, my database (tblpeople) shows the following values:

DateTimePosted: 2014-01-03 16:23:02

first: Array[0],Array[0]

second: Array[1],Array[1]

third: Array[2],Array[2]

Instead, I want my output to be...

DateTimePosted: 2014-01-03 16:23:02

first: 11,35

second: 15,42

third: 26,10

What's wrong?

5
  • there is extra ) at the end of session values Commented Jan 6, 2014 at 6:08
  • like how? I don't get it. Commented Jan 6, 2014 at 6:09
  • stackoverflow.com/questions/3413291/… Commented Jan 6, 2014 at 6:10
  • It seems that your table is not properly normalized; numerically named columns are a sign of sql anti-pattern. Commented Jan 6, 2014 at 6:12
  • You need to wrap the variables in curly braces; i.e. "...'{$_SESSION['Names'][0]}', ...". Commented Jan 6, 2014 at 6:15

3 Answers 3

2

To expand multidimensional arrays in a string, you need to wrap them in curly braces:

$que = "Insert into tblpeople (DateTimePosted, first, second, third)
        VALUES(now(),
               '{$_SESSION['Names'][0]}, {$_SESSION['Location'][0]}',
               '{$_SESSION['Names'][1]}, {$_SESSION['Location'][1]}',
               '{$_SESSION['Names'][2]}, {$_SESSION['Location'][2]}')";

You also had some extra parentheses in the values.

However, this seems like a pretty strange way to store data into a database. Why do you have two values separated by commas in each column, rather than splitting each into separate columns? And why are you storing array elements into different columns, rather than using separate tables with each value in a row?

Sign up to request clarification or add additional context in comments.

Comments

0

use this function

$x=serialize($_SESSION['Names']);

it return a string that you can save any where

and this function reverse it

$_SESSION['Names']=unserialize($x);

2 Comments

and i should store it as $x[0] instead of $_SESSION['Names'][0]? Is that what you mean?
serialize/unserialize has vulnerability, see owasp.org/index.php/PHP_Object_Injection. json_encode/json_decode will be a better choice.
0

Try this

<?php
session_start();
$_SESSION['Names'] = array (11,15,26);
$_SESSION['Location'] = array (35,42,10);


$refNumbers = $_SESSION['Names'];
$partIds = $_SESSION['Location'];



$combined = array();

foreach($refNumbers as $index => $refNumber) {
    if(!array_key_exists($index, $partIds)) {
        throw OutOfBoundsException();
    }

    $combined[] = array(
        'Names'  => $refNumber,
        'Location' => $partIds[$index]
    );
}

print_r($combined);

$combine1 = implode(",",$combined[0]);
$combine2 = implode(",",$combined[1]);
$combine3 = implode(",",$combined[2]);



$que = "insert into tblpeople (DateTimePosted, first, second, third) VALUES(now(),'$combine1','$combine2','$combine3')";
//$exec = mysql_query($que);

?>

1 Comment

Hi, thanks for your answer. I have already solved this problem but anyway, please don't delete your answer. It may be helpful to some who may land on this question :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.