MYSQL database not connecting to php, please help

<?php
if (isset($_POST['submit'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $connection = mysqli_connect('localhost', 'root', '', 'ayaneshapp');
    if ($connection) {
        echo "Logged in!";
    } else {
        die("Sorry! Something went wrong.");
    }
    $query = "INSERTED INTO ayaneshtable(username, password) ";
    $query.= "VALUES ('$username', '$password')";
    $result = mysqli_query($connection, $query);
    if (!$result) {
        die('404 ERROR');
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>

<div class="container">
	
	<div class="col-sm-6">
		
		<form action="login_create.php" method="post">
			<div class="form-group">
				<label for="username">Username</label>
				<input type="text" name="username" class="form-control">
			</div>
			<div class="form-group">
				<label for="password">Password</label>
				<input type="password" name="password" class="form-control">
			</div>
			<input class="btn btn-primary" type="submit" name="submit" value="Submit">
		</form>
	</div>
</div>	
   
</body>
</html>

When I click on submit, I see both Logged in and 404 error, I want logged in to happen when I put values in inputs and I want those values to be added on myphpadmin in my sql table.

The php code I provided in the same file as the html above.
Whatever code login_create.php contains, I just put in above (question) and It is located in another folder which has nothing but this.

Alright, I’ll check that but 404 error is not the problem. If you see my code, if the user doesn’t put anything in the input fields, then 404 error will appear.

$query = "INSERTED INTO ayaneshtable(username, password) ";
    $query.= "VALUES ('$username', '$password')";
    $result = mysqli_query($connection, $query);
    if (!$result) {
        die('404 ERROR');
    }

If you can connect to mysql but are getting an error, just a tip to output your SQL string to the web browser, and then copy and paste it into phpMyAdmin. This will help you to figure out whether or not there is something wrong with the SQL statement itself. PHP will just error without any description of what is wrong with the SQL statement whereas within phpMyAdmin you can find out what is wrong with the SQL statement (if it works in phpMyAdmin, then it’s something wrong in your PHP code). Once you know your SQL statement is correct, you can copy it back over into your PHP.

INSERTED is not a SQL keyword. Try INSERT instead (and again, if it’s not working, copy/paste it into phpMyAdmin until you have the syntax correct).

Also, the single quotes around the variable names within the VALUES() section are not required - PHP should resolve it correctly but it’s more keystrokes than are necessary.

1 Like

Hi !

In this line of code:

$connection = mysqli_connect('localhost', 'root', '', 'ayaneshapp'); 

You are not sending the password, or is password equal to ‘’?

(Unfortunately), the default root password in mysql is blank.

I think that all you need to do is change this into

$query = "INSERT INTO ayaneshtable(username, password) ";

If you’re getting the error after a user clicks ‘submit’ without filling in the input fields, then why not add some error handlers to your code? Something like this should do the trick:

if (isset($_POST['submit'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $connection = mysqli_connect('localhost', 'root', '', 'ayaneshapp');
    if ($connection) {
        echo "Logged in!";
    } else {
        die("Sorry! Something went wrong.");
    }
    //Check for empty fields
	if  (empty($username) || empty($password)) {
		header("Location: ?inputs=empty");
		exit();
	} else { //if everything was filled in, activate this code:
    $query = "INSERT INTO ayaneshtable(username, password) ";
    $query.= "VALUES ('$username', '$password')";
    $result = mysqli_query($connection, $query);
    }
}

Sorry I’m replying so late, as I was offline. For the suggestions of everyone of you, I have solved the problem. I’m new to PHP and it is frustrating to do these mistakes. I just did a minor mistake this time.

Hello @camperextraordinaire , there was only a minor problem. It should have been ‘INSERT’ not ‘INSERTED’.

I feel shamed that I made a stupid mistake and posted.