Skip to content
Advertisement

I want to insert data into a table using loop I have Multi -D array using PHP

**

data did not insert into table, please help I am not understand which loop to use I want to insert data into a table using loop I have Multi -D array using PHP

**

<?php
$data = array('eid' => array(124,658,457,145),
                'data' => array('2012/10/12','2012/10/15','2012/10/22','2012/10/02'),
                'name' => array('Chiku','Lipu','Babu','Dipu')
);
foreach($data as $key => $value){   ///////////
        $k = $key;
        $v = $value;
        echo "<pre>";
        print_r ($k);
        echo "<br>";
    foreach($v as $ke => $va){  /////////////
        //echo "<pre>";
        //print_r ($va);
        $ke;
        $va;
        //echo "<pre>";
        print_r ($va);
    }
        $count = count($va);
        echo "<br>";
        echo $count;
}
    echo "<pre>";
    //print_r ($k);
    //echo "<pre>";
    //print_r ($v);
    $k = $k;
    $v = implode('',$v);
    //echo $v;
    $con = mysqli_connect('localhost','root','','loop');
    $sql = "INSERT INTO loopt (`eid`,`date`,`name` ) VALUES ($v)";
    $exe = mysqli_query($con,$sql);

?>

Advertisement

Answer

it’s not a best way to do this, but still, you can try following

$data = array('eid' => array(124,658,457,145),
                'data' => array('2012/10/12','2012/10/15','2012/10/22','2012/10/02'),
                'name' => array('Chiku','Lipu','Babu','Dipu')
);


if (isset($data['eid']) && is_array($data['eid']) && sizeof($data['eid']) > 0) 
{
    $con = mysqli_connect('localhost','root','','loop');

    foreach ($data['eid'] as $key => $value) 
    {
        if (isset($data['data']) && isset($data['data'][$key]) && isset($data['name']) && isset($data['name'][$key]) ) 
        {
            $sql = "INSERT INTO loopt (`eid`,`date`,`name` ) VALUES ($value,$data['data'][$key],$data['name'][$key])";
            $exe = mysqli_query($con,$sql);    

        }
    }
} 
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement