提交警告时出错:PDOStatement :: execute():SQLSTATE [HY093]

时间:2017-04-09 19:02:41

标签: php sql

警告:PDOStatement :: execute():SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配

美好的一天,我一直在看这个几个小时,我只是找不到任何帮助,为什么我一直得到这个错误会有所帮助。

if(isset($_POST['update']))
{
	$data = getPosts();
		if(empty($data[0]) || empty($data[1]) || empty($data[2]) || empty($data[3]))
		{
			echo 'Enter player id number to update';
		}	else {
				$updateStmt = $statement = $db->prepare('UPDATE players SET Id_num = :idnum, Name = :name, Surname = :sunrame, Club = :club WHERE Id_num = :idnum');
				$updateStmt->execute(array(
							':idnum' =>$data[0],
                                                        ':name' =>$data[1],
                                                        ':surname' =>$data[2],
                                                        ':club' =>$data[3]
				));
				if($updateStmt)
				
				{
				
                                  
					echo 'Player updated successfully';
                                   
                                }
			}
}

2 个答案:

答案 0 :(得分:0)

你有五个参数,那么你必须绑定5个值。

在你的情况下你必须分配idNum两次

            $updateStmt->execute(array(
                        ':idnum' =>$data[0],
                         ':name' =>$data[1],
                         ':surname' =>$data[2],
                         ':club' =>$data[3],
                         ':idnum' =>$data[0]
            ));

并且只是为了测试尝试使用不同的参数ref为idnum

    $updateStmt = $statement = $db->prepare('UPDATE players 
                SET Id_num = :idnum
                , Name = :name
                , Surname = :sunrame
                , Club = :club 
                WHERE Id_num = :idnum2');
    $updateStmt->execute(array(
                    ':idnum' =>$data[0],
                     ':name' =>$data[1],
                     ':surname' =>$data[2],
                     ':club' =>$data[3],
                     ':idnum2' =>$data[0]
        ));

答案 1 :(得分:0)

感谢所有答案。我发现了问题。

如果(空($ data [0])||不应该在那里它现在工作正常。

见下面的代码。



if(isset($_POST['update']))
{
	$data = getPosts();
		if(empty($data[1]) || empty($data[2]) || empty($data[3]))
		{
			echo 'Enter player id number to update';
		}	else {
				$updateStmt = $statement = $db->prepare('UPDATE players SET Name = :name, Surname = :surname, Club = :club WHERE Id_Num = :idnum');
				$updateStmt->execute(array(
                                                       ':idnum' =>$data[0],
                                                        ':name' =>$data[1],
                                                        ':surname' =>$data[2],
                                                        ':club' =>$data[3],
				));
				if($updateStmt)
				
				{
				
                                  
					echo 'Player updated successfully';
                                   
                                }
			}
}