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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程