【发布时间】:2019-03-22 05:50:07
【问题描述】:
我一直在为课堂上的一个项目做一些php开发,我遇到了一个问题。
以下函数在与我自己输入的参数一起使用时应该返回true,但它返回false:
public function check_if_in($table, $condition){
$request="SELECT *"; // Selecting one column
$request=$request.' FROM '.$table.' WHERE '; // From the table i want to check in
$keys = array_keys($condition);
foreach($condition as $clé=>$val){
if(!($clé == end($keys))){ // If it's not the last condition
$request = $request.$clé." = :".$clé." AND "; // add AND
}
else{
$request = $request.$clé." = :".$clé.";"; // Add a semicolon
}
}
try {
$statement = $this->pdo->prepare($request); // Prepare the statement
}
catch (PDOException $e){
die("Erreur array :" . $e->getMessage());
}
foreach($condition as $clé=>$val) {
$statement->bindValue($clé, '%'.$val.'%'); // Binding all the parameters
}
try {
$statement->execute();
}
catch (PDOException $e){
die("Error :" . $e->getMessage());
}
if($statement->rowCount() > 0){
return true;
}
else {
return false;
}
}
请问哪里出了问题?
【问题讨论】:
-
您错误地使用了准备好的语句,关键是不要连接 any 字符串,其中包括您的表名(除非以另一种方式验证,因为您当前的代码容易受到SQL 注入)。
-
您好,表名不是用户输入的,我自己定义的。编辑:这个功能是在多个php页面中使用的,这是我老师的命令
-
尝试回显您的查询只是为了验证它是否正确,然后使用引用的值将其直接提交到您的数据库。如果您有权访问数据库服务器,则可以在运行脚本之前打开 MySQL 日志记录 (
SET GLOBAL general_log = 1;),然后将其关闭,然后检查 mysql 日志以找到它正在传递的确切查询。 -
嗨,我会这样做,但因为我在大学,我不能这样做。