简体   繁体   中英

how to get the value of next autoincrements in php for mysql

ok so I have to run 2 updates that rely on each others IDs to sync properly.

conversation ID has to be in message table, where as MessageID has to be in the conversation table. and both are auto increment values.

mysql_query("UPDATE ow_base_user Set activityStamp = '$stamp' where id = '$profile_id' ");
    mysql_query("INSERT INTO ow_base_user_online (id, userId, activityStamp, context) VALUES ('', '$profile_id', '$stamp', '1')");

    $conid = mysql_query("SELECT ID AS id FROM ow_mailbox_conversation WHERE ID = IDENT_CURRENT") + 1;
    $msgid = mysql_query("SELECT ID AS id FROM ow_mailbox_message WHERE ID = IDENT_CURRENT") + 1;

    mysql_query("INSERT INTO ow_mailbox_conversation (id, initiatorId, interlocutorId, subject, read, deleted, viewed, notificationSent, createStamp, initiatorDeletedTimestamp, interlocutorDeletedTimestamp, lastMessageId, lastMessageTimestamp)
     VALUES ('$conid', '$profile_id', '637', 'mailbox_chat_conversation', '1', '0', '1', '0', '$stamp', '0', '0', '$msgid', '$stamp')");
    mysql_query("INSERT INTO ow_mailbox_message (id, conversationId, timeStamp, senderId, recipientId, text, recipientRead, isSystem, wasAuthorized)
     VALUES ('$msgid', '$conid', '$stamp', '$profile_id', '637', 'hi there', '0', '0', '1')");

I thought possibly I could use IDENT_CURRENT + 1 to get the ID but when I echo it nothing comes up. sorry a bit new at this still can

--- edit ---

so tried using insert_id - problem then is the first number did not come back correctly. gave me a number almost 2x as large as it should be.

here is the code

mysql_query("INSERT INTO ow_mailbox_conversation (id, initiatorId, interlocutorId, subject, read, deleted, viewed, notificationSent, createStamp, initiatorDeletedTimestamp, interlocutorDeletedTimestamp, lastMessageId, lastMessageTimestamp)
VALUES ('', '$profile_id', '637', 'mailbox_chat_conversation', '1', '0', '1', '0', '$stamp', '0', '0', '', '$stamp')");
$conid = mysql_insert_id();
mysql_query("INSERT INTO ow_mailbox_message (id, conversationId, timeStamp, senderId, recipientId, text, recipientRead, isSystem, wasAuthorized)
VALUES ('', '$conid', '$stamp', '$profile_id', '637', 'hi there', '0', '0', '1')");
$msgid = mysql_insert_id();
mysql_query("UPDATE ow_mailbox_conversation Set lastMessageId = '$msgid' where id = '$conid' ");

--- edit ---

here is my full code everything seems to be working fine except Convarsation string is not inserting, then the $conid comes back really high in the debug.

$link = mysql_connect($OW_DB_HOST, $OW_DB_USER, $OW_DB_PASS);
if (!$link) {
    die('Connection fail: ' . mysql_error());
}
mysql_select_db($OW_DB_NAME, $link);
// End of connection database

$stamp = time(); 

//1
mysql_query("UPDATE ow_base_user Set activityStamp = '$stamp' where id = '$profile_id' ");
mysql_query("INSERT INTO ow_base_user_online (id, userId, activityStamp, context) VALUES ('', '$profile_id', '$stamp', '1')");


$receiver_id = '637';

mysql_query("INSERT INTO ow_mailbox_conversation (id, initiatorId, interlocutorId, subject, read, deleted, viewed, notificationSent, createStamp, initiatorDeletedTimestamp, interlocutorDeletedTimestamp, lastMessageId, lastMessageTimestamp)
VALUES ('', '$profile_id', '$receiver_id', 'mailbox_chat_conversation', '1', '0', '1', '0', '$stamp', '0', '0', '', '$stamp')");
$conid = mysql_insert_id();

mysql_query("INSERT INTO ow_mailbox_message (id, conversationId, timeStamp, senderId, recipientId, text, recipientRead, isSystem, wasAuthorized)
VALUES ('', '$conid', '$stamp', '$profile_id', '$receiver_id', 'hi there', '0', '0', '1')");
$msgid = mysql_insert_id();

mysql_query("INSERT INTO ow_mailbox_last_message (id, conversationId, initiatorMessageId, interlocutorMessageId)
VALUES ('', '$conid', '$msgid', '0')");
$lastmsgid = mysql_insert_id();

mysql_query("UPDATE ow_mailbox_conversation Set lastMessageId = '$lastmsgid' where id = '$conid' ");

//End of script if devmode = false

// Output all used variables on devmode = true
if (DEVMODE){
    echo 'Connection ok';echo '<br>';
    echo '1 = ',$conid;echo '<br>';
    echo '2 = ',$msgid;echo '<br>';
    echo '3 = ',$lastmsgid;echo '<br>';

}

// End of testbench

mysql_close($link);
?>

you can use PDO::lastInsertId for getting id of last inserted row if you used PDO for connection. please refer: http://php.net/manual/en/pdo.lastinsertid.php

alternatives, http://php.net/manual/en/mysqli.insert-id.php

You cannot be sure of the next insert ID because your system will (I assume) be handling multiple feeds at a time as your site is not single-user

The best thing to do is to use three queries:

  1. Insert into table 1 and retrieve the ID (id1)
  2. Insert into table 2 (storing id1) and retrieve the new ID (id2)
  3. Update table 1 (record ID id1) to store id2 in the relevant field

For this, I recommend using the InnoDB table and transactions. In the event of a failure, you can roll back to the state before anything started and minimise IDs being present which are unattached.

An alternative is to create a third table to store the relationship between the tables. Essentially, it stores id1 and id2 and nothing else

If possible, re-work your tables so that this mutual referencing is not required. It will make things easier in the long run

Finally, stop using the mysql_ functions. They are deprecated and removed completely in PHP 7. Use the PDO or mysqli_ functions. You'll thank me

the problem was errors with the structure

here is the fixed code.

    mysql_query("INSERT INTO ow_mailbox_conversation (id, initiatorId, interlocutorId, subject, `read`, deleted, viewed, notificationSent, createStamp, initiatorDeletedTimestamp, interlocutorDeletedTimestamp, lastMessageId, lastMessageTimestamp)
VALUES ('', $profile_id, $receiver_id, 'mailbox_chat_conversation', 1, 0, 1, 0, $stamp, 0, 0, '', $stamp)");
$conid = mysql_insert_id();

notice the removed ' ' around the numbers and variables.

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