0

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; 
Sotiris Kiritsis
  • 3,178
  • 3
  • 23
  • 31
Kavya Shree
  • 1,014
  • 2
  • 17
  • 52

4 Answers4

2

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));
     } 
Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88
1

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);    
}
Faiyaz Alam
  • 1,191
  • 9
  • 27
1
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()); 
}
channasmcs
  • 1,104
  • 12
  • 27
  • syntax error, unexpected '$valuesArr' (T_VARIABLE)..other and all okay but in case of valuesArr[] it is showing error – Kavya Shree Mar 29 '16 at 06:42
  • Ya i find the mistake. u forgt to mention one semicolon but what the problem is im using PHP pdo..bt the code u specified in mysqli – Kavya Shree Mar 29 '16 at 06:53
0

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; 
Thejas
  • 353
  • 6
  • 19