Php 警告:PDO::exec()只需要1个参数,0在(…)中给出

Php 警告:PDO::exec()只需要1个参数,0在(…)中给出,php,pdo,parameter-passing,prepared-statement,Php,Pdo,Parameter Passing,Prepared Statement,大家好 我自己也在做一个项目有一段时间了。现在我正在吃它。我知道关于它也有类似的问题,但我也读过,但没有成功。 在这种情况下,我总是得到一个链接到第26行的警告输出。 我在许多论坛上搜索以获得答案,为什么它不起作用,以及在exec(?)中应该给出什么样的参数?显然,exec($stmt)不起作用,它必须是一个字符串 你们中的一些人说exe()应该被execute()替换,但它不起作用。相反,有一个类似“调用未定义的方法PDO::execute()”(如果我使用$con->…而不是$stmt->…

大家好

我自己也在做一个项目有一段时间了。现在我正在吃它。我知道关于它也有类似的问题,但我也读过,但没有成功。 在这种情况下,我总是得到一个链接到第26行的警告输出。 我在许多论坛上搜索以获得答案,为什么它不起作用,以及在exec(?)中应该给出什么样的参数?显然,exec($stmt)不起作用,它必须是一个字符串

你们中的一些人说exe()应该被execute()替换,但它不起作用。相反,有一个类似“调用未定义的方法PDO::execute()”(如果我使用$con->…而不是$stmt->…)

如果你能帮助我并给我一个简短的解释,我将不胜感激


谢谢,祝你今天愉快:)

你执行的是语句,而不是连接()。所以你应该这样做:

if (isset($_POST["add_employee"]))
{
    $result = false;

    global $con; 

    $stmt = $con->prepare('INSERT INTO Employees(Name, Address, ZipCode, City)
                VALUE($full_name, $address, $zip_code, $city)');

    $stmt->bindParam($full_name, $_POST['full name']);
    $stmt->bindParam($address, $_POST['address']);
    $stmt->bindParam($zip_code, $_POST['zip_code']);
    $stmt->bindParam($city, $_POST['city']);

    $result = $con->exec($stmt);
用于未准备好的查询,例如

$stmt->bindParam($full_name, $_POST['full name']);
$stmt->bindParam($address, $_POST['address']);
$stmt->bindParam($zip_code, $_POST['zip_code']);
$stmt->bindParam($city, $_POST['city']);

$result = $stmt->execute();
此外,由于占位符不会展开,您的查询也不会按您的想法工作。如果占位符位于变量中(例如
$full\u name=':full\u name'
),则需要使用双引号:

$con->exec("DELETE FROM fruit WHERE colour = 'red'");
否则,您将需要使用实际占位符,如下所示:

$stmt = $con->prepare("INSERT INTO Employees(Name, Address, ZipCode, City)
            VALUE($full_name, $address, $zip_code, $city)");

您执行的是语句,而不是连接()。所以你应该这样做:

if (isset($_POST["add_employee"]))
{
    $result = false;

    global $con; 

    $stmt = $con->prepare('INSERT INTO Employees(Name, Address, ZipCode, City)
                VALUE($full_name, $address, $zip_code, $city)');

    $stmt->bindParam($full_name, $_POST['full name']);
    $stmt->bindParam($address, $_POST['address']);
    $stmt->bindParam($zip_code, $_POST['zip_code']);
    $stmt->bindParam($city, $_POST['city']);

    $result = $con->exec($stmt);
用于未准备好的查询,例如

$stmt->bindParam($full_name, $_POST['full name']);
$stmt->bindParam($address, $_POST['address']);
$stmt->bindParam($zip_code, $_POST['zip_code']);
$stmt->bindParam($city, $_POST['city']);

$result = $stmt->execute();
此外,由于占位符不会展开,您的查询也不会按您的想法工作。如果占位符位于变量中(例如
$full\u name=':full\u name'
),则需要使用双引号:

$con->exec("DELETE FROM fruit WHERE colour = 'red'");
否则,您将需要使用实际占位符,如下所示:

$stmt = $con->prepare("INSERT INTO Employees(Name, Address, ZipCode, City)
            VALUE($full_name, $address, $zip_code, $city)");

在这里,适当的解决方案是:

$stmt = $con->prepare('INSERT INTO Employees(Name, Address, ZipCode, City)
            VALUE(:full_name, :address, :zip_code, :city)');
$stmt->bindParam(':full_name', $_POST['full name']);
$stmt->bindParam(':address', $_POST['address']);
$stmt->bindParam(':zip_code', $_POST['zip_code']);
$stmt->bindParam(':city', $_POST['city']);
if(设置($\u POST[“添加员工”]))
{   
试一试{
$result=false;
全球$con;
$sql=“插入员工(姓名、地址、邮编、城市)值(:全名、地址、邮政编码、城市)”;
$stmt=$con->prepare($sql);
$stmt->bindParam(':full_name',$_POST[“full name”],PDO::PARAM_STR);
$stmt->bindParam(':address',$\u POST[“address”],PDO::PARAM_STR);
$stmt->bindParam(':zip_code',$_POST[“zip_code”],PDO::PARAM_INT);
$stmt->bindParam(':city',$\u POST[“city”],PDO::PARAM\u STR);
$result=$stmt->execute();
}捕获(异常$result){
如果($result==false){
echo“添加新员工时出错!
”; } 否则{ 回显“新员工成功插入!
”; } } }
在这里,适当的解决方案是:

$stmt = $con->prepare('INSERT INTO Employees(Name, Address, ZipCode, City)
            VALUE(:full_name, :address, :zip_code, :city)');
$stmt->bindParam(':full_name', $_POST['full name']);
$stmt->bindParam(':address', $_POST['address']);
$stmt->bindParam(':zip_code', $_POST['zip_code']);
$stmt->bindParam(':city', $_POST['city']);
if(设置($\u POST[“添加员工”]))
{   
试一试{
$result=false;
全球$con;
$sql=“插入员工(姓名、地址、邮编、城市)值(:全名、地址、邮政编码、城市)”;
$stmt=$con->prepare($sql);
$stmt->bindParam(':full_name',$_POST[“full name”],PDO::PARAM_STR);
$stmt->bindParam(':address',$\u POST[“address”],PDO::PARAM_STR);
$stmt->bindParam(':zip_code',$_POST[“zip_code”],PDO::PARAM_INT);
$stmt->bindParam(':city',$\u POST[“city”],PDO::PARAM\u STR);
$result=$stmt->execute();
}捕获(异常$result){
如果($result==false){
echo“添加新员工时出错!
”; } 否则{ 回显“新员工成功插入!
”; } } }
代码图像对任何人都没有任何好处,尤其是你自己。是的,你是对的。我把它删除了,只是贴了一段代码。代码的图像对任何人都没有任何好处,尤其是你自己。是的,你是对的。我删除了它,只发布了一段代码。你需要修复他们的查询。恐怕是这样。我想他们可能不知道怎么做。嗯……我想我会继续努力并尝试一下。不过,我很感谢你的帮助和贡献。嘿,老兄,我能解决这个问题。首先,你说的“execute()”是对的,我现在明白了。其次,我在所有“bindParam”中添加了第三个参数PDO::PARAM(例如PDO::PARAM_STR)。此外,我创建了一个“try/catch-block”,将代码片段放入其中,并在catch短语中写道:PDOExeception$result{…}您需要修复它们的查询。恐怕是这样。我想他们可能不知道怎么做。嗯……我想我会继续努力并尝试一下。不过,我很感谢你的帮助和贡献。嘿,老兄,我能解决这个问题。首先,你说的“execute()”是对的,我现在明白了。其次,我在所有“bindParam”中添加了第三个参数PDO::PARAM(例如PDO::PARAM_STR)。此外,我创建了一个“try/catch-block”,并将代码片段放入其中,并在catch短语中编写:PDOExeception$result{…}