我需要一些方法方面的帮助。
我有3个text
输入,格式为'add_task'
:
<form action="page.php" method="POST">
<input type="text" name="task_string" value="" />
<input type="text" name="hours" value="" />
<input type="text" name="minutes" value="" />
<input type="submit" value="Add Task" />
</form>
表单使用上面的输入"创建"一个带有时间表的任务。
现在,我有两个表,tasks
和timelines
我可以用task_string
值INSERT INTO
任务表,然后该条目保存一个自动递增的唯一id
。
// tasks
+----+------------------+
| id | string |
+----+------------------+
| 1 | task string here |
+----+------------------+
我现在需要将minutes
和hours
值输入到timelines
表中,但我需要参考上面的tasks.id
//timelines
+----+-------+---------+-----------------------------+
| id | hours | minutes | task_id |
+----+-------+---------+-----------------------------+
| 1 | 3 | 29 | // task_id from table above |
+----+-------+---------+-----------------------------+
如何在多用户环境中使用SQL Server和PHP实现这一点?
您可以将@@IDENTITY
用于此目的;下面是一个例子。但对于多用户环境,您必须使用transaction block
来确保两次插入都成功进行。
create procedure doDML(@task_string varchar(100),@hours int, @minutes int)
as
begin
declare @lastID int;
insert into tasks values(@task_string);
set @lastID = @@IDENTITY;
insert into timelines values(@hours, @minutes, @lastID);
end