Jump to content

Php runs with ajax in network tab NO errors...


exceedinglife

Recommended Posts

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
// 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();
?>

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.

Link to comment
Share on other sites

Try a tool like Postman to debug your script without Ajax client code.  

Don't use die() unless you want the script to echo immediately.  For example, you have $conn->close() at the end, which won't run normally when clearly you want it to.

Use bind params for your queries rather than building up your queries into interpolated strings.  Doing so is safer, and allows you to remove all the escaping.  Your SQL code will also be cleaner and easier to read, maintain and debug.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.