简体   繁体   中英

How to insert data into multiple php sql tables with foreign keys?

I am looking to run three sql queries to insert 3 sets of data into 3 different sql tables for a login/reg system. I need to be able to update all three tables at once whilst keeping all keys accurate and up to date. Im able to get two tables working but my login table wont allow any info to be inserted.

Any help will be useful :)

PHP CODE:

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);

My html code is fine in terms of buttons and text boxes.

Here are the variable ive declared before the if statment:

 $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']);

I've solved it bois. It was because i was hashing the password but i didnt set enough characters in the sql table :) thanks for the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM