Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDO MySQL每次插入两次?_Php_Mysql_Pdo - Fatal编程技术网

Php PDO MySQL每次插入两次?

Php PDO MySQL每次插入两次?,php,mysql,pdo,Php,Mysql,Pdo,每次我填写表单时,系统会将数据插入表中两次吗? 为什么会这样 // 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) { // S

每次我填写表单时,系统会将数据插入表中两次吗? 为什么会这样

// 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;
    }
}
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';
    }
}
杂项:


恢复

您的姓名:
此恢复的密码:
您的留言:


你执行了两次

$insert->execute(array(...

你想要的是

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

你执行了两次

$insert->execute(array(...

你想要的是

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

您正在调用insert->execute两次:

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

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

您正在调用insert->execute两次:

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

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

你执行了两次死刑

1) 要检查错误,请使用Try-Catch语句

2) 或者,您也可以填充数组,然后在if(!$insert->execute()){中运行它


3) 您可以运行一次并检查数据库中受影响的行,这将告诉您该命令是否已执行。

您执行了两次

1) 要检查错误,请使用Try-Catch语句

2) 或者,您也可以填充数组,然后在if(!$insert->execute()){中运行它


3) 您可以运行一次并检查数据库中受影响的_行,这将告诉您该命令是否已执行。

页面是否加载两次?否,但我包括functions.inc.php、session.inc.php(打开会话),需要config.inc.php来创建连接并对其进行测试。你为什么要在php中生成ID?你应该把它留给你的SQL->
int not null auto_increment主键。请刷新,我添加了GenerateID函数。你的页面加载了两次吗?没有,但我包括functions.inc.php,session.inc.php(打开一个会话),并且需要config.inc.php来创建连接并测试它。你为什么要在php中生成ID?你应该把它留给你的SQL->
int not null auto_increment主键
。请刷新,我添加了GenerateID函数。检查返回值。不要再次调用它。检查返回值。不要再次调用它。