PHP:使用触发器使用 sqlsrv pdo 插入

发布于 2024-10-30 06:19:01 字数 1668 浏览 4 评论 0原文

我正在使用 sqlsrv pdo 驱动程序(同时使用 5.2 和 5.3 版本),并且我看到了我认为是意外的行为。希望我做错了什么。

我在 sql express 2008 中创建了 2 个表。其中一个(位置)是与其他表共享主键的表。另一个表 (Rooms) 有 2 个(或更多)列...一个键列作为外键链接到第一个表和一个(或多个)数据列。所以它看起来像:

位置

LocationID int
1
2
3
4
5

房间

LocationID int,房间名称 varchar(50)
2、‘生活’
4、‘就餐’

2,4 都链接回“Locations”表。

然后,我在 Roomname 列上创建一个唯一约束:

ALTER TABLE Rooms ADD CONSTRAINT UniqueRoom UNIQUE (Roomname)

接下来,我在 Rooms 表上创建了一个 INSTEAD OF INSERT 触发器:

CREATE TRIGGER [dbo].[Rooms_INSERT] 
on [dbo].Rooms 
INSTEAD OF INSERT 
as 
BEGIN 
 INSERT INTO dbo.Locations DEFAULT VALUES 
 INSERT INTO dbo.Rooms select @@IDENTITY, Roomname from inserted    
END  

现在我可以通过以下方式将数据插入到 rooms 表中:

INSERT INTO Rooms VALUES (null, 'roomname')

我使用 sqlsrv PDO 对象来通过 PHP 插入数据:

$db = new PDO("sqlsrv:server=$serverName;Database=db", "", "");

$sql=("insert into Rooms values (null,?)");
$dbobj = $db->prepare($sql);
if($dbobj->execute(array("dining")))
    print "<p>insert successful</p>";
else
    print "<p>insert unsuccessful</p>";

从上面列出的表开始,我得到以下结果:

  • 将插入语句修改为“插入房间值(?)” - 按预期失败,因为它需要两个参数“插入不成功”。
  • 将插入语句恢复正常,尝试插入数组(“卧室”) - 如预期成功,“插入成功”。
  • 重新尝试插入数组(“卧室”) - 数据库插入失败,两个表中均未生成任何条目,但尝试报告成功“插入成功”。

当数据库中没有持久条目时,为什么第三次尝试报告为成功?或者,更具体地说,如何修改 PHP 或 SQL 来正确检测此类插入何时因违反唯一约束而失败?

I'm using sqlsrv pdo driver (used both 5.2 and 5.3 versions) and I'm seeing what I believe is unintended behavior. Hopefully I'm doing something wrong.

I've created 2 tables in sql express 2008. One (Locations) is a table of primary keys shared with other tables. The other table (Rooms) has 2 (or more) columns...a key column linked as a foreign key to the first table and one (or more) data columns. So it looks like:

Locations

LocationID int
1
2
3
4
5

Rooms

LocationID int, Roomname varchar(50)
2, 'living'
4, 'dining'

2,4 both link back to the Locations table.

Then I create a unique constraint on the Roomname column:

ALTER TABLE Rooms ADD CONSTRAINT UniqueRoom UNIQUE (Roomname)

Next, I created an INSTEAD OF INSERT trigger on the Rooms table:

CREATE TRIGGER [dbo].[Rooms_INSERT] 
on [dbo].Rooms 
INSTEAD OF INSERT 
as 
BEGIN 
 INSERT INTO dbo.Locations DEFAULT VALUES 
 INSERT INTO dbo.Rooms select @@IDENTITY, Roomname from inserted    
END  

Now I can insert data into the rooms table by:

INSERT INTO Rooms VALUES (null, 'roomname')

I use the sqlsrv PDO object to insert data through PHP:

$db = new PDO("sqlsrv:server=$serverName;Database=db", "", "");

$sql=("insert into Rooms values (null,?)");
$dbobj = $db->prepare($sql);
if($dbobj->execute(array("dining")))
    print "<p>insert successful</p>";
else
    print "<p>insert unsuccessful</p>";

Starting with the table listing above, I get the following results:

  • Modify insert statement to "insert into Rooms values (?)" - fails as expected because it expects two args, "insert unsuccessful."
  • Revert insert statement to normal, attempt to insert array("bedroom") - successful as expected, "insert successful."
  • Re-attempt insert of array("bedroom") - db insert fails, no entries are made in either table, but attempt is reported successful "insert successful."

Why is the 3rd attempt reported as successful when there were no lasting entries made to the database? Or, more specifically, how can I modify either my PHP or my SQL to properly detect when an insert like this fails due to a violation of a unique constraint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文