我刚刚更改了一个脚本以使用
$query=$database->query (INSERT...)
但如果从上一页生成的输入字段数高于预期,我还有几个if语句添加到查询中。
原始查询如下......
$query=$database->query("INSERT INTO pool_" . $pool_name . " (team_name) VALUES('$team_one'), ('$team_two'), ('$team_three'), ('$team_four'), ('$team_five'),
('$team_six'), ('$team_seven'), ('$team_eight')");
我理解我得到的错误,我只是不知道如何避免它。
错误是可捕获的致命错误:PDOStatement类的对象可以 不能转换成字符串 第32行的D:\ newXamp \ htdocs \ real_do_create_pool.php
代码行是......
$query.=", ('$team_nine'), ('$team_ten'), ('$team_eleven')";
如何在使用$ database-> query
时添加查询由于
答案 0 :(得分:2)
在全部烘焙之前,请不要调用$ database->查询:
$queryString = "INSERT INTO pool_" . $pool_name . " (team_name) VALUES('$team_one'), ('$team_two'), ('$team_three'), ('$team_four'), ('$team_five'),
('$team_six'), ('$team_seven'), ('$team_eight')";
然后根据需要你可以:
$queryString .=", ('$team_nine'), ('$team_ten'), ('$team_eleven')";
然后最后
$query=$database->query($queryString);
编辑:甚至更好,使用PDO prepared statements
的魔力$stmt = $dbh->prepare("INSERT INTO pool_" . $pool_name . " (team_name) VALUES (:team_name)");
$stmt->bindParam(':team_name', $team_name);
$stmt->execute();
还有更多要插入的内容吗?
$team_name = $team_nine;
$stmt->execute();
$team_name = $team_ten;
$stmt->execute();
等