Reputation: 1143
I have a registration script for one of my websites where I first check if the email entered by the user matches one that is in the database, and if it does, then return that there was a match found and do not insert their new account into the database.
- I would like to know if there is a way to accomplish what I am doing with only one mysql connection object rather than initializing two of them?
- Also, I feel like my code is pretty messy and would love some extra opinions on how to write it better. Thanks!
Note: The validation boolean will be used in the future, so ignore the fact that it does nothing.
<?php
include '../portfolio/libraries/settings.php';
$first = $_POST['first'];
$middle = $_POST['middle'];
$last = $_POST['last'];
$email = $_POST['email'];
$emailFound = false;
$validation = true;
if ($_POST['type'] == "Student") {
$type = "0";
} else {
$type = "1";
}
$connect = mysqli_connect($HOST, $DB_USER, $DB_PASS, $DATABASE);
$connect1 = mysqli_connect($HOST, $DB_USER, $DB_PASS, $DATABASE);
$result = $connect1->prepare("SELECT email FROM AUTH_User WHERE email = ?");
$result->bind_param("s", $email);
$result->execute();
while($result->fetch()) {
$emailFound = true;
}
if (!$emailFound) {
if ($middle != "" || $middle != null) {
$query = "INSERT INTO AUTH_User (email, FirstName, MiddleName, LastName, is_faculty) VALUES (?, ?, ?, ?, ?)";
$stmt = $connect->prepare($query);
$stmt->bind_param("ssssi", $email, $first, $middle, $last, $type);
if ($stmt->execute() && $validation) {
echo "success";
}
} else {
$query = "INSERT INTO AUTH_User (email, FirstName, LastName, is_faculty) VALUES (?, ?, ?, ?)";
$stmt = $connect->prepare($query);
$stmt->bind_param("sssi", $email, $first, $last, $type);
if ($stmt->execute() && $validation) {
echo "success";
}
}
} else {
echo "found";
}
?>
Upvotes: 0
Views: 6248
Reputation: 98
You can use only one $connect :)
Maybe i can recommend you to use PDO to make query : http://php.net/manual/en/book.pdo.php
And I tried to clear your code with my own experience, so I haven't the better way to code ;)
include '../portfolio/libraries/settings.php';
$first = $_POST['first'];
$middle = $_POST['middle'];
$last = $_POST['last'];
$email = $_POST['email'];
$emailFound = false;
$validation = true;
$type = ($_POST['type'] == "Student")? 0 : 1 ;
$connect = mysqli_connect($HOST, $DB_USER, $DB_PASS, $DATABASE);
$result = $connect->prepare("SELECT email FROM AUTH_User WHERE email = ?");
$result->bind_param("s", $email);
$result->execute();
if(!$result->fetch()) {
if (isset($middle)) {
$query = "INSERT INTO AUTH_User (email, FirstName, MiddleName, LastName, is_faculty) VALUES (?, ?, ?, ?, ?)";
$stmt = $connect->prepare($query);
$stmt->bind_param("ssssi", $email, $first, $middle, $last, $type);
if ($stmt->execute() && $validation)
echo "success";
} else {
$query = "INSERT INTO AUTH_User (email, FirstName, LastName, is_faculty) VALUES (?, ?, ?, ?)";
$stmt = $connect->prepare($query);
$stmt->bind_param("sssi", $email, $first, $last, $type);
if ($stmt->execute() && $validation)
echo "success";
}
} else {
echo "found";
}
Upvotes: 2