Page 1 of 1

converting pdo oop to mysqli procedural

Posted: Fri Oct 06, 2017 7:24 pm
by UniqueIdeaMan
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!

Code: Select all

<?php
 
/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
 
include 'config.php';
 
// check if user is already logged in
if (is_logged() === true) 
{
	//Redirect user to homepage page after 5 seconds.
	header("refresh:2;url=home.php");
	exit; //
}

if (
	array_key_exists('login_username_or_email', $_POST) &&
	array_key_exists('login_password' , $_POST)
) {
	$usernameoremail = trim($_POST["login_username_or_email"]); //
	$password = $_POST["login_password"];

	// don't bother trimming, they can't enter it right, don't let them log in!

	$stmt = $conn->prepare('
		SELECT ids, usernames, passwords, emails, accounts_activations_statuses
		FROM users
		WHERE ' . (
			strpos($usernameoremail, '@') === false) ? 'usernames' : 'emails'
		) . ' = ?
	');
	
	$stmt->bind_param('s', $_POST['login_username_or_email']);
	$stmt->execute();
	$stmt->bind_result(
		$db_id, $db_username, $db_password, $db_email,
		$db_account_activation_status
	);
	
	if (
		$stmt->fetch() &&
		password_verify($_POST['login_password'], $db_password)
	) {
		echo '
			<p>Login Successful</p>
			<dl>
				<dt>User Id</dt>
				<dd>', $db_id, '</dd>
				<dt>E-Mail</dt>
				<dd>', $db_email, '</dd>
				<dt>Username</dt>
				<dd>', $db_username, '</dd>
				<dt>Activation Stats</dt>
				<dd>', $db_account_activation_status, '</dd>
			</dl>
		';
	} else echo '<p>Invalid username or password</p>';
	
	$stmt->close();
	
} else echo '<p>Missing username or password</p>';
		
		
	
?>

<!DOCTYPE html>
<html>
<head>
<title><?php $site_name?> Member Login Page</title>
  <meta charset="utf-8">
</head>
<body>
<div class = "container">
<form method="post" action="">
	<h3><?= $site_name ?> Member Login Form</h3>
	<fieldset>
		<label for="login_name">Username/Email:</label>
		<input type="text" name="login_username_or_email" id="login_name">
		<br>
		<label for="login_pass">Password:</label>
		<input type="password" name="login_password" id="login_pass">
	</fieldset>
	<div class="submitsAndHiddens">
		<button type="submit">Login</button><br>
		<a href="login_password_reset.php">Forgot your Password?</a><br>
		<a href="register.php">Register New Account</a>
	</div>
</form>
</div>
</body>
</html>
 

Re: converting pdo oop to mysqli procedural

Posted: Sat Oct 07, 2017 3:49 am
by Celauran
UniqueIdeaMan wrote:I really need the following code converted to mysqli procedural from pdo oop.
There's no PDO that I can see in there.
UniqueIdeaMan wrote:Other newbies would learn from your conversion.
I don't know that I'd call actively making code worse a learning experience.

Re: converting pdo oop to mysqli procedural

Posted: Wed Oct 11, 2017 12:57 pm
by VladSun
Celauran wrote:
UniqueIdeaMan wrote:Other newbies would learn from your conversion.
I don't know that I'd call actively making code worse a learning experience.
+100

Re: converting pdo oop to mysqli procedural

Posted: Wed Oct 11, 2017 3:45 pm
by Christopher
VladSun wrote:
Celauran wrote:
UniqueIdeaMan wrote:Other newbies would learn from your conversion.
I don't know that I'd call actively making code worse a learning experience.
+100
:drunk:

It does look like the job of converting from mysqli procedural from mysqli OOP has been done. Excellent work!