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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)