简体   繁体   English

PHP + SQL-将主键的值插入同一行中的另一个字段

[英]PHP + SQL - insert value of primary key to another field in the same row

I need to insert a row to table where second element of the row (chatID) copies value of auto incremented primary key (messageID). 我需要在表中插入一行,其中该行的第二个元素(chatID)复制自动递增的主键(messageID)的值。

safe_query("INSERT INTO ".PREFIX."messenger ('messageID', 'chatID') VALUES('','')");

Any ideas would be very helpful. 任何想法都将非常有帮助。 Thanks. 谢谢。

In php when you inserted the record get the lastInsertId(); 在php中,当您插入记录时,获取lastInsertId(); and update the chatID 并更新chatID

$inserted_id = $pdo->lastInsertId();

$sth2 = $pdo->prepare("UPDATE ".PREFIX."messenger 
SET `chatID`= $inserted_id WHERE `messageID`=$inserted_id");
$sth2->execute();

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

相关问题 插入到使用相同的行(相同的主键)mysql php pdo - insert into using same row(same primary key) mysql php pdo PHP / SQL-在多个字段上插入相同的值 - php/sql - insert same value on multiple field PHP在主键MySql中没有插入任何值 - Php insert no value in primary key MySql 使用 php mysql 在另一个表中插入选定下拉列表值的主键作为外键 - Insert Primary key of Selected Drop down list value as a foreign key in another table using php mysql 根据主键值而不是行号检索PHP中的特定MySQL字段数据 - Retrieving Specific MySQL Field Data in PHP Based on Primary Key Value Instead of Row Number 如何将行的主键插入其另一列? - How to insert a row's primary key in to another one of its columns? Sql 在将主键与 where 子句中的另一个值组合时返回空行 - Sql returns empty row on combinning primary key with another value in where clause PHP 脚本:如何返回正在写入的 SQL 记录的 ID 字段(主键自增 1)的值 - PHP Scripting: How to return the value of the ID field (primary key auto-increment 1) of the SQL record being written to 根据PHP数组同一行中的另一个键值检索键值 - Retrieve key value based on another key value within the same row of a PHP array PHP MySQL 在同一个表中复制一行...使用主键和唯一键 - PHP MySQL Copy a row within the same table... with a Primary and Unique key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM