I hope using the 'mysql' db in mysql does not affect anything (I honestly can't remember if that was mistake or not) as I will have to completely re-do the mysql portion of this site if so (I don't know if it is reserved for something else). I have two things that I am trying to do here, 1) Verify login information via usernames table, and 2) Create a listing if login information is valid. I originally had this in two PHP files, but am trying to simplify this as much as possible; some examples I have seen for logging into a site are too complicated, all I need is a mechanism to verify login details, and create a listing...this is not intended to be a networking site or anything. Also, in the IDE I'm using it mentions not to use $query twice but changing it doesn't seem to make a difference. I am not too familiar with how PDO::fetch() works (or PHP in general) but this is how I have implemented it:
<?php
header('Content-Type: application/json');
error_reporting(E_ALL);
ini_set('display_errors', 1);
$uname = filter_input(INPUT_POST, 'uname');
$upassword = filter_input(INPUT_POST, 'upassword');
$name = filter_input(INPUT_POST, 'name');
$description = filter_input(INPUT_POST, 'description');
$createlisting = '';
$hello = array();
if(!empty($uname && $upassword && $name && $description)) {
$conn = new PDO('mysql:host=127.0.0.1;dbname=mysql', 'root', "password");
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = 'SELECT `uname`,`upassword` FROM `usernames` WHERE `uname` = :uname';
$stmt = $conn->prepare($query);
$stmt->bindParam(":uname",$uname, PDO::PARAM_STR, strlen($uname));
$stmt->execute();
$hello = $stmt->fetch(PDO::FETCH_ASSOC);
$upasswordhash = password_hash($upassword, PASSWORD_DEFAULT);
$loginarray = array($uname, $upasswordhash);
$loginarray1 = array($hello['uname'], $hello['upassword']);
if ($loginarray === $loginarray1){
$query = "INSERT INTO user_meta (name, description) VALUES (:name, :description)";
$stmt = $conn->prepare($query);
$query->bindParam(":name",$name, PDO::PARAM_STR, strlen($name));
$query->bindParam(":description",$description, PDO::PARAM_STR, strlen($description));
$query->execute();
echo json_encode("Your listing has been created. Please use the search function to see it.");
$conn = null;
$query = null;
//closes the MySQL connection.
} else {
echo json_encode("Listing failed.");
}
}