php - 信息未使用 PDO 输入到 mysql 数据库中

标签 php mysql registration

我有一个注册页面,用户可以在其中输入他们的姓名和电子邮件,然后会向他们发送一封激活电子邮件。这很有效,但我被告知我需要使用 pdo 来使其更安全。现在,当我单击提交时,它会无误地运行所有内容,但不会将用户添加到数据库中。这是我的代码:

<?
session_start();

include 'db.php';
$dbh = new PDO("mysql:host=$dbhost;dbname=$database_name", $dbusername, $dbpasswd);


// Define post fields into simple variables
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$username = $_POST['username'];
$email_address = $_POST['email_address'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
$hash = password_hash($password, PASSWORD_DEFAULT);


/* Let's strip some slashes in case the user entered
any escaped characters. */

$first_name = stripslashes($first_name);
$last_name = stripslashes($last_name);
$username = stripslashes($username);
$email_address = stripslashes($email_address);



if((!$username) || (!$email_address)){
    echo 'You did not submit the following required information! <br />';
    if(!$username){
        echo "Username is a required field. Please enter it below.<br />";
    }
    if(!$email_address){
        echo "Email Address is a required field. Please enter it below.<br />";
    }
    include 'register.html'; // Show the form again!
    /* End the error checking and if everything is ok, we'll move on to
     creating the user account */
    exit();  //if the error checking has failed, we'll exit the script!
}


 if ( $password <> $confirm_password ){
    echo "<br /><strong><div style=color:#FF0000;><center>Password and confirm password do not match!<BR></center></div></strong>";
    include 'register.html';
    exit(); 
}


/* Let's do some checking and ensure that the user's email address or username
 does not exist in the database */

 $sql_email_check = mysql_query("SELECT email_address FROM users WHERE email_address='$email_address'");
 $sql_username_check = mysql_query("SELECT username FROM users WHERE username='$username'");

 $email_check = mysql_num_rows($sql_email_check);
 $username_check = mysql_num_rows($sql_username_check);

 if(($email_check > 0) || ($username_check > 0)){
    echo "<br /><div style=color:#FF0000;><center>Please fix the following errors: </div><br /><br />";
    if($email_check > 0){
        echo "<strong><div style=color:#FF0000;><center>Your email address has already been used by another member in our database. Please submit a different Email address!</div><br />";
        unset($email_address);
    }
    if($username_check > 0){
        echo "<strong><div style=color:#FF0000;><center>The username you have selected has already been used by another member in our database. Please choose a different Username!</div><br />";
        unset($username);
    }
    include 'register.html'; // Show the form again!
    exit();  // exit the script so that we do not create this account!
 }

/* Everything has passed both error checks that we have done.
It's time to create the account! */

$stmt = $dbh->prepare("insert into users set first_name=?, last_name=?, username=?, email_address=?, password=?");
$stmt->execute([$first_name, $lastname, $username, $email_address, $hash]);

if(!$stmt){
    echo 'There has been an error creating your account. Please contact the webmaster.';
} else {
    $userid = mysql_insert_id();
    // Let's mail the user!

最佳答案

要使用 PDO(不是 mysql_insert_id())获取最后插入的 ID,您可以这样做:

$userid = $dbh->lastInsertId();
// Let's mail the user!

要将其余的 mysql_* 查询转换为 PDO,您可能需要执行如下操作:

$sql_email_check = $dbh->prepare("SELECT email_address FROM users WHERE email_address = :email");
$sql_email_check->execute([':email' => $email_address]);
$email_check = $sql_email_check->rowCount();

$sql_username_check = $dbh->prepare("SELECT username FROM users WHERE username = :username");
$sql_username_check->execute([':username' => $username]);
$username_check = $sql_username_check->rowCount();

if (($email_check > 0) || ($username_check > 0)) {
    // ...
}

关于php - 信息未使用 PDO 输入到 mysql 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29813066/

相关文章:

php - 验证 Android 在 Magento 站点中传递的 paypal pay id

php - 脚本将相同的值插入数据库

php - SQLSTATE[HY093] : Invalid parameter number: number of bound variables does not match number of tokens error

java - Java应用程序的用户注册设计

php - 如何解决 Joomla 错误 "The template for this display is not available."

php - 显示 foreach 循环结果时出现问题

mysql - 将不同日期格式的csv上传到mysql

mysql - 在 mysql 中使用单个查询计算多个值

python - Django Registration Redux 注册表单设置

database - AngularFire2 如何创建用户(名字、姓氏、电子邮件、密码)