Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,
I have a php file connecting to phpmyadmin on xampp. I set the localhost password to ‘password’ I run the script with ajax and it does work because I tested it with a filecreate. I get NO errors and all I want to do is check and see if the email exists and if not then create a row in the database.
PHP
<?php
// Connect to Phpmyadmin database localhost.
$hostname = "localhost";//"";127.0.0.1
$username = "root";
$password = "password";//"password";
$dbname = "test";

$conn = new mysqli($hostname, $username, $password, $dbname);
if($conn -> connect_error) {
	die("Connection failed: " . $conn -> connect_error);
}
// USED to CHANGE the db. - (mysqli_select_db($con, "test") or die());

/* Check to make all fields entered -make sure submitted- #2*/
if(isset($_POST["register"])) {
	if(!$_POST["email"] | !$_POST["password"] | !$_POST["password2"]) {
		die("You did not fill in all the fields");
	}

/* do escape strings for SQL injection */
$emailsafe = mysqli_real_escape_string($conn, $_POST["email"]);
$passsafe = mysqli_real_escape_string($conn, $_POST["password"]);
$pass2safe = mysqli_real_escape_string($conn, $_POST["password2"]);

/* Check and see if email EXISTS in db. */

/* Insert email in db if does not exist */
$sql = "SELECT email FROM users WHERE email = '$emailsafe'";//limit 1
$result = $conn->query($sql) or die("invalid email check " . mysqli_error($conn));
$numrows = mysqli_num_rows($result);
if($numrows != 0) {
	die("Sorry the email " . $_POST["email"] . "is already in use");
}
// check if passwords both match
if($_POST["password"] != $_POST["password2"]) {
	die("Passwords did NOT match");
}
// if passwords match encrypt
$hashedpass = password_hash($passsafe, PASSWORD_DEFAULT);
// With everything escaped and hashed INSERT into db.
$sqlINSERT = "INSERT INTO users(email, password) " .
	"VALUES('" . $emailsafe ."','". $hashedpass ."')";
if($query = $conn->query($sqlINSERT)=== TRUE) {
	echo '<script type="text/javascript">alert("Successfully INSERTed");</script>';
} else {
	die("Unsuccessful");
}
$conn->close();
?>


What I have tried:

I ran my php with ajax and it creates the file successfully so i know it runs. I wanted to setup PHP debugging with atom or visual studio but i have not been able to successfully do that... I tried with xdebug so i just use the network tab inchrome to find if errors exist.
I wish i was able to run debug line by line I use xampp for some reason my network tab makes me use mysqli I thought the newest php just used mysql I downloaded the php7 and put it on my hard drive but there was no php.ini to setup xdebug. I found a php.ini in xampp but that did not correctly do it. I also downloaded the NEWEST xampp thinking that was the problem.. packages on atom followed many different guides online but nothing was successful.
Posted
Comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900