首页 > 解决方案 > 如何从表单中获取结果并将它们发送到 PHP 和 SQL 中的不同表

问题描述

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

在我的第一个表中,我有以下列: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';
}



}


?>

任何帮助表示赞赏:)

标签: phpsqlmysqliinput

解决方案


步骤(除了@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);

推荐阅读