This question already has an answer here:
这个问题在这里已有答案:
- Can I mix MySQL APIs in PHP? 4 answers
我可以在PHP中混合MySQL API吗? 4个答案
I'm trying to insert data into my database, but the error that I get is: Access denied for user ''@'localhost' (using password: NO)
我正在尝试将数据插入到我的数据库中,但我得到的错误是:用户''@ localhost'拒绝访问(使用密码:否)
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?
有人知道如何解决这个问题吗?
1 个解决方案
#1
2
You are mixing PDO
with procedural way
...
你正在以程序的方式混合PDO ......
Replace your mysql_query(
with $conn->query(
替换你的mysql_query(使用$ conn-> query(
#1
2
You are mixing PDO
with procedural way
...
你正在以程序的方式混合PDO ......
Replace your mysql_query(
with $conn->query(
替换你的mysql_query(使用$ conn-> query(