简体   繁体   English

无法使用PHP在Ajax调用中连接到MY SQL DB

[英]Unable to connect to MY SQL DB in Ajax call using PHP

I am performing a DB operation after making an ajax call using php, 1) I see in fiddler that the request and response to the PHP (ajax call) are fine but and 2) I see the error "Access denied for user 'root'@'localhost' (using password: NO" in Fiddler. 我使用php进行ajax调用后正在执行数据库操作,1)在小提琴手中看到对PHP的请求和响应(ajax调用)都很好,但是2)我看到错误“用户'root'的访问被拒绝” @'localhost'(在Fiddler中使用密码:NO”。

I am not trying to connect to root but to a different user. 我不是要连接到root用户,而是要连接到另一个用户。 Here is the view.php file where ajax call is initiated: 这是启动ajax调用的view.php文件:

$.ajax({ 
 url: 'delete_entry.php',
     data: "id="+del_id,
     type: 'post',
     success: function(output) {
             alert("Success");
                  //document.getElementById("test1").innerHTML = output;
              }
});

I recieve the alert message "Success" though. 我收到警报消息“成功”。

Code in delete_entry.php: delete_entry.php中的代码:

<?php

    $servername = "localhost";
    $username = "testdb";
    $password = "testdb";
    $dbname = "testts";


    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        echo "Connection Failed";
        die("Connection failed: " . $conn->connect_error);
    } 

$id=$_POST['id'];
echo $id;  // I get a proper Id here
$delete = "DELETE FROM ExpenseTable WHERE Id='"+$id+"'";
$result = mysql_query($delete) or die(mysql_error());
?>

Please help as I do not understand why the mysql db is trying to connect to root eventhough I specify the db detais as "testdb". 请帮忙,尽管我将db detais指定为“ testdb”,但我不理解为什么mysql db试图连接到root。 I am able to connect to the same db with these credentials in my view.php 我可以在我的view.php中使用这些凭据连接到同一数据库

You are not using your created DB connection when you pass the sql query to the DB. 当您将sql查询传递给数据库时,您没有使用创建的数据库连接。 Use the mysqli_query method as below.. 使用如下的mysqli_query方法。

mysqli_query($conn,$delete);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM