【问题标题】:How to take results from a form and send them to different tables in PHP and SQL如何从表单中获取结果并将它们发送到 PHP 和 SQL 中的不同表
【发布时间】:2018-06-20 06:00:43
【问题描述】:

我正在尝试从我的输入表单中获取结果并将结果发送到两个不同的表。

在我的第一个表中,我有以下列:PersonID、StudentID、FirstName、LastName、

在我的第二个表中,我有以下列:EventID、EventName

在我的第三个链接表中,我有以下列:PersonID、EventID 和 Time

我想从我的表单中获取所有输入并将它们分配到所有三个表中,但我不确定如何执行此操作。

这是我的 PHP:

  <form action="enter.php" method="post">
    <h2>Student ID: </h2>
    <input name="StudentID"/>
    <h2>First Name: </h2>
    <input name="FirstName"/>
    <h2>Last Name: </h2>
    <input name="LastName"/>
    <h2>Event Name: </h2>
    <input name="EventName"/>
    <h2>Event Time: </h2>
    <input name="EventTime"/>
    <br /><input type="submit" name="submit" value="Enter Results for Person"/>
  </form>

<?php
if(!mysqli_select_db($dbcon,'NAMEOFDATABASE')) {
    echo 'Database not selected!';
} 
if(isset($_POST['submit'])){

$StudentID = $_POST['StudentID'];
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];

$sql = "INSERT INTO Persons (StudentID, FirstName, LastName) VALUES ('$StudentID','$FirstName', '$LastName')";
if(!mysqli_query($dbcon,$sql)) {
    echo 'Person was NOT inserted into Persons Table Succesfully';
} else {
    echo 'Person was inserted into Persons Table Succesfully';
}



}


?>

任何帮助表示赞赏:)

【问题讨论】:

标签: php sql mysqli input


【解决方案1】:

步骤(除了@Jens 所说的有效):

1) insert into person table like you do 
2) insert into Events in the same way (separate statement and mysqli_query statement) 
3) select person_id from persons where student_id = <that student id> -- let's hope that your table constraint makes student ID unique >
4) select event_id from Events where event_name = <event_name from above> -- same warning about event name being unique in the events table
5) insert the person_id event_id in the cross table 

给出这个,必须存在其他一些逻辑。如果该人存在该学生证,您应该发现违规行为。您可以忽略它或更新学生姓名。如果你有同样的事件?相同的。如果由于学生已经存在以外的其他原因无法插入学生,则不应继续。如果您无法插入事件,您可能不需要撤消学生插入。

我假设您正在将其作为一个学习练习并希望手动编写逻辑代码。否则,您将使用 ORM https://www.killerphp.com/articles/what-are-orm-frameworks/

如果您将 StudentID 和 EventName 作为主键,则您已经有了 ID,因此:

$sql1 = "INSERT INTO Persons (StudentID, FirstName, LastName) VALUES ('$StudentID','$FirstName', '$LastName')";
$sql2 = "INSERT INTO Events (EventName, EventTime) VALUES ('$EventName','$EventTime')";
$sql3 = "INSERT INTO EventPerson (EventName, StudentID) VALUES ('$EventName','$StudentID')";

// insert person
if(mysqli_query($dbcon,$sql1)) {
    echo 'Person was inserted into Persons Table Successfully';
    // set error and return  
} elsif (// error is already exists - primary key error){
    echo 'Person already existed';
} else {
    echo 'Person was NOT inserted into Persons Table Successfully';
    // set error and return
}

// insert event
if(mysqli_query($dbcon,$sql2)) {
    echo 'Event was inserted into Events Table Successfully'; 
} elif (// error is already exists - primary key error){
    echo 'Event already existed';
} else {
    echo 'Event was NOT inserted into Events Table Successfully';
    // set error and return
}

// insert cross reference table
if(mysqli_query($dbcon,$sq3)) {
    echo 'Reference was inserted successfully';  
} else {
    echo 'Reference was NOT inserted successfully';
    // set error and return
}

由于上面提到的注入,这仍然是糟糕的代码。查看和使用:http://php.net/manual/en/mysqli-stmt.bind-param.php,例如:

$stmt = $mysqli->prepare("INSERT INTO Persons (StudentID, FirstName, LastName) VALUES (?, ?, ?)");
$stmt->bind_param('sss', $StudentID, $FirstName, $LastName);

【讨论】:

  • 感谢这些步骤 :) 我对如何尝试第 3 步和第 4 步(ID 是主键)有点困惑,我应该将其作为查询执行还是可以直接执行php。谢谢
  • 相应地附加我的回复。
【解决方案2】:

查看以下代码,使用 mysqli_insert_id() 获取自动生成的表 id

<form action="enter.php" method="post">
        <h2>Student ID: </h2>
        <input name="StudentID"/>
        <h2>First Name: </h2>
        <input name="FirstName"/>
        <h2>Last Name: </h2>
        <input name="LastName"/>
        <h2>Event Name: </h2>
        <input name="EventName"/>
        <h2>Event Time: </h2>
        <input name="EventTime"/>
        <br /><input type="submit" name="submit" value="Enter Results for Person"/>
      </form>

    <?php
    if(!mysqli_select_db($dbcon,'NAMEOFDATABASE')) {
        echo 'Database not selected!';
    } 
    if(isset($_POST['submit'])){

    $StudentID = $_POST['StudentID'];
    $FirstName = $_POST['FirstName'];
    $LastName = $_POST['LastName'];
    $EventName = $_POST['EventName'];
    $EventTime = $_POST['EventTime'];

    $sql = mysqli_query($dbcon,"INSERT INTO Persons (StudentID, FirstName, LastName) VALUES ('$StudentID','$FirstName', '$LastName'");

    $person_id = mysqli_insert_id($dbcon);

    $sql1 = mysqli_query($dbcon,"INSERT INTO first_table (PersonID, StudentID, FirstName, LastName) VALUES ('$person_id','$StudentID', '$FirstName', '$LastName' ");

    $sql2 = mysqli_query($dbcon,"INSERT INTO second_table (EventName) VALUES ('$EventName'");

    $event_id = mysqli_insert_id($dbcon);

    $sql3 = mysqli_query($dbcon,"INSERT INTO third_table (PersonID, EventID , Time) VALUES ('$person_id', '$event_id', '$EventTime'");


    if(!sql1 && !$sql2 && !$sql3 ) {
        echo 'Person was NOT inserted into Persons Table Succesfully';
    } else {
        echo 'Person was inserted into Persons Table Succesfully';
    }


    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    • 1970-01-01
    • 2015-03-22
    • 2021-07-21
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多