用户''@ localhost'拒绝访问。试图插入数据PHP Mysql [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我正在尝试将数据插入到我的数据库中,但我得到的错误是:用户''@ localhost'拒绝访问(使用密码:否)

奇怪的是,我可以从我的数据库中选择数据,它还会将这些数据显示在我的表单字段中。

http://sayhey.nl/mealapp/add-ingredients.php

但是当我尝试通过此表单插入新数据时,它会给出“拒绝访问”错误。看起来没有设置用户名或密码,但实际上我确实这样做了:

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

这是代码的其余部分:

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

有人知道如何解决这个问题吗?

php mysql database permissions user-permissions
1个回答
2
投票

你在混合PDOprocedural way ......

mysql_query(替换你的$conn->query(

© www.soinside.com 2019 - 2024. All rights reserved.