通过外键同时插入两个表,A表有字段id,name,pw,其中id是主键是一个自增字段,B表有字段id,p_id,其中id也是主键,p_id是外键,现在我想插入A表的时候同时也插入B表且B表中的p_id的值要等于A表中的id的值。我应该怎么写?谢谢各位
解决方案 »
- ucenter接口实例中无法同步退出
- PHP Unlink 错误提示: warning: Unlink failed (permission denied) in ...... on line 51
- linux主机,网站安装的是mysql,网站现可运行,补充安装phpmyadmin出问题
- PHP Script 来实现判断来路,对指定来路自动跳转
- 求代码,打开网站输入以下任意关键词搜索
- pear+smarty 的set_include_path 问题
- 为什么我去官网找不到ckeditor for php?
- jaris flv player怎么配置 rtmp流啊
- 求cmseasy大牛
- 求助!!!网站3天2头被日,烦死了!求高手指点下怎么做好维护?
- php如何限定一个函数的执行时间???
- 有没有类似haozip.com或者coolrar.com
你看一下这个把,是数据库触发器,不是php触发器用php的话可以这样class A{ public function insertA(){
//插入A表的代码
$this->insertB($a_id);
} public function insertB($a_id){
//插入B表的代码
}
}这样只需要调用insertA()就会自动调用insertB()了
//插入A表的代码
$this->insertB($a_id);
} public function insertB($a_id){
//插入B表的代码
}
}
http://blog.csdn.net/mengxiangbaidu/article/details/7345108
$f = mysql_query("INSERT INTO `A` (name,pw) VALUES (".$name.",".$pw.")");
if($f){
$p_id = mysql_insert_id();
mysql_query("INSERT INTO `B` (p_id) VALUES (".$p_id.")");}
本来想在PHP 论坛来看哈帖子,结果又看到mysql的帖子了。
-------------------------------------------------------------------------------------------
可以用个触发器,大概写一下伪代码
CREATE TRIGGER test AFTER INSERT ON A
FOR EACH ROW BEGIN
IF new.id!=null
then
insert into b(id ,pid) values (new.id); end if
END
<form action="charu.php" method="post">
<input name="name" type="text" />
<input type="submit" value="提交" name="submit" />
</form>
这是提交部分<?php
include("conn.php");
$name = trim($_POST['name']);
mysql_query("insert into jf(id,name) values(null,".$name.")",$conn);
if($f){
$jf_id = mysql_insert_id();
mysql_query("INSERT INTO user (id,jf_id) VALUES (null,".$jf_id.")",$conn);}
?>这是印证部分,写不进去数据库呀 奇怪啦
if($f){
$jf_id = mysql_insert_id();
mysql_query("INSERT INTO user (id,jf_id) VALUES (null,'$jf_id')",$conn) or die(mysql_error());
}这样试试。
; you're strongly encouraged to turn this feature off, and use error logging
; instead (see below). Keeping display_errors enabled on a production web site
; may reveal security information to end users, such as file paths on your Web
; server, your database schema or other information.
display_errors = On; Even when display_errors is on, errors that occur during PHP's startup
; sequence are not displayed. It's strongly recommended to keep
; display_startup_errors off, except for when debugging.
display_startup_errors = Off
已经开启啦
更改的是否是apache 加载的php.ini ?
自己再调试调试。
include("conn.php");
$name = trim($_POST['name']);
mysql_query("insert into jf(id,name) values(null,'$name')",$conn) or die(mysql_error());
if(!$f){
$jf_id = mysql_insert_id();
mysql_query("insert into user(id,jf_id values (null,'$jf_id')",$conn) or die(mysql_error());
}
?>就会提示这个了 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'values (null,'22')' at line 1