GreenGarden
GreenGarden

Reputation: 33

Unidentified issue

I have made a html file with a registration form, and want the data saved in a mysql database.

I have a login.php file to connect to the mysql database that looks like this:

<?php 
$db_hostname = 'localhost'; 
$db_database = 'tester'; 
$db_username = 'p2';
$db_password = 'adgangskode';
?>

The other php file is called proces_input.php and look like this:

<?php
require_once 'login.php';
$connection = mysqli_connect($db_hostname, $db_username, $db_password);

if(!$connection) {
    die("Cant connect to MySQL: " . mysqli_connect_error());
}

$selected_db = mysqli_select_db($connection, $db_database);

if(!$selected_db){
    die("Couldnt decide database: " . mysqli_connect_error());
}    
$fornavn = $_POST['fornavn'];
$efternavn = $_POST['efternavn'];
$adresse = $_POST['adresse'];
$postnummer = $_POST['postnummer'];
$city = $_POST['city'];
$telefonnummer = $_POST['telefonnummer'];
$fdag = $_POST['fdag'];
$password = $_POST['password'];
$email = $_POST['email'];

$insert_query = "INSERT INTO users VALUES ('$fornavn', '$efternavn',
'$adresse', '$postnummer', '$city', '$telefonnummer', '$fdag', '$password',
'$email')";

$result = mysqli_query($connection, $insert_query);
if(!$result){ 
die ("Acces to database denied: " . mysqli_connect_error());
}

mysqli_close($connection);
?>

When I fill out the form and submit the data i get the error:

"Acces to database denied".

To solve the problem I have tried to run the following code in mysql:

GRANT ALL PRIVILEGES ON tester.* TO p2@localhost IDENTIFIED BY 'adgangskode';

This solution didn't seem to work out, so I am looking for someone who hopefully can give me an answer to the problem.

Upvotes: 1

Views: 71

Answers (2)

Webeng
Webeng

Reputation: 7113

Replace this code:

insert_query = "INSERT INTO users VALUE ('$fornavn', '$efternavn',
'$adresse', '$postnummer', '$city', '$telefonnummer', '$fdag', '$password',
'$email')";

with this code:

$insert_query = "INSERT INTO users VALUES ('$fornavn', '$efternavn',
'$adresse', '$postnummer', '$city', '$telefonnummer', '$fdag', '$password',
'$email')";

You seem to have miss-spelled VALUE when it should be VALUES.

Also, you forgot to include $ before insert_query

Let me know if it works now!

Upvotes: 3

Dipanwita Kundu
Dipanwita Kundu

Reputation: 1667

To give privileges:

GRANT ALL ON database TO username@'localhost' IDENTIFIED BY 'password';

Please find the link for more details: http://dev.mysql.com/doc/refman/5.7/en/grant.html

your insert_query must be $insert_query

Upvotes: 1

Related Questions