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; 
13
  • Post the query you are using to insert. Commented Mar 29, 2016 at 5:59
  • use foreach or for loop for multiple insert. Commented Mar 29, 2016 at 6:00
  • Possible duplicate of How does 'foreach' actually work? Commented Mar 29, 2016 at 6:01
  • 1
    should be $slot_timings[$i] Commented Mar 29, 2016 at 6:06
  • 1
    in insert query replace ,slot_timings with ,:slot_timings Commented Mar 29, 2016 at 6:18

4 Answers 4

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));
     } 
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

syntax error, unexpected '$valuesArr' (T_VARIABLE)..other and all okay but in case of valuesArr[] it is showing error
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
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);    
}

7 Comments

After using this it is showing "number of bound variables does not match number of tokens" error. i dont know whts d problem
can you show us the output for print_r($slot_timings1); ?
Array ( [0] => 6.10 [1] => 6.30 [2] => 6.40 )
Thank u so much @Er Faiyaz Alam
I have one more doubt is it possible to specify the position where i can insert this array of values.example insert should take place from the position of id="25"
|
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; 

3 Comments

If i declare $slot_timings1[$i] it is showing error.
syntax error, unexpected '=', expecting ')' is the error im getting after used this
try @Insomania solution. Its better using foreach. I am having some trouble with debugging right now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.