我正在尝试在我的数据库中插入多个值。但我找不到解决方案来做我想做的事。
我的问题是我有一系列的值,比如
("6.40","6.50","7.00","7.10","7.20","7.30")
我希望在每行中插入这些值,如6.40将存储在一行对应的id" 1"。
同样" 6.50"将存储在" 2"的id中。 id将自动递增。
类似地,它将在数据库中插入值,直到数组为空。如果有人对如何解决这个问题有任何想法,请帮助我!请怀疑我。我是PHP的新手。提前谢谢。
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;
答案 0 :(得分:2)
尝试这样的事情。
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));
}
答案 1 :(得分:1)
试试这个:
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);
}
答案 2 :(得分: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());
}
答案 3 :(得分:0)
试试这个:
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;