dscizpq790832708 2015-06-17 17:19
浏览 121

我可以在foreach循环中使用Multiple Array吗?

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());  }
  • 写回答

3条回答 默认 最新

  • dongxia5394 2015-06-17 17:31
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向