0

I have researched several related question in this forum and google, Kindly assist . I am trying to insert some values into database from several arrays stored in session. I also have some single values stored in some session also which i want to insert into multiple rows of dbase table.

//First, I recall the values stored in sessions from previous pages into the current page as below.

//take note of the comment in front of the sessions and All array contains the same number of values except for the first two sessions.

$ticketid="t".date('dmyHis').mt_rand (1000,9999);
        $bettime= date('d/m/y H:i');
        $_SESSION['bettime']=$bettime;//Not array, contains single value
        $_SESSION['ticketid']=$ticketid;//Not array, Contains single value
        $_SESSION['gamecode'];//array
        $_SESSION['starttime'];//array
        $_SESSION['optioncode']//array
        $_SESSION['home'];//array
        $_SESSION['away'];//array
        $_SESSION['odd'];//array

Here, I connected to dbase. //Works fine. require('gumodb.php');

Here i try to start a loop using one array session as key

 foreach($_SESSION['starttime'] as $ro => $col){
    mysql_query("INSERT INTO reg_bet (bettime, ticketid,matchcode,starttime,home,away,optionodd,optioncode) VALUES('$_SESSION[bettime]','$_SESSION[ticketid]','$_SESSION[gamecode]', '$_SESSION[starttime]','$_SESSION[home]','$_SESSION[away]','$_SESSION[odd]','$_SESSION[optioncode]' ) ") 
    or die(mysql_error());  

            }

It returns Notice: Array to string conversion in C:\xampp\htdocs\gumo\consel.php on line 61

EDIT QUESTION I am trying to achieve something like this.

foreach($_SESSION['gamecode'] as $gc => $gcvalue && $_SESSION['starttime'] as $st =>$stvalue && $_SESSION['optioncode'] as $oc => $ocvalue ){ 
mysql_query("INSERT INTO reg_bet (matchcode,starttime,optioncode) VALUES('$gcvalue','$stvalue','$ocvalue') ") 
or die(mysql_error());  }
scylla
  • 124
  • 9
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 17 '15 at 17:19
  • 1
    Your session variables in the query are missing quotes around their identifiers, `$_SESSION[odd]` should be `$_SESSION['odd']`, for example. – Jay Blanchard Jun 17 '15 at 17:20
  • possible duplicate of [When to use single quotes, double quotes, and backticks?](http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks) – Jay Blanchard Jun 17 '15 at 17:23
  • Are the arrays for gamecode, etc associative or numeric? –  Jun 17 '15 at 17:25
  • Also, what's your primary key? –  Jun 17 '15 at 17:35
  • primary key is id(auto increment) for every row created in the table – scylla Jun 17 '15 at 19:23
  • pls check the edited question to see what i actually intend doing – scylla Jun 18 '15 at 13:32

3 Answers3

1
$x = json_encode($_SESSION);
$query = "INSERT INTO ".$TABLE_NAME data "VALUES ("$x");";
 $mysqli->query( $query );

Encode the session array into a single string and insert in to the table on a row of data. When you fetch the same data use json_decode to convert the string into array.

Tanmoy
  • 1,035
  • 6
  • 19
  • i quite cant figure out how to implement this. I tried and returned error unexpected data. – scylla Jun 17 '15 at 19:21
  • you can try it with any other encoded function,you can also use serialize to convert the array in to string.. – Tanmoy Jun 18 '15 at 05:12
0

Assuming the arrays in the $_SESSION variable are numeric, you could try something like this:

for ($i = 0; $i < $max_index_count; $i++) {
  $query = "INSERT INTO ".$TABLE_NAME;
  $query += "VALUES (".$_SESSION['index'][$i].");";
  $mysqli->query( $query );
}

The above is pseudo code, but the problem is you are trying to use an array as a string. The $_SESSION variable is a multidemsional array, therefore, specify two ibdexes.

0

After so many trials, i was able to get it done with this

$ticketid="t".date('dmyHis').mt_rand (1000,9999);//ticket id generateed 
$bettime= date('d/m/y H:i');
$_SESSION['bettime']=$bettime;
$_SESSION['ticketid']=$ticketid;
$_SESSION['gamecode'];
$_SESSION['starttime'];
$_SESSION['optioncode'];
$_SESSION['home'];
$_SESSION['away'];
$_SESSION['odd'];
require('gumodb.php');


foreach ($_SESSION['gamecode'] as $index => $value)
{
$ge = $_SESSION['gamecode'][$index];
$se = $_SESSION['starttime'][$index];
$oe = $_SESSION['optioncode'][$index];
$he = $_SESSION['home'][$index];
$ay = $_SESSION['away'][$index];
$od = $_SESSION['odd'][$index];

This post the arrays and non array into table row and display

  mysql_query("INSERT INTO reg_bet (matchcode,ticketid,bettime,starttime,home,away,optionodd,optioncode) VALUES('$ge','$ticketid','$bettime','$se','$he' ,'$ay','$od' ,'$oe' ) ") 
    or die(mysql_error());  
    echo $_SESSION['gamecode'][$index] .'-'. $_SESSION['starttime'][$index].'- '. $_SESSION['optioncode'][$index].' -'. $_SESSION['home'][$index].'- '. $_SESSION['away'][$index].'- '. $_SESSION['odd'][$index].$bettime.' -' .$ticketid.'</br>' ;
}

Thanks for your contributions.

scylla
  • 124
  • 9