//大概也可以这个样子 $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
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()); }这样试试。
20楼的jf这个表示可以插入了,可是user这个没有插入。
mysql_query("INSERT INTO `user` (id,jf_id) VALUES (null,'$jf_id')",$conn) or die(mysql_error());还有错的话请贴出错误信息。
你没有打开错误显示功能,php.ini中令 display_errors = On 重启apache
; Print out errors (as a part of the output). For production web sites, ; 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 已经开启啦
会呢 有提示呢这样写<?php 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
我查出来了,为什么不提示,是没有加$f这个,但是出现了错误提示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,'24')' at line 1这样一个,是怎么回事呢
你看一下这个把,是数据库触发器,不是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