I'm trying to insert data into my database, but the error that I get is: Access denied for user ''@'localhost' (using password: NO)
The strange thing is that I can select data from my database and it also displays this data into my form fields.
http://sayhey.nl/mealapp/add-ingredients.php
But when I try to insert new data via this form, it gives the 'access denied' error. It looks like there is no username or password set, but actually I did do this:
//Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$database = "databasename";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
And this is the rest of the code:
<?php
//Connect
include 'connect.php';
$ingredient_sql = "SELECT id, name FROM ingredient_cat";
$ingredient_cat_result = $conn->query($ingredient_sql);
?>
<div id="wrapper">
<form method = "post" action = "<?php $_PHP_SELF ?>">
<input name="ingredient_name" type="text" id="ingredient-name" />
<select name="ingredient_cat" id="ingredient-cat">
<?php
while($row = $ingredient_cat_result->fetch_assoc()) {
echo '<option value="'.$row["id"].'">'.$row["name"].'</option>';
}
?>
</select>
<select name="ingredient_unit" id="ingredient-unit">
<option value="stuks">stuks</option>
<option value="gram">gram</option>
<option value="liter">liter</option>
</select>
<input type="submit" name="add" value="Add ingredient" />
</form>
</div>
<?php
if(isset($_POST['add'])) {
$ingredient_name = $_POST['ingredient_name'];
$ingredient_cat = $_POST['ingredient_cat'];
$ingredient_unit = $_POST['ingredient_unit'];
mysql_query("INSERT INTO ingredient(name, unit, cat) VALUES('".$ingredient_name."', '".$ingredient_unit."', ".$ingredient_cat.")")or die(mysql_error());
}
?>
Does someone know how to fix this problem?