423,319 Members | 2,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,319 IT Pros & Developers. It's quick & easy.

Converting PDO OOP To Mysqli Procedural

P: 39
Folks, :)

My 3+ pages are all in mysqli procedural. I cannot just switch to pdo and oop and throw 6 mnths of work down the drain! And so, let's try converting the following code suggestion by Death Shadow to mysqli procedural.


Expand|Select|Wrap|Line Numbers
  1. if (
  2.     array_key_exists('login_username_or_email', $_POST) &&
  3.     array_key_exists('login_password'], $_POST)
  4. ) {
  5.  
  6.     // don't bother trimming, they can't enter it right, don't let them log in!
  7.  
  8.     $stmt = $conn->prepare('
  9.         SELECT ids, usernames, passwords, emails, accounts_activations_statuses
  10.         FROM users
  11.         WHERE ' . (
  12.             strpos($usernameOrEmail, '@') === false) ? 'usernames' : 'emails'
  13.         ) . ' = ?
  14.     ');
  15.     $stmt->bind_param('s', $_POST['login_username_or_email']);
  16.     $stmt->execute();
  17.     $stmt->bind_result(
  18.         $db_id, $db_username, $db_password, $db_email,
  19.         $db_account_activation_status
  20.     );
  21.  
  22.     if (
  23.         $stmt->fetch() &&
  24.         password_verify($_POST['login_password'], $db_password)
  25.     ) {
  26.         echo '
  27.             <p>Login Successful</p>
  28.             <dl>
  29.                 <dt>User Id</dt>
  30.                 <dd>', $db_id, '</dd>
  31.                 <dt>E-Mail</dt>
  32.                 <dd>', $db_email, '</dd>
  33.                 <dt>Username</dt>
  34.                 <dd>', $db_username, '</dd>
  35.                 <dt>Activation Stats</dt>
  36.                 <dd>', $db_account_activation_status, '</dd>
  37.             </dl>
  38.         ';
  39.     } else echo '<p>Invalid username or password</p>';
  40.  
  41.     $stmt->close();
  42.  
  43. } else echo '<p>Missing username or password</p>';
  44.  
I need your help.
Remember, the script is a login page and the user is given a choice to either type his email or username. And then finally the password.
The html form looks like this:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title><?php $site_name?> Member Login Page</title>
  5.   <meta charset="utf-8">
  6. </head>
  7. <body>
  8. <form method="post" action="">
  9.     <h3><?= $site_name ?> Member Login Form</h3>
  10.     <fieldset>
  11.         <label for="login_name">Username/Email:</label>
  12.         <input type="text" name="login_username_or_email" id="login_name" value="<?php if(isset($_COOKIE["login_username_or_email"])) echo $_COOKIE["login_username_or_email"]; ?>"
  13.         <br>
  14.         <label for="login_pass">Password:</label>
  15.         <input type="password" name="login_password" id="login_pass" value="<?php if(isset($_COOKIE["login_password"])) echo $_COOKIE["login_password"]; ?>">
  16.     </fieldset>
  17.     <div class="submitsAndHiddens">
  18.         <label for="login_remember">Remember Login Details:</label>
  19.         <input type="checkbox" name="login_remember" />
  20.         <br>
  21.         <button type="submit">Login</button>
  22.         <br>
  23.         <a href="login_password_reset.php">Forgot your Password ? Reset it here!</a>
  24.         <br>
  25.         <a href="register.php">Register here!</a>
  26.     </div>
  27. </form>
  28.  
  29. </body>
  30. </html>
  31.  

The following is regex to check if the user typed email or not.
Newbies, if you were after a regex that checks if the input is email or not. Then, here it is:
Expand|Select|Wrap|Line Numbers
  1. function valid_email($email) { 
  2.     if(preg_match('/^([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])*$/',$email)) { 
  3.         return TRUE; 
  4.     } else { 
  5.         return FALSE; 
  6.     } 
  7. }  
  8.  
I need help adding the above regex in the appropriate place in the script. Appropriate integrationing.

:thumbsup:
Sep 29 '17 #1
Share this Question
Share on Google+
1 Reply


P: 39
Folks!

I really need the following code converted to mysqli procedural from pdo oop. Once that is done, my 7 months project will come to an end. And, I can move-on to learning pdo. Right now, can't afford to jump into pdo without finishing my current project.
So, who will help me convert ? Other newbies would learn from your conversion.

Thanks!

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. /*
  4. ERROR HANDLING
  5. */
  6. declare(strict_types=1);
  7. ini_set('display_errors', '1');
  8. ini_set('display_startup_errors', '1');
  9. error_reporting(E_ALL);
  10. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  11.  
  12. include 'config.php';
  13.  
  14. // check if user is already logged in
  15. if (is_logged() === true) 
  16. {
  17.     //Redirect user to homepage page after 5 seconds.
  18.     header("refresh:2;url=home.php");
  19.     exit; //
  20. }
  21.  
  22. if (
  23.     array_key_exists('login_username_or_email', $_POST) &&
  24.     array_key_exists('login_password' , $_POST)
  25. ) {
  26.     $usernameoremail = trim($_POST["login_username_or_email"]); //
  27.     $password = $_POST["login_password"];
  28.  
  29.     // don't bother trimming, they can't enter it right, don't let them log in!
  30.  
  31.     $stmt = $conn->prepare('
  32.         SELECT ids, usernames, passwords, emails, accounts_activations_statuses
  33.         FROM users
  34.         WHERE ' . (
  35.             strpos($usernameoremail, '@') === false) ? 'usernames' : 'emails'
  36.         ) . ' = ?
  37.     ');
  38.  
  39.     $stmt->bind_param('s', $_POST['login_username_or_email']);
  40.     $stmt->execute();
  41.     $stmt->bind_result(
  42.         $db_id, $db_username, $db_password, $db_email,
  43.         $db_account_activation_status
  44.     );
  45.  
  46.     if (
  47.         $stmt->fetch() &&
  48.         password_verify($_POST['login_password'], $db_password)
  49.     ) {
  50.         echo '
  51.             <p>Login Successful</p>
  52.             <dl>
  53.                 <dt>User Id</dt>
  54.                 <dd>', $db_id, '</dd>
  55.                 <dt>E-Mail</dt>
  56.                 <dd>', $db_email, '</dd>
  57.                 <dt>Username</dt>
  58.                 <dd>', $db_username, '</dd>
  59.                 <dt>Activation Stats</dt>
  60.                 <dd>', $db_account_activation_status, '</dd>
  61.             </dl>
  62.         ';
  63.     } else echo '<p>Invalid username or password</p>';
  64.  
  65.     $stmt->close();
  66.  
  67. } else echo '<p>Missing username or password</p>';
  68.  
  69.  
  70.  
  71. ?>
  72.  
  73. <!DOCTYPE html>
  74. <html>
  75. <head>
  76. <title><?php $site_name?> Member Login Page</title>
  77.   <meta charset="utf-8">
  78. </head>
  79. <body>
  80. <div class = "container">
  81. <form method="post" action="">
  82.     <h3><?= $site_name ?> Member Login Form</h3>
  83.     <fieldset>
  84.         <label for="login_name">Username/Email:</label>
  85.         <input type="text" name="login_username_or_email" id="login_name">
  86.         <br>
  87.         <label for="login_pass">Password:</label>
  88.         <input type="password" name="login_password" id="login_pass">
  89.     </fieldset>
  90.     <div class="submitsAndHiddens">
  91.         <button type="submit">Login</button><br>
  92.         <a href="login_password_reset.php">Forgot your Password?</a><br>
  93.         <a href="register.php">Register New Account</a>
  94.     </div>
  95. </form>
  96. </div>
  97. </body>
  98. </html>
  99.  
  100.  
Oct 6 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.