我想在PHP中为循环准备一个SQL插入语句,我的语句是这样的
$insert_stmt = "";
for($x=1; $x<=$all_property[duration]; $x++) {
$insert_stmt .= "INSERT INTO `schedule` VALUES ($all_property[serial], $_POST[Day$x])";
echo $insert_stmt;
mysqli_free_result($insert_stmt); // Free result set for next query
}
我希望结果像
INSERT INTO `schedule` VALUES ($all_property[serial], $_POST[Day1] //for first loop
INSERT INTO `schedule` VALUES ($all_property[serial], $_POST[Day2] //for second loop
依此类推,但出现错误
语法错误,意外的是'$ x'(T_VARIABLE),期望中的']' 第163行上的schedule.php
所以,如果有人可以帮助我纠正查询中的错误。
答案 0 :(得分:0)
已更新:
由于字段不是恒定的,而且也是字符串,因此需要用引号引起来,因此只需使用以下行:
$insert_stmt = "INSERT INTO schedule VALUES (‘".$all_property[serial]."’, ‘".$_POST["Day".$x]."’)";
答案 1 :(得分:0)
否,day1和day2不是常量值,这些是从下表生成的$ _POST []字段
for($ x = 1; $ x <= $ all_property ['duration']; $ x ++){
$ date_result = mysqli_query($ connection,“ SELECT DATE_ADD(dur_from,INTERVAL $ x-1 DAY)as dur_from FROM batch_details
WHERE serial = 1”);
$ today_date = mysqli_fetch_assoc($ date_result);
回声'
“ size =” 12“ value =”“>
“ size =” 20“占位符=”主题或主题“ maxlength =” 50“必填>
“ size =” 18“ placeholder =” Room No“ maxlength =” 3“ pattern =” [0-9] {3}“必需>
“ size =” 18“占位符=”主讲师ID“ maxlength =” 3“ pattern =” [A-Za-z] {3}“必需>
“ size =” 18“ placeholder =” Subs Lecturer ID“ maxlength =” 3“ pattern =” [A-Za-z] {3}“必填>
“ size =” 20“占位符=”主题或主题“ maxlength =” 50“必填>
“ size =” 18“ placeholder =” Room No“ maxlength =” 3“ pattern =” [0-9] {3}“必需>
“ size =” 18“占位符=”主讲师ID“ maxlength =” 3“ pattern =” [A-Za-z] {3}“必需>
“ size =” 18“ placeholder =” Subs Lecturer ID“ maxlength =” 3“ pattern =” [A-Za-z] {3}“必填>
“ size =” 20“占位符=”主题或主题“ maxlength =” 50“必填>
“ size =” 18“ placeholder =” Room No“ maxlength =” 3“ pattern =” [0-9] {3}“必需>
“ size =” 18“占位符=”主讲师ID“ maxlength =” 3“ pattern =” [A-Za-z] {3}“必需>
“ size =” 18“ placeholder =” Subs Lecturer ID“ maxlength =” 3“ pattern =” [A-Za-z] {3}“必填>
';
其中,根据“持续时间”(基本上是几天),输入字段将以name =“ Day $ x”生成,然后再次将这个值插入到另一个表中。
答案 2 :(得分:0)
是的,以上一行确实解决了问题。因此,该问题可能现在已经解决。