Php 无法解决此问题:致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[HY093]:无效参数编号:未定义参数”
这是我的代码,它不像我从头文件调用方法的最后一行。它声明无效的参数号,即使我调用的方法使用相同数量的参数。我不理解这个参数在调用之前没有定义,因为$params是在调用之前定义的,并且$query从另一个文件传递了一个值。我曾尝试使用点运算符而不是加号来定义$params,但没有帮助。下面是我对值为$query的uploadImage方法的调用:Php 无法解决此问题:致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[HY093]:无效参数编号:未定义参数”,php,pdo,Php,Pdo,这是我的代码,它不像我从头文件调用方法的最后一行。它声明无效的参数号,即使我调用的方法使用相同数量的参数。我不理解这个参数在调用之前没有定义,因为$params是在调用之前定义的,并且$query从另一个文件传递了一个值。我曾尝试使用点运算符而不是加号来定义$params,但没有帮助。下面是我对值为$query的uploadImage方法的调用: Image::uploadImage('postimg', "UPDATE dry_posts SET postimg=:postimg WH
Image::uploadImage('postimg',
"UPDATE dry_posts SET postimg=:postimg WHERE id=:postid", array(':postid'=>$postid));
该文件用于将来自另一个文件中表单的图像上载到imgur
<?php
include_once("connect.php");
class Image
{
public static function uploadImage($formname,$query,$params)
{
$formname = "";
$response = "";
$image = "";
if(file_exists($formname))
{
$image = base64_encode(file_get_contents($_FILES[$formname]['tmp_name']));
}
$options = array('http'=>array(
'method'=>"POST",
'header'=>"Authorization: Bearer 8cd9d1bb71401737dc720f9381d8e9763f580af6\n".
"Content-Type: application/x-www-form-urlencoded",
'content'=>$image
));
$context = stream_context_create($options);
$imgurURL = "https://api.imgur.com/3/image";
if ($_FILES[$formname]['size'] > 10240000) {
die('Image too big, must be 10MB or less!');
}
if(file_exists($formname))
{
$response = file_get_contents($imgurURL, false, $context);
}
$response = json_decode($response);
//$prearams = array($formname=>$response->data->link);
//$params = $preparams + $params;
$params=array(':postid'=>$postid)+array(':postimg'=>$postimg);
connect::query($query,$params);
}
}
?>
你的问题来自:
Image::uploadImage('postimg',
"UPDATE dry_posts SET postimg=:postimg WHERE id=:postid", array(':postid'=>$postid));
您要求:postimg和:postid,但只提供$postid
让我们通过添加:postimg值来解决此问题:
当然,您必须填充$postimg值
也要修正这一行:
$params=array(':postid'=>$postid)+array(':postimg'=>$postimg);
从Image::uploadImage函数。应该是:
$params = array(':postid' => $params['postid'], ':postimg' => $params['postimg']);
希望能有帮助 好的,我试着像这样填充$postimg=connect::query'SELECT postimg FROM dry_posts';即使填写$postimg,我也会犯同样的错误。我仍然会犯同样的错误。你确定$postimg的值是多少吗?尝试var_dump它并检查这是否是您要查找的字符串。$postimg是另一个文件中某个表单的变量。也请修复此行:$params=array':postid'=>$postid+array':postimg'=>$postimg; 从Image::uploadImage函数。应该是:$params=array:postid'=>$params['postid'],':postimg'=>$params['postimg']@布巴:太棒了!谢谢你验证我的答案,如果它解决了你的问题。祝你好运
$params = array(':postid' => $params['postid'], ':postimg' => $params['postimg']);