简体   繁体   中英

How to insert multidimensional array fields and array rows to mysql using php

I've been trying to insert this kind of array to mysql using php but always failed, any kind of suggestion guys ?

Array
(
    [id] => Array
        (
            [0] => 0303/FTSCY/WS95011
            [1] => 0403/FTSCY/WS95011
            [2] => 0403/FTSCY/WS95011
            [3] => 1103/FTSCY/WS95011
        )

    [total] => Array
        (
            [0] => 120825.00
            [1] => 116441.00
            [2] => 11441.00
            [3] => 350000.00
        )

    [berita] => Array
        (
            [0] => #weye
            [1] => gunadi #1441
            [2] => Kekurangan # 1441
            [3] => webreport Spulsa
        )

    [nama] => Array
        (
            [0] => ROSTRIVIA EVELYN D
            [1] => GUNADI HIDAYAT
            [2] => GUNADI HIDAYAT
            [3] => SUJOKO
        )

)

this is my php code :

$output=$VCurl->goCurl('https://ibank.klikbca.com/accountstmt.do?value(actions)=acctstmtview');
    $output= trim(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $output[0])); 
    preg_match_all('@<td width="130" bgcolor="#(.*?)"><div align="left"><font face="verdana" size="1" color="#(.*?)">(.*?)</font></div></td><td width="30" bgcolor="#(.*?)"><div align="center"><font face="verdana" size="1" color="#(.*?)">0000</font></div></td>@',  $output,$info); 
    //print_r ($info);
    foreach($info[3] as $newInfo)
    {
        preg_match_all("#TRSF E-BANKING CR<br>(.*?)<br>(.*?)<br>(.*?)<br>(.*?)<br>(.*?)#",$newInfo,$data);
        if($data[2] && $data[3]){

            $dataTrf['tanggal'][] = trim($data[0][0]);
            $dataTrf['id'][] = trim($data[1][0]);
            $dataTrf['total'][]= trim($data[2][0]);
            $dataTrf['berita'][]= trim($data[3][0]);
            $dataTrf['nama'][]= trim($data[4][0]);

            $conn = mysql_connect(DBHOST, DBUSER, DBPASS) or
            die ('Error connecting to mysql');

            mysql_select_db(DBNAME) or
                    die ('Error selecting database');



            for($i = 1; $i < count($data); $i++){
                //this is where your sql goes
                $sql = "INSERT INTO bca (id, tanggal, total, berita,nama) 
                VALUES(trim($data[1][$i]),$data[2][$i], $data[3][$i], $data[4][$i], $data[5][$i]) ";

                print_r ($sql);
                mysql_query($sql) 
                        or die(mysql_error());  
            }
        }

I think my mistake are in looping process but i'm new to php programming, please give me a suggestion / solutions, thx

in the middle of my coding I will need of this in inserting multi array at the same time...

Here is my code...

  $keyid = $_POST['keyid'];
  $ksiteid = $_POST['ksiteid'];
  $lickey = $_POST['lickey'];
  $keytype = $_POST['keytype'];
  $lcontext = $_POST['lcontext'];
  $ctxparm = $_POST['ctxparm'];
  $progby = $_POST['progby'];
  $progdt = $_POST['progdt'];
  $produser = $_POST['produser'];
  $lickey = $_POST['lickey'];

  // in this section I will insert a lists of data at the same time 
  foreach($lickey as $idlk=>$lkey)
  {
  $sql = "INSERT INTO sitekeys (keyid, ksiteid, lickey, keytype, lcontext, ctxparm, progby, progdt, produser )
  VALUES ('$keyid', '$ksiteid', '$lkey', '$keytype', '$lcontext', '$ctxparm', '$progby', '$progdt',  '$produser')"; 
  $result = mysqli_query($connection, $sql);
  }

I hope it helps

Change this line:

$sql = "INSERT INTO bca (id, tanggal, total, berita, nama) 
        VALUES('" . trim($data['id'][$i]) . "', '', 
        '" . $data['total'][$i] . "', 
        '" . $data['berita'][$i] . "', 
        '" . $data['nama'][$i] . "'");

Note: Be sure to escape the string before inserting into DB.

Try this code

   for($i = 0; $i < count($data['id']); $i++){      

        $_id = trim($data['id'][$i]);

        $sql = "INSERT INTO bca (`id`, `tanggal`, `total`, `berita`, `nama`) ";
        $sql.= "VALUES ('{$_id}', '{$data['tanggal'][$i]}', '{$data['total'][$i]}', '{$data['berita'][$i]}', '{$data['nama'][$i]}');";

        print_r($sql);
        mysql_query($sql) or die(mysql_error());  
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM