簡體   English   中英

PDO MySQL每次插入兩次?

[英]PDO MySQL inserting twice each time?

每次填寫表格時,系統兩次將數據插入表中嗎? 為什么會這樣呢?

// This checks if variabes are not empty, and if username is less than 11 characters long.
if (!empty($username) && !empty($password) && !empty($message) && strlen($username) < 11) {

    // Shortcut for our ID generator function.
    $ID = generateID();

    // Now lets insert (register the account) these datas to our database.
    $insert = $connect->prepare(
    "INSERT INTO users ( 
        username, 
        password, 
        message, 
        `date`, 
        `time`, 
        id
    ) VALUES ( 
        :username, 
        :password, 
        :message, 
        CURDATE(), 
        CURTIME(), 
        :id 
    )");

    // Executing in array, because if we binded all these variables, it would look very bad.
    $insert->execute(array(
        ':username' => $username,       
        ':password' => $password, 
        ':message' => $message, 
        ':id' => $ID
    ));

    if (!$insert->execute()) {
        print_r($insert->errorInfo());
    }       
}
// Let's check if errors is not empty.
else if (!empty($errors)) 
{
    // Now let's use a loop to get all of the error messages set in the array.
    foreach ($errors as $error) {
        echo $error;
    }
}

我對PDO還是很陌生,這是我第一次完成PDO連接,執行,查詢。 我在那里沒發現什么問題嗎? 謝謝。

生成ID功能

    $nums = substr(hash('sha512', '123456789'), 0, 20);
    $ID = str_shuffle($nums);
    return $ID;

HandleErrors函數

function handleErrors() {
    // Create new array to handle errors
    $errors = array();

    // Setting our errors

    // If username is empty, set error.
    if (empty($username)) {
        $errors[] = 'Username is empty';
    } 
    // If password is empty, set error.
    else if (empty($password)) 
    {
        $errors[] = 'Password is empty';
    } 
    // If username length is more than 11, set error.
    else if (strlen($username) > 11)
    {
        $errors[] = 'Your username is more than 11 characters long';
    }
}

其他:

<?php
require('inc/config.inc.php');
require('inc/session.inc.php');
include('inc/functions.inc.php');
?>

<html>
<h1>Recover</h1>
<br />


<form action="recover.php" method="POST">
    Your name: <input type="text" name="username"><br />
    Password for this recover: <input type="password" name="password"><br />
    Your Message: <input type="text" name="message"><br />
    <input type="submit" name="submit"><br />
</form>

<?php
    // Check if isset, also fixes the php notices error.
    if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['message'])) {
        // Created shortcuts for the POSTs we currently have.
        $username = $_POST['username'];
        $password = $_POST['password'];
        $message = $_POST['message'];
    }

    // Let's call our error handling function from functions.inc.php - before we doing the checking.

    handleErrors();

您調用兩次insert-> execute:

$insert->execute(array(
    ':username' => $username,       
    ':password' => $password, 
    ':message' => $message, 
    ':id' => $ID
));

if (!$insert->execute()) {
    print_r($insert->errorInfo());
}       

您執行兩次。

$insert->execute(array(...

if (!$insert->execute()) { ...

你想要的是

if( !$insert->execute(array(
    ':username' => $username,       
    ':password' => $password, 
    ':message' => $message, 
    ':id' => $ID)) ) {
        print_r($insert->errorInfo());
}

您執行兩次,

1)使用Try Catch語句檢查錯誤

2),也可以填充數組,然后在if(!$ insert-> execute()){。中運行它。

3)您可以運行一次並檢查數據庫中的受影響的行,這將告訴您命令是否已執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM