drodsh7940 2018-06-20 06:00
浏览 82
已采纳

如何从表单中获取结果并将它们发送到PHP和SQL中的不同表

I am trying to take the results from my input form and sent the results to two different tables.

In my first table I have the following columns: PersonID, StudentID, FirstName, LastName,

In my second table I have the following columns: EventID, EventName

In my third linking table I have the following columns: PersonID, EventID and Time

From my form, I want to take all the inputs and distribute them across all three tables, but I'm not sure how to do this.

Here is my 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';
}



}


?>

Any help is appreciated :)

  • 写回答

2条回答 默认 最新

  • douxishai8552 2018-06-20 06:33
    关注

    steps (besides what @Jens says which is valid):

    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 
    

    Give this, some other logic must exist. If the person exists with that student id you should catch the violation. You could ignore it or update the students name maybe. If you have the same event? Same. If you can't insert the student for some reason other than the student already existing, you should not proceed. If you can't insert the event you probably don't need to undo thee student insert.

    I would assume you are doing this as a learning exercise and want to code the logic by hand. Otherwise you would be using an ORM https://www.killerphp.com/articles/what-are-orm-frameworks/ .

    If you have StudentID and EventName as primary keys you already have the IDs, so:

    $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
    }
    

    and this is still bad code, because of injection mentioned above. See and use: http://php.net/manual/en/mysqli-stmt.bind-param.php, for example:

    $stmt = $mysqli->prepare("INSERT INTO Persons (StudentID, FirstName, LastName) VALUES (?, ?, ?)");
    $stmt->bind_param('sss', $StudentID, $FirstName, $LastName);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM