通过外键同时插入两个表,A表有字段id,name,pw,其中id是主键是一个自增字段,B表有字段id,p_id,其中id也是主键,p_id是外键,现在我想插入A表的时候同时也插入B表且B表中的p_id的值要等于A表中的id的值。我应该怎么写?谢谢各位

解决方案 »

  1.   

    那B表p_id这个怎么才能等于A表的id呢?能说清楚一点吗?写一段代码可以吗
      

  2.   

    http://cn.php.net/manual/en/function.mysql-insert-id.php这是API。
      

  3.   

    如果你不想通过php代码来操作B表,你看一下数据库的触发器就可以了,可以处理在A表有数据插入的时候自动在B表实现插入操作
      

  4.   

    php触发器这个不行,我是在学习,我想知道的是sql如何实现,3楼给我看的我找了一会儿好像找不到我想要的那个,也没有说明哦
      

  5.   

    http://baike.baidu.com/view/1189954.htm
    你看一下这个把,是数据库触发器,不是php触发器用php的话可以这样class A{  public function insertA(){
        //插入A表的代码
         
        $this->insertB($a_id);
      }  public function insertB($a_id){
        //插入B表的代码
      }
    }这样只需要调用insertA()就会自动调用insertB()了
      

  6.   

    class A{  public function insertA(){
        //插入A表的代码
         
        $this->insertB($a_id);
      }  public function insertB($a_id){
        //插入B表的代码
      }
    }
      

  7.   

    三楼的(mysql_insert_id)是,返回刚才插入那条记录的ID
      

  8.   

    用mysql 触发器
    http://blog.csdn.net/mengxiangbaidu/article/details/7345108
      

  9.   

    //大概也可以这个样子
    $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.")");}
      

  10.   


    本来想在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
      

  11.   

    mysql_insert_id() 为什么不行?
      

  12.   


    <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);}
    ?>这是印证部分,写不进去数据库呀 奇怪啦
      

  13.   

     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());
    }这样试试。
      

  14.   

    20楼的jf这个表示可以插入了,可是user这个没有插入。
      

  15.   

    mysql_query("INSERT INTO `user` (id,jf_id) VALUES (null,'$jf_id')",$conn) or die(mysql_error());还有错的话请贴出错误信息。
      

  16.   

    你没有打开错误显示功能,php.ini中令  display_errors = On 重启apache
      

  17.   

    ; 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
    已经开启啦
      

  18.   

    有没有重启apache?
    更改的是否是apache 加载的php.ini ?
      

  19.   

    出鬼了? 第二句SQL为什么不行? 出错了应该会有提示的啊。其他地方出错了会有提示吗?
    自己再调试调试。
      

  20.   

    会呢 有提示呢这样写<?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
      

  21.   

    我查出来了,为什么不提示,是没有加$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这样一个,是怎么回事呢