php - Information not being entered into mysql database using PDO -
i have register page user enters name , email , send them activation email. working told need use pdo make more secure. right when click submit runs through without errors user not added database. here code:
<? session_start(); include 'db.php'; $dbh = new pdo("mysql:host=$dbhost;dbname=$database_name", $dbusername, $dbpasswd); // define post fields 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 slashes in case user entered 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 following required information! <br />'; if(!$username){ echo "username required field. please enter below.<br />"; } if(!$email_address){ echo "email address required field. please enter below.<br />"; } include 'register.html'; // show form again! /* end error checking , if ok, we'll move on creating user account */ exit(); //if error checking has failed, we'll exit script! } if ( $password <> $confirm_password ){ echo "<br /><strong><div style=color:#ff0000;><center>password , confirm password not match!<br></center></div></strong>"; include 'register.html'; exit(); } /* let's checking , ensure user's email address or username not exist in database */ $sql_email_check = mysql_query("select email_address users email_address='$email_address'"); $sql_username_check = mysql_query("select username users 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 following errors: </div><br /><br />"; if($email_check > 0){ echo "<strong><div style=color:#ff0000;><center>your email address has been used member in our database. please submit different email address!</div><br />"; unset($email_address); } if($username_check > 0){ echo "<strong><div style=color:#ff0000;><center>the username have selected has been used member in our database. please choose different username!</div><br />"; unset($username); } include 'register.html'; // show form again! exit(); // exit script not create account! } /* has passed both error checks have done. it's time create account! */ $stmt = $dbh->prepare("insert 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 error creating account. please contact webmaster.'; } else { $userid = mysql_insert_id(); // let's mail user!
to last inserted id using pdo (not mysql_insert_id()
) you'd this:
$userid = $dbh->lastinsertid(); // let's mail user!
to convert rest of mysql_*
queries pdo, you'd want this:
$sql_email_check = $dbh->prepare("select email_address users email_address = :email"); $sql_email_check->execute([':email' => $email_address]); $email_check = $sql_email_check->rowcount(); $sql_username_check = $dbh->prepare("select username users username = :username"); $sql_username_check->execute([':username' => $username]); $username_check = $sql_username_check->rowcount(); if (($email_check > 0) || ($username_check > 0)) { // ... }