Php 插入到多个表中的帮助

Php 插入到多个表中的帮助,php,mysql,Php,Mysql,我正在建立一个学习计划 我已经设计了一个草稿界面和数据库,目前我正在尝试将测试注册细节插入数据库,但我已经做了三天了,没有成功。我将非常感谢你能帮我指出我做错了什么 我收到的信息/错误如下: Connected successfully Error details for Result 2: Cannot add or update a child row: a foreign key constraint fails (`p00702`.`universityreferences`, CON

我正在建立一个学习计划

我已经设计了一个草稿界面和数据库,目前我正在尝试将测试注册细节插入数据库,但我已经做了三天了,没有成功。我将非常感谢你能帮我指出我做错了什么

我收到的信息/错误如下:

Connected successfully

Error details for Result 2: Cannot add or update a child row: a foreign key constraint fails (`p00702`.`universityreferences`, CONSTRAINT `universityreferences_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `users` (`userid`) ON UPDATE CASCADE).

Error details for Result 3: Cannot add or update a child row: a foreign key constraint fails (`p00702`.`mobiles`, CONSTRAINT `mobiles_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `users` (`userid`) ON UPDATE CASCADE).

Error details for Result 4: Cannot add or update a child row: a foreign key constraint fails (`p00702`.`logins`, CONSTRAINT `logins_ibfk_1` FOREIGN KEY (`userid`) REFERENCES `users` (`userid`) ON UPDATE CASCADE).

All queries were rolled back
我还注意到,
$id=$mysqli->insert\u id在第一次查询之后也不起作用,我不知道为什么,但我怀疑这可能是根本原因

我的PHP代码是:

<?php
if (isset($_POST['submitted'])) {

    include 'serverSide/connectToServer.php';

    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $studentId = $_POST['studentId'];
    $mobile = $_POST['mobile'];
    $username = $_POST['username'];
    $password = $_POST['password'];

    //turn off autocommit
    $mysqli->autocommit(false);

    $flag = true;

    // attempt insert query executions
    $query1 = "INSERT INTO users (firstname, lastname) VALUES ('$firstName', '$lastName');";
    $id = $mysqli->insert_id;

    $query2 = "INSERT INTO universityreferences (userid, universityreference) VALUES ('$id', '$studentId');";
    $id2 = $mysqli->insert_id;

    $query3 = "INSERT INTO mobiles (userid, mobile) VALUES ('$id2', '$mobile');";
    $id3 = $mysqli->insert_id;

    $query4 = "INSERT INTO logins (userid, username, password) VALUES ('$id3', '$username', '$password');";

    $result1 = mysqli_query($mysqli, $query1);
    if (!$result1) {
        $flag = false;
        echo "Error details for Result 1: " . mysqli_error($mysqli) . ".";
    }

    $result2 = mysqli_query($mysqli, $query2);
    if (!$result2) {
        $flag = false;
        echo "Error details for Result 2: " . mysqli_error($mysqli) . ".";
    }

    $result3 = mysqli_query($mysqli, $query3);
    if (!$result3) {
        $flag = false;
        echo "Error details for Result 3: " . mysqli_error($mysqli) . ".";
    }

    $result4 = mysqli_query($mysqli, $query4);
    if (!$result4) {
        $flag = false;
        echo "Error details for Result 4: " . mysqli_error($mysqli) . ".";
    }

    if ($flag) {
        mysqli_commit($mysqli);
        echo "All queries were executed successfully";
    } else {
        mysqli_rollback($mysqli);
        echo "All queries were rolled back";
    }

    mysqli_close($mysqli);
}
?>

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<html>
    <head>
        <meta charset="UTF-8">
        <link rel="shortcut icon" type="image/png" href="/images/favicon.png"/>
        <title>Just-Read</title>
        <link rel="stylesheet" type="text/css" href="css/styles.css">
    </head>
    <body>
        <!-- Link to external JavaScript file -->
        <script src="javascript/validator.js"></script>
        <div id="container">
            <div id="header">
                <!-- Web site Logo -->
                <div class="logo">
                    <img src="images/logo.png" width="128" height="93.5" alt="Logo"/><br><br>
                </div>
                <div id="logoText">
                    <h1>Just Read</h1>
                </div>
            </div>
            <div id="leftColumn">
                <h4>The ultimate study planner</h4>
            </div>
            <div id="rightColumn">
                <!-- Registration Form -->
                <h3>Please fill out the form below</h3>
                <form name="Register" action="registration.php" onsubmit="return registrationValidator()" autocomplete="on" method="POST">
                    <!--According to YouTuber Ralph Philips, this makes sure a blank form cannot be submitted to the database-->
                    <input type="hidden" name="submitted" value="true"/>
                    <div class="register">
                        <label><b>First Name*</b></label>
                        <input type="text" id="firstName" name="firstName" placeholder="Enter your first name" autofocus/>
                        <label><b>Last Name*</b></label>
                        <input type="text" id="lastName" name="lastName" placeholder="Enter your last name" />
                        <label><b>Student ID*</b></label>
                        <input type="text" id="studentId" name="studentId" placeholder="Enter your university ID" />
                        <label><b>Mobile</b></label>
                        <input type="text" id="mobile" name="mobile" placeholder="Enter your phone number" />
                        <label><b>Email Address (Username)*</b></label>
                        <input type="email" id="username" name="username" placeholder="Enter your email address" />
                        <label><b>Password*</b></label>
                        <input type="password" id="password" name="password" placeholder="Enter your password" />

                        <button type="submit">Register</button>
                    </div>
                    <div id="back">
                        <a href="index.php">Back</a>
                    </div>
                    <div id="mandatoryFields">
                        <h4>* Mandatory Fields</h4>
                    </div>
                </form>
            </div>
            <div id="footer">
                Copyright &copy; 2017, Chizzy Meka.
            </div>
        </div>
    </body>
</html>

提前感谢您的帮助。

问题是您在执行查询之前正在使用
mysqli->insert\u id
。定义第一个查询时,数据库尚未生成id,因为尚未执行插入

正确的做法是

$query1 = "INSERT INTO users (firstname, lastname) VALUES ('$firstName', '$lastName');";

$result1 = mysqli_query($mysqli, $query1);
if (!$result1) {
    $flag = false;
    echo "Error details for Result 1: " . mysqli_error($mysqli) . ".";
}

$id = $mysqli->insert_id;

//go on with the next insert
//defining the query, executing it and then using the generated id

所有这些错误消息都是由以下事实引起的:当您运行query2时,您违反了您在
user\u id
上定义的外键,因为您插入了一个空值的行。

问题是,您试图在执行任何查询之前获取最后插入的id。例如:

$query1 = "INSERT INTO users (firstname, lastname) VALUES ('$firstName', '$lastName');";
$id = $mysqli->insert_id;
在上面的代码中,您只定义sql语句,但不执行它。因此,最后一个插入id不会返回任何内容

仅在执行查询后执行
insert\u id()
方法:

$result1 = mysqli_query($mysqli, $query1);

可能是重复的你好,@asteriskTheServer,谢谢你提出类似的问题,但是,我已经看到了那篇帖子,在我决定发布自己的帖子之前,它没有告诉我足够的信息。虽然我同意有相似之处,但我相信我的PHP是错误的,建议的答案不能帮助我解决这个问题。此外,我正在处理更多的表格,这使得事情变得更加棘手,因此,如果有人能调查我自己的情况并提供一些建议,我将不胜感激。非常感谢,@Stefano Zanini,我会让你知道它是如何工作的。它现在正在工作。非常感谢您,希望您在需要时也能找到帮助!非常感谢,@Shadow,我会让你知道它是如何工作的。它现在正在工作。非常感谢您,希望您在需要时也能找到帮助!
$result1 = mysqli_query($mysqli, $query1);