MySQL: Multiple Queries with Same Connection Object

Thursday, July 10, 2014

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!



<?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";
}

?>






http://ift.tt/1mDy01d