准备好的SQL语句INSERT

时间:2016-08-16 20:04:16

标签: php mysql sql

我目前正在使用准备好的SQL语句关注W3Schools教程,当我尝试将数据插入数据库时​​,它说$ firstname =“Nathan”和$ lastname =“Kent”是一个未使用的局部变量?它也没有返回任何错误,因此连接正常,当我检查数据库时它没有新的条目。

 function setData(){
    global $servername;
    global $username;
    global $password;
    global $dbname;

    $conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
     if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
     } else{
         echo "Connection Successful" . "<br>";
   }
    $stmt = $conn->prepare("INSERT INTO test(firstname, surname) VALUES       (?,?)");
    $stmt->bind_param("ss", $firstname, $surname);

    $firstname = "James";
    $lastname = "Williams";
    $stmt->execute();

    echo "New Records Created";

    $stmt->close();
    $conn->close();

}

2 个答案:

答案 0 :(得分:0)

使用MYSQLI,你实际上需要寻找错误,而不是假设他们会对你大喊大叫。

function setData(){
    global $servername;
    global $username;
    global $password;
    global $dbname;

    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
    } else{
         echo "Connection Successful" . "<br>";
    }
    $stmt = $conn->prepare("INSERT INTO test(firstname, surname) VALUES (?,?)");

    // add error check
    if ( $stmt === false ) {
        echo $conn->error;
        exit;
    }

    // Its not necessary to load the variables before the `bind_param`
    // but as you dont actually have these variables yet you do
    $firstname = "James";
    $lastname = "Williams";

    $stmt->bind_param("ss", $firstname, $surname);

    $status = $stmt->execute();

    // add error check
    if ( $status === false ) {
        echo $conn->error;
        exit;
    }


    echo "New Records Created";

    $stmt->close();
    $conn->close();

}

您可以通过连接到主线代码中的数据库并将连接参数传递给函数以及数据参数

来清理它。
function setData($conn, $firstname, $lastname){

    $stmt = $conn->prepare("INSERT INTO test(firstname, surname) VALUES (?,?)");

    // add error check
    if ( $stmt === false ) {
        echo $conn->error;
        exit;
    }

    $stmt->bind_param("ss", $firstname, $surname);

    $status = $stmt->execute();

    // add error check
    if ( $status === false ) {
        echo $conn->error;
        exit;
    }

    return "New Records Created";

    $stmt->close();
    $conn->close();

}


$servername = '127.0.0.1';
$username   = 'root';
$password   = 'veryStRongPassPhrASe';
$dbname     = 'mydatabase';

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} else{
     echo "Connection Successful" . "<br>";
}


$msg = setData($conn, 'Fred', 'Bloggs');
echo $msg;

答案 1 :(得分:0)

您没有为$surname定义值:

$stmt->bind_param("ss", $firstname, $surname);

$firstname = "James";
$lastname = "Williams";
$stmt->execute();

$lastname更改为$surname