dousong5161 2019-02-21 02:08
浏览 102
已采纳

INSERT数组多维到数据库中

i'm newbie in PHP, and sorry for my English. I had read all the questions about this topic in this forum, but i still cant find the solution for my PHP. I have 2 session here, first to save data person, and second to save cart. Here my session structure

<?php
$id_pers person = $_SESSION['person'] ; //its contain just id


//for second $_SESSION['cart'] structure when i print_r is like this
Array (
[0] => Array (
   [item_id] => 4
   [item_name] => Notebook
   [item_qty] => 1
   [price] => 750
 )
[1] => Array (
   [item_id] => 5
   [item_name] => Keyboard
   [item_qty] => 1
   [price] => 70
 )
[2] => Array (
   [item_id] => 6
   [item_name] => Mouse
   [item_qty] => 1
   [price] => 50
 )
)
?>

The problem is when i save array into database its just one array is saved, the other array cant save into database. Here my insert function what i wrote from watching video tutorial:

<?php
function save($array, $connect){
  if(is_array($array)){
     $id_person = $_SESSION['person'];
     foreach($array as $row => $value) {
         $item_id = mysqli_real_escape_string($connect, $value['item_id'];
         $item_qty = mysqli_real_escape_string($connect, $value['item_qty'];
         $sql = "INSERT INTO tbl_order(id_person,item_id,qty) VALUES ('".$id_person."','".$item_id."','".$item_qty."')";
         mysqli_query ($connect, $sql);
     }
   }
 }
save($_SESSION['cart'], $ connect);
?>

Please tell me where is my mistake on my script for inserting data array into table. Thanks :)

  • 写回答

2条回答 默认 最新

  • dousuochu7291 2019-02-21 03:11
    关注

    Syntax error

      $item_id = mysqli_real_escape_string($connect, $value['item_id'];
      $item_qty = mysqli_real_escape_string($connect, $value['item_qty];
    

    Missing the ' and a ) 2x

     $item_id = mysqli_real_escape_string($connect, $value['item_id']);
     $item_qty = mysqli_real_escape_string($connect, $value['item_qty']);
    

    And change this (this is ok, just not the best way to do it):

    function save($array, $connect){
        if(is_array($array)){
    

    to

     function save(array $array, $connect){
    

    Now if you pass in something other then an array PHP with issue an error. It's called type hinting and it lets us get rid of that check as we are guaranteed that only arrays can be passed as that argument. With manual checking your code would just fail silently and you wouldn't know why.

    I would probably toss a if(!session_id()) session_start(); in there to be safe. If there is no session ID, then create a session. You cannot store data to the session unless it's started.

    So like this:

    <?php
    function save(array $array, $connect){
    
         if(!session_id()) session_start();
    
         //don't trust this who knows where it came from, QQ'n not me.
         $id_person = mysqli_real_escape_string($connect, $_SESSION['person']);
         foreach($array as $row => $value) {
             $item_id = mysqli_real_escape_string($connect, $value['item_id']);
             $item_qty = mysqli_real_escape_string($connect, $value['item_qty']);
             $sql = "INSERT INTO tbl_order(id_person,item_id,qty) VALUES ('".$id_person."','".$item_id."','".$item_qty."')";
             mysqli_query ($connect, $sql);
         }
     }
    
    save($_SESSION['cart'], $ connect);
    ?>
    

    -NOTE- It's also preferred to prepare queries instead of escaping them.

    Cheers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line