如何使用外键将数据插入多个php sql表?

时间:2018-02-27 18:10:07

标签: php html sql database login

我希望运行三个sql查询,将3组数据插入3个不同的sql表中,用于登录/注册系统。我需要能够同时更新所有三个表,同时保持所有键准确和最新。我能够得到两个表,但我的登录表不允许插入任何信息。

任何帮助都会有用:)

PHP代码:

if ($password == $password2){
        $password = md5($password);
        $sql = "INSERT INTO customer(FirstName, SecondName, Address, EmailAddress, TelephoneNumber) VALUES ('$firstname', '$surname', '$address', '$email', '$telephonenumber')";
        mysqli_query($db, $sql);
        $CustomerID = mysqli_insert_id($db);

        $sql2 = "INSERT INTO login(CustomerID, Username, PassW) VALUES ('$CustomerID', '$username', '$password')";
        mysqli_query($db, $sql2);


        $sql3 = "INSERT INTO inkstyle(CustomerID, Colour, Blackwork, Realism, Traditional, Japanese, Watercolour) VALUES ('$CustomerID', '$colour', '$blackwork', '$realism', '$traditional', '$japanese', '$watercolour')";
        mysqli_query($db, $sql3);

我的html代码在按钮和文本框方面都很好。

以下是在if语句之前声明的变量:

 $username= mysqli_real_escape_string($db, $_POST['username']);
$password= mysqli_real_escape_string($db, $_POST['password']);
$password2= mysqli_real_escape_string($db, $_POST['password2']);
$firstname= mysqli_real_escape_string($db, $_POST['firstname']);
$surname= mysqli_real_escape_string($db, $_POST['surname']);
$address= mysqli_real_escape_string($db, $_POST['address']);
$email= mysqli_real_escape_string($db, $_POST['email']);
$telephonenumber= mysqli_real_escape_string($db,      $_POST['telephonenumber']);

$colour= mysqli_real_escape_string($db, $_POST['cb1']);
$blackwork= mysqli_real_escape_string($db, $_POST['cb2']);
$realism= mysqli_real_escape_string($db, $_POST['cb3']);
$traditional= mysqli_real_escape_string($db, $_POST['cb4']);
$japanese= mysqli_real_escape_string($db, $_POST['cb5']);
$watercolour= mysqli_real_escape_string($db, $_POST['cb6']);

1 个答案:

答案 0 :(得分:0)

我已经解决了它的问题。这是因为我正在哈希密码,但我没有在sql表中设置足够的字符:)感谢您的帮助!