繁体   English   中英

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

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

我使用php进行ajax调用后正在执行数据库操作,1)在小提琴手中看到对PHP的请求和响应(ajax调用)都很好,但是2)我看到错误“用户'root'的访问被拒绝” @'localhost'(在Fiddler中使用密码:NO”。

我不是要连接到root用户,而是要连接到另一个用户。 这是启动ajax调用的view.php文件:

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

我收到警报消息“成功”。

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

请帮忙,尽管我将db detais指定为“ testdb”,但我不理解为什么mysql db试图连接到root。 我可以在我的view.php中使用这些凭据连接到同一数据库

当您将sql查询传递给数据库时,您没有使用创建的数据库连接。 使用如下的mysqli_query方法。

mysqli_query($conn,$delete);

暂无
暂无

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

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