Mike
Mike

Reputation: 6839

get return value from php for ajax call

I am trying to get the auto increment id from a database insert in php back to my javascript ajax call:

My ajax call looks like this:

//get values
        var note = $('#note1').val();

        alert(userID);
        alert(beerID);


        var ajaxSettings = {
        type: "POST",
        url: "atn.php",
        data: {u:userID , b:beerID ,n:note},
        success: function(data){
            alert(data);


     } ,
        error: function(xhr, status, error) { alert("error: " + error); }

    };          

        $.ajax(ajaxSettings);

        return false;

and my php script looks like this:

<?php
error_log("starting code"); 
require_once('myConnectDB.inc.php');

        $u = $_POST['u'];
        $b = $_POST['b'];
        $n = $_POST['n'];

        //do some checks etc

        $db = new myConnectDB();


        $u = $db->real_escape_string($u);
        $n = $db->real_escape_string($n);
        $b = $db->real_escape_string($b);

        $query3 = "INSERT INTO tn (userID,beerID,note) VALUES ($u, '$b', '$n')";


        $result = $db->query($query3);

        $dbID = mysql_insert_id();

        echo $dbID;



?>

the $dbID that I am trying to send back is not getting alerted after the php script runs. I am getting this error in my alert:

<br />
<b>Warning</b>:  mysql_insert_id() [<a href='function.mysql-insert-id'>function.mysql-insert-id</a>]: A link to the server could not be established in <b>/home4/m133414/public_html/atn.php</b> on line <b>23</b><br /> 

Upvotes: 0

Views: 3199

Answers (1)

Replace your following line:

$dbID = mysql_insert_id();

for this one:

$dbID = $db->insert_id;

As you were incorrectly using the procedural version of mysql_insert_id() while you were otherwise using the object oriented version, so to be consistent we use the OOP version here too.

Upvotes: 2

Related Questions