使用 mysql 和 php 将数据插入两个 mysql 表中


Inserting data into two mysql tables using mysql and php

我有两个表,分别名为'Students_tbl'和'admission'。我想同时在两个表中插入录取号,以便在"students_tbl"中,它是一个外键,而在"录取"表中,它是一个主键。"students_tbl"的主键为"std_index"我正在使用一个 html 表单。我编写的代码正在输出错误。提前感谢您的回复

这是代码

<?php
$manzu =mysqli_connect("localhost","root","MANZu1992", "cdms");
// Check connection
if (!$manzu) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    echo "Please Check your connection. We were unable to connect you to the desired site.";
}
if (isset($_POST['submit'])) {
    $identification = mysqli_real_escape_string($manzu, $_POST['iddd']);
    $National_Number = mysqli_real_escape_string($manzu, $_POST['national_Numberr];

    $sql = "INSERT INTO students_tbl (std_index,std_national_number) 
    VALUES ('$identification','$National_Number')";
    $sql = "INSERT INTO admission (Admission_Number)VALUES($National_Number)";
    if (!mysqli_query($manzu,$sql)) {
        die('Error: ' . mysqli_error($manzu));
    }ELSE {
        die ('Thank you for registering');
    }
}
?>
  1. 您缺少右括号和引号
  2. 在覆盖$sql之前,您没有执行第一个查询

    <?php    
    $manzu =mysqli_connect("localhost","root","MANZu1992", "cdms");
    if (!$manzu) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        echo "Please Check your connection. We were unable to connect you to the desired site.";
    }    
    if (isset($_POST['submit'])) {
        $identification = mysqli_real_escape_string($manzu, $_POST['iddd']);
        $National_Number = mysqli_real_escape_string($manzu, $_POST['national_Numberr']);
        $sql = "INSERT INTO students_tbl (std_index,std_national_number)  VALUES ('$identification','$National_Number')";
        mysqli_query($manzu,$sql);
        $sql = "INSERT INTO admission (Admission_Number)VALUES($National_Number)";  
        if (!mysqli_query($manzu,$sql)) {
            die('Error: ' . mysqli_error($manzu));
        }else {
            die ('Thank you for registering');
        }
    }
    ?>