Php 第39行好像有错误,我想不出是什么,请给我指出我的错误?

Php 第39行好像有错误,我想不出是什么,请给我指出我的错误?,php,Php,创建了一个登录页面,但在执行时出现以下错误: $result->bindParam(1, $user, PDO::PARAM_STR); $result->bindParam(2, $password, PDO::PARAM_STR); 绑定变量的数量与..\login1.php第39行中的令牌数量不匹配 $result->bindParam(1, $user, PDO::PARAM_STR); $result->bindParam(2, $password, PDO:

创建了一个登录页面,但在执行时出现以下错误:

$result->bindParam(1, $user, PDO::PARAM_STR);
$result->bindParam(2, $password, PDO::PARAM_STR);
绑定变量的数量与..\login1.php第39行中的令牌数量不匹配

$result->bindParam(1, $user, PDO::PARAM_STR);
$result->bindParam(2, $password, PDO::PARAM_STR);
下面是login1.php中的代码,我做错了什么

 // query

    $result = $handle->prepare("SELECT * FROM user WHERE username= ? AND password = ?");
    $result->bindParam('?', $user, PDO::PARAM_STR);
    $result->bindParam('?', $password, PDO::PARAM_STR);
    $result->execute(); //line 39
    $rows = $result->fetch(PDO::FETCH_NUM);
$result->bindParam(1, $user, PDO::PARAM_STR);
$result->bindParam(2, $password, PDO::PARAM_STR);

在bindParam中,不要使用问号“?”而是使用参数索引位置,如

$result->bindParam(1, $user, PDO::PARAM_STR);
$result->bindParam(2, $password, PDO::PARAM_STR);
试着这样做

$result->bindParam(1, $user, PDO::PARAM_STR);
$result->bindParam(2, $password, PDO::PARAM_STR);
$result = $handle->prepare("SELECT * FROM user WHERE username= ? AND password = ?");
$result->bindParam(1, $user, PDO::PARAM_STR);
$result->bindParam(2, $password, PDO::PARAM_STR);
$result->execute(); //line 39
$rows = $result->fetch(PDO::FETCH_NUM);

您混淆了绑定值的两种不同方式。如果要使用通用占位符(即?),则需要按正确顺序向execute()传递一个值数组,例如
$result->execute([$user,$password])

$result->bindParam(1, $user, PDO::PARAM_STR);
$result->bindParam(2, $password, PDO::PARAM_STR);
如果要绑定值,则占位符必须是唯一的,例如,
“…其中username=:user AND password=:pass

$result->bindParam(1, $user, PDO::PARAM_STR);
$result->bindParam(2, $password, PDO::PARAM_STR);
$result->bindParam(':user', $user, PDO::PARAM_STR);
 $result->bindParam(':pass', $password, PDO::PARAM_STR);
后者的好处(您正在尝试使用)是您可以绑定一次,但可以在多个位置使用它,例如。
“…从(t.col=B.col)上的表t、内部联接表_B B中选择*,其中B.id=:id和t.id=:id….

$result->bindParam(1, $user, PDO::PARAM_STR);
$result->bindParam(2, $password, PDO::PARAM_STR);
$result->bindParam(':id', $id, PDO::PARAM_INT);
即使您不打算单独绑定,而是使用execute([array]),那么您仍然可以将绑定命名为

$result->bindParam(1, $user, PDO::PARAM_STR);
$result->bindParam(2, $password, PDO::PARAM_STR);
$result->execute([':bind1' => $val1,':bind2'=>$val2 etc....])

最好指定您的绑定,以最大限度地减少绑定错误值的机会

您是否已
var_dump
'ed
$result
以确保它是您想要的think@treyBake我应该在哪里输入var_dump命令,因为当输入它并试图使页面正常工作时,我仍然有相同的错误
var_dump($result);
->prepare
之后和
->bindParam
之后“在bindParam中,不要使用问号”?”使用参数索引位置,如
$result->bindParam(1,$user,PDO::PARAM_STR);$result->bindParam(2,$password,PDO::PARAM_STR);
最好显式命名绑定,然后如果需要,可以多次应用它们,并且在进行错误代码更改的相关更改时,将绑定错误值的可能性降至最低“以下行的未定义索引://new data$username=$\u POST['username'];$password=$\u POST['password'];然后您的post变量未定义,我们需要查看您的表单以提供帮助。确保每个输入都有一个名称集我只需要刷新连接并尝试再次执行页面,登录尝试成功,感谢您的建议。下一步是我将成功登录链接回主页,但我将保存我不能再等一天了。
$result->bindParam(1, $user, PDO::PARAM_STR);
$result->bindParam(2, $password, PDO::PARAM_STR);