【问题标题】:prepare SQLite insert statement inside php for loop在 php for 循环中准备 SQLite 插入语句
【发布时间】:2018-08-06 07:02:21
【问题描述】:

我想在 PHP for 循环中准备一个 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),期望 ']' 在 schedule.php 在第 163 行

所以,如果有人可以帮助我纠正我的查询中的错误。

【问题讨论】:

    标签: php for-loop mysqli-multi-query


    【解决方案1】:

    更新:

    由于字段不是常量,而且它们是字符串,你需要引用它们,所以只需使用这一行:

    $insert_stmt = "INSERT INTO schedule VALUES (‘".$all_property[serial]."’, ‘".$_POST["Day".$x]."’)";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 2016-04-08
      • 1970-01-01
      • 2019-06-18
      相关资源
      最近更新 更多