donglusou3335 2016-06-17 10:57
浏览 52
已采纳

PHP:将多个数组插入mysql表

I am testing multiple array insertion into my table, I have try all what I could but I am not getting.Here is my code:

<?php

    ini_set('display_errors',1);
    //ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

                    $mysqli = new mysqli(HOST,USER,PASS,DB);
                     $message = array();    

                    $id =1;
                    $SocialHandle = "Facebook,Twitter,LinkIn";
                    $SociaUrl ="Url1,Url2,Url3";

                    $strSocialHandle = explode(',', $SocialHandle); 
                    $strSociaUrl = explode(',', $SociaUrl); 
                    print_r($strSocialHandle);
                    print_r($strSociaUrl);
                    $sql = "INSERT INTO `social_table`(`id`, `social_handle`, `handle_url`) VALUES";
                    foreach($strSocialHandle as $SocialNameValue){                                      
                    $sql .= "({$id}, '{$SocialNameValue}','{$strSociaUrl}'),";              
                    }
                    $sql = rtrim($sql, ',');
                    $result = $mysqli->query($sql);

                    if (!$result){
                            $message = array('Message' => 'insert fail or record exist');
                            echo json_encode($message);         
                    }else{             
                            $message = array('Message' => 'new record inserted');
                            echo json_encode($message);   
                    }
?>

Here is my goal achievement:

ID         social handle         handle url
1           Facebook         url1
1           Twitter             url2
1           LinkIn             url3

Please help.

  • 写回答

2条回答 默认 最新

  • dongpiansui8755 2016-06-17 11:09
    关注

    You can use for loop for it as

    $id =1;
    $SocialHandle = "Facebook,Twitter,LinkIn";
    $SocialHandle = explode(",", $SocialHandle);
    $SociaUrl = "Url1,Url2,Url3";
    $SociaUrl = explode(",", $SociaUrl);
    
    $sql = "INSERT INTO `social_table`(`id`, `social_handle`, `handle_url`) VALUES";
    for ($i = 0; $i < count($SocialHandle); $i++) {
        $sql .= "({$id}, '$SocialHandle[$i]','$SociaUrl[$i]'),";
    }
    
    
    $sql = rtrim($sql, ',');
    echo $sql;
    $result = $mysqli->query($sql);
    

    OUTPUT

    INSERT INTO social_table(id, social_handle, handle_url) VALUES(1, 'Facebook','Url1'),(1, 'Twitter','Url2'),(1, 'LinkIn','Url3')

    DEMO

    UPDATED

    Better use prepare and bind statement to prevent form sql injection as

    for ($i = 0; $i < count($SocialHandle); $i++) {
        $sql = "INSERT INTO `social_table`(`id`, `social_handle`, `handle_url`) VALUES (?,?,?)";
        $stmt = $mysqli->prepare($sql);
        $stmt->bind_param('iss', $id, $SocialHandle[$i], $SociaUrl[$i]);
        $stmt->execute();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?