Kavya Shree
Kavya Shree

Reputation: 1032

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; 

Upvotes: 0

Views: 125

Answers (4)

Faiyaz Alam
Faiyaz Alam

Reputation: 1217

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);    
}

Upvotes: 1

channasmcs
channasmcs

Reputation: 1156

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()); 
}

Upvotes: 1

Amrinder Singh
Amrinder Singh

Reputation: 5482

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));
     } 

Upvotes: 2

Thejas
Thejas

Reputation: 353

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; 

Upvotes: 0

Related Questions