dqly83915 2019-04-18 19:15
浏览 110
已采纳

PHP - 使用2个外键将数据插入MySQL表

I'm doing a school project - a website with students performances in various sports. I have three tables:

TABLE1 - "students"

  • id (primary key)
  • class
  • firstname
  • lastname

TABLE2 - "sports"

  • sport_id (primary key)
  • sportname

TABLE3 - "performances"

  • performance_id (primary key)
  • sport_id (foreign key - sports.sport_id)
  • student_id (foreign key - students.id)
  • value

I want to make a form that adds data into the third table. That form should include:

  • class
  • firstname
  • lastname
  • sportname
  • value

...but I have no idea how to achieve this.

I could just create a form where user user adds value and then copy-pastes sport_id and student_id from tables below it, but that's unpractical.

I've been searching the internet for a while, but I haven't found any solution to this and if I did, it was only for one foreign key.

Does anyone know how to do this? If so, I would highly appreciate it! :)

EDIT: I should've mentioned that tables "students" and "sports" already have all the data in them, I just need to insert new performances using that data.

  • 写回答

2条回答 默认 最新

  • dtgr6303 2019-04-19 00:08
    关注

    Since the data is already in the tables for students and sports, this information can be queried with some select statements in order to populate some HTML dropdowns. The advantage of using the select queries and the dropdowns is that value of the options can be set to the database ID while showing the user the human-readable text. Then, the page just needs to monitor for the form's submission and insert the IDs from the dropdowns along with the performance metric. I have not tested the code below, but here is a quicky example of how that might work.

    Note: I like the PDO interface for preparing SQL queries in order to prevent injection attacks.

    <?php
    $user = 'user';
    $password = 'password';
    $con = new PDO('mysql:dbname=dbname;host=127.0.0.1;chartset=urf8', $user, $password);
    
    $student_stmt = $con->prepare('select * from students');
    $student_stmt->execute();
    
    $sport_stmt = $con->prepare('select * from sports');
    $sport_stmt->execute();
    
    if (isset($_GET['student']) && isset($_GET['sport']) && isset($_GET['value'])) {
        $student = $_GET['student'];
        $sport = $_GET['sport'];
        $value = $_GET['value'];
        $insert_stmt = $con->prepare('insert into preformances (sport_id, student_id, value) values (:sport_id, :student_id, :value)');
        $insert_stmt->bindParam(':sport_id', $sport);
        $insert_stmt->bindParam(':student_id', $student);
        $insert_stmt->bindParam(':value', $value);
        $insert_stmt->execute();
    }
    ?>
    
    <html>
        <head>
            <title>Form</title>
        </head>
        <body>
            <form action="self.php" method="get">
                Student:
                <select name="student">
        <?php while ($row = $student_stmt->fetch(PDO::FETCH_ASSOC)) { ?>
                    <option value="<?php echo $row['id']; ?>"><?php echo $row['firstname'] . " " . $row['lastname']; ?></option>
        <?php } ?>
                </select>
    
                Sport:
                <select name="sport">
        <?php while ($row = $sport_stmt->fetch(PDO::FETCH_ASSOC)) { ?>
                    <option value="<?php echo $row['sport_id']; ?>"><?php echo "$row['sportname']"; ?></option>
        <?php } ?>
                </select>
                Performance: <input name="value" type="text" />
                <button type="submit">Submit</button>
            </form>
        </body>
    </html>
    

    Edit: Made the changes in the code in the suggested comment.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败