简体   繁体   中英

Insert multiple values in database using php

I am trying to insert multiple values in my database. But I could not find the Solution to do what I want.

My problem here is that I have a array of values like ("6.40","6.50","7.00","7.10","7.20","7.30") and I want to insert these values in each row like 6.40 will store in one row corresponding of id "1".

Similarly "6.50" will store in id of "2". The id will just auto increment.

Similarly it will insert values in database until array empty. If anyone has any ideas on how to solve this problem please help me out! Please bear my doubts. I am new to PHP. Thanks in advance.

 for ($i=0;$i < count($slot_timings1); $i++)
 { 
    $q = $this->link->prepare('INSERT INTO doctor_appointment (doctor_name,doctor_id,appointment_date,slot_name,slot_timings) VALUES (:doctor_name,:doctor_id,:appointment_date,:slot_name,slot_timings)');
    $q->execute(array(':doctor_name'=>$doctor_name,':doctor_id'=>$doctor_id, ':appointment_date'=>$appointment_date,':slot_name'=>$slot_name,':slot_timings'=‌​>$slot_timings));
 } 
 $counts = $q->rowCount(); 
 return $counts; 

Try something like this.

foreach($slot_timings1 as $data)
     { 
        $q = $this->link->prepare('INSERT INTO doctor_appointment (doctor_name,doctor_id,appointment_date,slot_name,slot_timings) VALUES (:doctor_name,:doctor_id,:appointment_date,:slot_name,slot_timings)');
        $q->execute(array(':doctor_name'=>$doctor_name,':doctor_id'=>$doctor_id, ':appointment_date'=>$appointment_date,':slot_name'=>$slot_name,':slot_timings'=‌​>$data));
     } 

try this:

if(is_array($slot_timings1) && !empty($slot_timings1))
{
 foreach ($slot_timings1 as $slot_timing)
 { 
$q = $this->link->prepare('INSERT INTO doctor_appointment (doctor_name,doctor_id,appointment_date,slot_name,slot_timings) VALUES (:doctor_name,:doctor_id,:appointment_date,:slot_name,:slot_timings)');

 $q->execute(array(':doctor_name'=>$doctor_name,':doctor_id'=>$doctor_id, ':appointment_date'=>$appointment_date,':slot_name'=>$slot_name,':slot_timings'=>$slot_timing));
} 

return count($slot_timings1);    
}
if(is_array($slot_timings1)){


 sort($slot_timings1); //Sort the elements of the array in ascending

$sql = "INSERT INTO doctor_appointment (doctor_name,doctor_id,appointment_date,slot_name,slot_timings) VALUES ";

$query_val = array();
foreach($slot_timings1 as $gettime){

    $row1 = $doctor_name;
    $row2 = $doctor_id;
    $row3 = $appointment_date;
    $row4 = $slot_name;
    $row5 = $gettime;
    $query_val[] = "('$row1', '$row2', '$row3', '$row4', '$row5')";
}

$sql .= implode(',', $query_val);

mysql_query($sql) or exit(mysql_error()); 
}

Try this:

for ($i=0;$i < count($slot_timings1); $i++)
 { 
$q = $this->link->prepare('INSERT INTO doctor_appointment (doctor_name,doctor_id,appointment_date,slot_name,slot_timings) VALUES (:doctor_name,:doctor_id,:appointment_date,:slot_name,slot_timings)');
 $q->execute(array(':doctor_name'=>$doctor_name,':doctor_id'=>$doctor_id, ':appointment_date'=>$appointment_date,':slot_name'=>$slot_name,':slot_timings'=‌​>$slot_timings1[$i]));
} 
$counts = $q->rowCount(); 
return $counts; 

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