MYSQL database not connecting to php, please help

MYSQL database not connecting to php, please help
0

#1
<?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>

#2

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

You are going to have to be a little more detailed in your request. What kind of error messages are you seeing?


#3

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.


#4

Since I can not see all the files and their code, I have to ask is the php code you provided in the same file as the html above? Also, what code is in the file named login_create.php and where is it located locally in relation to the file containing the html you show above?


#5

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.


#6

Here is the mysql screenshot


#7

Your form’s action attribute refers to “login_create.php”, so if that file is in another folder other than the html file, then that is why you are getting the 404 error.


#8

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');
    }


#9

Still a little confused about something. If the php code you have a the top of this post is in the same file as the form html, it will never execute, because upon submitting the form, the code in the file named “login_create.php” will be executed. If the file “login_create.php” is not located in the same directory as file containing this html form, you are going to get a 404 error message.


#10

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.


#11

Hi !

In this line of code:

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

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


#12

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


#13

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

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


#14

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);
    }
}

#15

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.


#16

Would you mind explaining what the problem actually was and what you did to resolve it?


#17

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


#18

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


#19

It could have been any number of things. We all make mistakes that seem silly later. It is part of the learning process.