简体   繁体   中英

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.

I am not trying to connect to root but to a different user. Here is the view.php file where ajax call is initiated:

$.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:

<?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". I am able to connect to the same db with these credentials in my view.php

You are not using your created DB connection when you pass the sql query to the DB. Use the mysqli_query method as below..

mysqli_query($conn,$delete);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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