【问题标题】:Array PDO to MYSQL数组 PDO 到 MYSQL
【发布时间】:2022-01-25 12:18:47
【问题描述】:

所以我尝试使用 fav_id 输入每个便利设施,但每次我遇到这两个错误时,我都尝试在 foreach() 之前使用 if 但仍然得到相同的两个错误

 $inserted_id = $PDO->lastInsertId();
 $amenity_name = ['wifi', 'amenity kit'];

 $sql = "INSERT INTO user_fav (fav_id, amenity_name) VALUES (:LASTINSERTID, :AMENITY)";

 $stmt = $PDO->prepare($sql);
 $stmt->bindParam(':LASTINSERTID', $inserted_id);
 $stmt->bindParam(':AMENITY', $amenity_name);  --- line 54
     
 
 foreach($amenity_name as $item) { --- line 57
 $stmt->execute($item);
}

我得到了这两个错误

注意:第 54 行的数组到字符串的转换
警告:在第 57 行为 foreach() 提供的参数无效

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    这一行

    $stmt->bindParam(':AMENITY', $amenity_name);
    

    $amenity_name 从数组更改为字符串:

    注意:数组到字符串的转换

    然后你不能迭代它(因为它是一个字符串,可合并)所以你也会收到:

    警告:为 foreach() 提供的参数无效

    你应该绑定在foreach:

    $inserted_id = $PDO->lastInsertId();
    $amenity_name = ['wifi', 'amenity kit'];
    $sql = "INSERT INTO user_fav (fav_id, amenity_name) VALUES (:LASTINSERTID, :AMENITY)";
    $stmt = $PDO->prepare($sql);
    foreach($amenity_name as $item) {
        $stmt->execute(array('LASTINSERTID' => $inserted_id, 'AMENITY' =>$item));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 2012-07-16
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多