0

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 /> 
4
  • As a test, have you tried posting to a bare-bones script like <?php echo "hi"; ?>? Commented Nov 2, 2013 at 14:48
  • I will try echo instead of print right now Commented Nov 2, 2013 at 14:49
  • Sorry, I use echo out of habit. Print and echo are basically the same. I just wanted you to try something simple so you can know whether the problem is PHP or in javascript. Commented Nov 2, 2013 at 14:51
  • Well I think I was dumb and forgot to update the right file on my server. Now I did and I get an error. It seems to not like line $dbID = mysql_insert_id(); I think I am getting the value wrong... Commented Nov 2, 2013 at 14:52

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.