【问题标题】:Update data baseSQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens更新数据库SQLSTATE[HY093]: Invalid parameter number: 绑定变量数与token数不匹配
【发布时间】:2016-05-24 06:05:42
【问题描述】:

当用户登录并想要更新配置文件时,我正在做一个 php 文件。

我收到 SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens.

EditProfile.php

<?php
session_start();
require_once 'class.user.php';
$user_home = new USER();


$stmt = $user_home->runQuery("SELECT * FROM registered_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$_SESSION['email'] = $row['email'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Update Data From MySql - By Cleartuts</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>

<h1>Edit Particulars</h1>

<div id="body">
    <div id="content">
    <table align="center" width="100%">


            <tr>
            <td>Full Name</td>
            <td><?php echo $row["fullName"]; ?></td>

            </tr>
            <tr>
            <td>Mobile Number</td>
            <td><?php echo $row['mobileNumber']; ?></td>
            </tr>

            <tr>
            <td>Password</td>
            <td><?php echo $row ['password']; ?></td>
            </tr>

            <tr>
            <td>Address</td>
            <td><?php echo $row['address']; ?></td>
            </tr>

            <tr>
            <td>Postal Code</td>
            <td><?php echo $row['postalCode']; ?></td>
            </tr>

            <tr>
            <td>Edit</td>
            <td><a href="edit_data.php?edit_id=<?php echo $row[0]; ?>"><img src="b_edit.png" alt="Edit" /></a></td>
           </tr>

    </table>
    </div>
</div>


</center>
</body>
</html>

edit_data.php

<?php
session_start();
require_once 'class.user.php';
$user_home = new USER();
$reg_user = new USER();

$stmt = $user_home->runQuery("SELECT * FROM registered_users WHERE userID=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$email=$_SESSION['email'];
if(isset($_GET['edit_id']))
{
    $stmt = $user_home->runQuery("SELECT * FROM registered_users WHERE userID=:uid".$_GET['edit_id']);


}
if(isset($_POST['btn-update']))
{
    // variables for input data

    $fullName = $_POST['fullName'];

    $mobileNumber = $_POST['mobileNumber'];

    $password = $_POST['password'];
    $address = $_POST['address'];
    $postalCode = $_POST['postalCode'];
    // variables for input data

    // sql query for update data into database

        if($reg_user->updateUser($fullName,$mobileNumber,$password,$address,$postalCode,$email))
        {

            echo"good";
            ?>
            <script>
            alert('Data Are Updated Successfully');
            window.location.href='home.php';
            </script>
            <?php 
        }
        else
        {
            echo "sorry ,Pleae go to nearest NPC to register.";
            ?>
            <script>
            alert('error occured while updating data');
            </script>
            <?php 
        }



}
if(isset($_POST['btn-cancel']))
{
    header("Location: home.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Update Data From MySql - By Cleartuts</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>

<div id="header">
    <div id="content">
    <label>PHP PHP Update Data From MySql - By Cleartuts</label>
    </div>
</div>
<div id="body">
    <div id="content">
    <form method="post">
    <table align="center">
    <tr>
    <td>Full Name</td>
    <td><input type="text" name="fullName" placeholder="full Name" value="<?php echo $row['fullName']; ?>" required /></td>
    </tr>
    <tr>
    <td>Mobile Number</td>
    <td><input type="text" name="mobileNumber" placeholder="mobile Number" value="<?php echo $row['mobileNumber']; ?>" required /></td>
    </tr>

    <tr>
    <td>Password</td>
    <td><input type="password" name="password" placeholder="password" value="<?php echo $row['password']; ?>" required /></td>
    </tr>

    <tr>
    <td>Address</td>
    <td><input type="text" name="address" placeholder="Blk 123 Ang Mo kio Ave 1 #12-112" value="<?php echo $row['address']; ?>" required /></td>
    </tr>

    <tr>
    <td>Postal Code</td>
    <td><input type="text" name="postalCode" placeholder="123456" value="<?php echo $row['postalCode']; ?>" required /></td>
    </tr>
    <tr>
    <td>
    <button type="submit" name="btn-update"><strong>UPDATE</strong></button>
    <button type="submit" name="btn-cancel"><strong>Cancel</strong></button>
    </td>
    </tr>
    </table>
    </form>
    </div>
</div>



</center>
</body>
</html>

功能

public function updateUser($fullName,$mobileNumber,$password,$address,$postalCode,$email)
    {
        try
        {

            $password = md5($password);
            $stmt = $this->conn->prepare("UPDATE SET  registered_users (fullName=:fullName,mobileNumber=:mobileNumber,password=:password,address=:address,postalCode=:postalCode WHERE email=:email_id");
            $stmt->execute(array(":email_id"=>$email));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);

            $stmt->bindparam(":fullName",$fullName);

            $stmt->bindparam(":mobileNumber",$mobileNumber);

            $stmt->bindparam(":password",$password);
            $stmt->bindparam(":address",$address);
            $stmt->bindparam(":postalCode",$postalCode);



            $stmt->execute();
            return $stmt;
        }
        catch(PDOException $ex)
        {
            echo $ex->getMessage();
        }
    }

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    你有更多参数(:fullName,:mobileNumber,:password,......)

    但你只绑定一个:email_id

    $stmt = $this->conn->prepare("UPDATE SET  registered_users  
           (fullName=:fullName,mobileNumber=:mobileNumber,
           password=:password,address=:address,postalCode=:postalCode WHERE email=:email_id");
            $stmt->execute(array(":email_id"=>$email));
    

    【讨论】:

    • $stmt = $this->conn->prepare("UPDATE SET registered_users (fullName=:fullName,mobileNumber=:mobileNumber,password=:password,address=:address,postalCode=:postalCode WHERE email=:email_id"); $stmt->execute(array(":email_id"=>$email)); $userRow=$stmt->fetch(PDO::FETCH_ASSOC); $stmt->bindparam(":fullName ",$fullName); $stmt->bindparam(":mobileNumber",$mobileNumber); $stmt->bindparam(":password",$password); $stmt->bindparam(":address",$address) ;$stmt->bindparam(":postalCode",$postalCode);
    • @peter .. 我不明白您的评论 .. 当您需要显示代码时更新您的问题 .. 或获取您的论点的简要描述.. 无论如何您执行 $stmt->execute ..在绑定之前..检查这是否正确
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 2015-08-12
    相关资源
    最近更新 更多