麻烦使用带有select查询的预准备语句

时间:2018-04-13 00:51:26

标签: php

获取以下内容

mysqli_sql_exception:SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以便在#"'附近使用正确的语法。在第55行的C:\ Users \ user \ Documents \ examplePHP \ UAMP \ UwAmp \ www \ php-project \ update.php第1行

使用以下

//getting id from url sent from the select.php 
$StaffID = $_GET['StaffID'];


        // Prepare an insert statement
        $sql = mysqli_query($conn, "SELECT Name, Address, Telephone, BusinessID FROM staff WHERE StaffID = ?");

        if($stmt = mysqli_prepare($conn, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "i",$param_staffid);

            // Set parameters

            $param_staffid = $StaffID;


            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Redirect to login2 page if successful
               while($row = mysqli_fetch_array($result))
{
    $Name = $row['Name'];
    $Address = $row['Address'];
    $Telephone = $row['Telephone'];
    $BusinessID = $row['BusinessID'];
}
            } 
            else{
                 die("ERROR: Could not insert. " . mysqli_connect_error());
}

}

2 个答案:

答案 0 :(得分:2)

您应该将$sql设置为字符串,而不是在那里调用mysqli_query()

$sql = "SELECT Name, Address, Telephone, BusinessID FROM staff WHERE StaffID = ?";

您在致电mysqli_stmt_execute()时执行查询。

答案 1 :(得分:0)

以这种方式更改查询: -

$sql = "SELECT Name, Address, Telephone, BusinessID FROM staff WHERE StaffID = ?";
if($stmt = mysqli_prepare($conn, $sql)){

    // the rest of your code

if(mysqli_stmt_execute($stmt))
{
    // the rest of your code
} 
else
{
    // the rest of your code
}

注意: - check this