使用用户输入连接2个表

时间:2014-09-26 16:56:18

标签: php mysql sql

好吧我设置了外键,我可以通过phpmyadmin中的sql标签加入表格,但我无法弄清楚如何让外界正常工作我允许用户通过表单输入值。现在我只是玩一个非常基本的表格,允许使用名字,姓氏和电话号码。 First和Last名称将发送到名为Customer的表,并且电话号码将传递到名为Customer_Number的表。问题是,现在当我在输入字段中输入值时,名字和姓氏保存到数据库表中,但电话号码不保存并吐出此错误消息Cannot add or update a child row: a foreign key constraint fails (``.Customer_Number, CONSTRAINT Customer_Number_ibfk_1 FOREIGN KEY (Customer_ID) REFERENCES Customer (Customer_ID))

有没有办法可以根据正确的客户为我的Customer_Number表更新添加这些值和外键?

这是我的代码:

 <?php

        $con=mysqli_connect("");
            if(mysqli_connect_errno()){
                echo "There was a mistake connecting". mysqli_connect_errno();
                }
        $First=mysqli_real_escape_string($con,$_POST["FirstName"]);
        $Last=mysqli_real_escape_string($con, $_POST["LastName"]);
            if(!empty($_POST["FirstName"]) && !empty($_POST["LastName"])){
                $sql="INSERT INTO Customer(First,Last)
                    VALUE('$First', '$Last')";

                        if(!mysqli_query($con,$sql)) {
                            die("ERROR". mysqli_error($con));
                            }else{
                                echo"record added";
                                }
                            }
                                    mysql_close($con);
                            ?>
     <?php

        $con=mysqli_connect("");
            if(mysqli_connect_errno()){
                echo "There was a mistake connecting". mysqli_connect_errno();
                }
        $Phone=mysqli_real_escape_string($con,$_POST["Number"]);

            if(!empty($_POST["Number"])){
                $sql="INSERT INTO Customer_Number(Number)
                    VALUE('$Phone')";

                        if(!mysqli_query($con,$sql)) {
                            die("ERROR". mysqli_error($con));
                            }else{
                                echo"record added";
                                }
                            }
                                    mysql_close($con);
                            ?>

这是我的表信息:

CREATE TABLE `Customer` (
`Customer_ID` int(11) NOT NULL AUTO_INCREMENT,
`First` varchar(255) NOT NULL,
`Last` varchar(255) NOT NULL,
 PRIMARY KEY (`Customer_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1

CREATE TABLE `Customer_Number` (
`Num_ID` int(11) NOT NULL AUTO_INCREMENT,
`Customer_ID` int(11) NOT NULL,
`Number` varchar(255) NOT NULL,
PRIMARY KEY (`Num_ID`),
KEY `Customer_ID` (`Customer_ID`),
CONSTRAINT `Customer_Number_ibfk_1` FOREIGN KEY (`Customer_ID`) REFERENCES `Customer`         (`Customer_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

我知道必须有一种方法可以做到这一点,但我是新手,我的谷歌搜索只是真正介绍了如何通过phpMyAdmin和手动输入外键值。

1 个答案:

答案 0 :(得分:0)

由于Customer_Number.Customer_ID是指向相应Customer行的外键,因此在插入Customer_Number.Customer_ID后插入数字时,需要将Customer设置为正确的值。

只要您使用相同的连接(即在查询之间跳过关闭/重新打开),就可以使用LAST_INSERT_ID()来获取刚刚插入的客户的Customer_ID,例如(SQL)仅部分);

INSERT INTO Customer(First,Last) 
  VALUES ('$First', '$Last')

INSERT INTO Customer_Number(Customer_ID, Number) 
  VALUES (LAST_INSERT_ID(), '$Phone')