简体   繁体   English

如何知道PDO是否在带有复合键的表上插入记录?

[英]How to know if PDO inserted a record on a table with a composite key?

Yes, there are similar questions (ie PDO mysql: How to know if insert was successful and $stmt->execute() : How to know if db insert was successful? ), however, the supplied answers don't seem to work for me. 是的,也有类似的问题(即PDO mysql:如何知道插入是否成功,以及$ stmt-> execute():如何知道数据库插入是否成功? ),但是,提供的答案似乎不适用于我。

Given the below query, $stmt->execute(array($doc_id,$id,$sites_id)) returns true , however, a record is not inserted. 给定以下查询, $stmt->execute(array($doc_id,$id,$sites_id))返回true ,但是未插入记录。

How do I determine whether PDO inserted a record? 如何确定PDO是否插入了记录?

EDIT. 编辑。 Please don't just say to use PDO::lastInsertId , but if doing so is necessary, give a reason why it is necessary. 请不要只是说要使用PDO::lastInsertId ,而是如果有必要这样做,请说明为什么有必要这样做。

INSERT INTO docx_priv_projects (documents_id,projects_id)
SELECT 2972614382,t.id
FROM projects AS t INNER JOIN entities AS e ON e.id=t.id
WHERE t.id=1379519490 AND e.record_status='active' AND e.sites_id=2416619273;

PDO::lastInsertId appears to only work on auto incremented PKs, however, this requirement does not seem to be documented very well at http://php.net/manual/en/pdo.lastinsertid.php . PDO::lastInsertId似乎仅适用于自动递增的PK,但是,在http://php.net/manual/zh/pdo.lastinsertid.php上似乎没有很好地记录此要求。 Furthermore, it does not appear to work with composite keys. 此外,它似乎不适用于复合键。

pdo::rowCount() is required. pdo::rowCount()是必需的。 See http://php.net/manual/en/pdostatement.rowcount.php . 参见http://php.net/manual/en/pdostatement.rowcount.php Thanks given to AbraCadaver and Ryan Vincent. 感谢AbraCadaver和Ryan Vincent。

$sql='INSERT INTO docx_priv_projects (documents_id,projects_id)
SELECT :doc_id,t.id
FROM projects AS t INNER JOIN entities AS e ON e.id=t.id
WHERE t.id=:id AND e.record_status='active' AND e.sites_id=:sites_id';
$stmt = $conn->prepare($sql);
$stmt->execute(array('id'=>1379519490 ,'sites_id'=>2416619273,'doc_id'=>2972614382));
$x=$stmt->rowCount();  //Will be `1` if added

You can test if there was a last inserted id in two ways: 您可以通过两种方式测试是否最后插入了一个ID:

Using the PDO API: 使用PDO API:

$stmt = $db->prepare("...");
$stmt->execute();
$id = $db->lastInsertId();

Using SQL instead of the PDO API: 使用SQL代替PDO API:

$stmt = $db->query("SELECT LAST_INSERT_ID()");
$lastId = $stmt->fetch(PDO::FETCH_NUM);
$lastId = $lastId[0];

Then test for example is, lastid or id is int > 1 or do your own validation. 然后测试例如,lastid或id为int> 1或进行您自己的验证。

http://php.net/manual/en/pdo.lastinsertid.php

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

相关问题 如何使用PDO删除插入MySQL表的最后一条记录? - How do I delete the last record inserted into a MySQL table using PDO? PDO如何知道MySQL中最后插入的id? - How does PDO know last inserted id in MySQL? 与主复合键的Zend表关系 - 从表中删除记录 - Zend Table Relationship with Primary Composite Key - delete record from table 如何使用Laravel检查MySQL表中是否存在带有复合主键的记录? - How to check if a record with a composite primary key exists in a MySQL table using Laravel? 使用PDO和PHP从MSSQL表中获取最后插入的记录的ID - Getting the id of the last inserted record from an MSSQL table using PDO and PHP 为什么没有将数据从这些PDO语句插入表(此表包含外键)中? - Why data is not inserted into the table(this table contains Foreign Key) from these PDO statements? 如何在Cakephp3复合主键中插入具有空值的记录? - how to insert record with a null in Cakephp3 composite primary key? 如何使用PHP PDO从SQL Server获取最后插入的记录 - How to get last inserted record from sql server using PHP PDO 插入新记录时如何自动刷新数据表? - How to auto refresh data table when new record is inserted? 使用PDO将记录插入宽表中 - Inserting record into a wide table with PDO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM