【问题标题】:PHP - Insert data into MySQL table with 2 foreign keysPHP - 使用 2 个外键将数据插入 MySQL 表
【发布时间】:2019-09-09 04:04:48
【问题描述】:

我正在做一个学校项目 - 一个展示学生在各种运动项目中表现的网站。我有三张桌子:

TABLE1 - “学生”

  • id(主键)
  • 名字
  • 姓氏

TABLE2 - “运动”

  • sport_id(主键)
  • 运动名称

TABLE3 - “表演”

  • performance_id(主键)
  • sport_id(外键 -sports.sport_id)
  • student_id(外键-students.id)
  • 价值

我想制作一个表格,将数据添加到第三个表中。 该表格应包括:

  • 名字
  • 姓氏
  • 运动名称
  • 价值

...但我不知道如何实现这一点。

我可以创建一个用户用户添加价值的表单,然后从它下面的表中复制粘贴 sport_id 和 student_id,但这是不切实际的。

我已经在互联网上搜索了一段时间,但我没有找到任何解决方案,如果我找到了,那也只是一个外键。

有人知道怎么做吗?如果是这样,我将不胜感激! :)

编辑:我应该提到“学生”和“体育”表中已经包含所有数据,我只需要使用这些数据插入新的表演。

【问题讨论】:

  • 到目前为止你做了什么?
  • 在第一个表中插入 - 您将使用 last_insert_id 获取新的 ID,与第二个表相同,然后使用两个 ID 插入到第三个表中。 (或者如果它已经存在,则通过下拉列表获取sports.id)
  • 我有一个工作表格,其中只有 sport_id、student_id 和 value + 一个表格,其中显示来自表格 SELECT * FROM performances INNER JOIN sports ON sports.sport_id = performances.sport_id INNER JOIN students ON students.id = performances.student_id 的所有必需数据
  • 我应该提到表“学生”和“体育”已经包含了所有数据,我只需要使用这些数据插入新的表演。

标签: php mysql


【解决方案1】:

由于数据已经在学生和体育的表格中,因此可以使用一些 select 语句查询此信息,以填充一些 HTML 下拉列表。使用选择查询和下拉菜单的优点是value 的选项可以设置为数据库 ID,同时向用户显示人类可读的文本。然后,页面只需要监视表单的提交,并从下拉列表中插入 ID 以及性能指标。我没有测试过下面的代码,但这里有一个简单的例子来说明它是如何工作的。

注意:我喜欢 PDO 接口用于准备 SQL 查询以防止注入攻击。

<?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>

编辑: 对建议评论中的代码进行了更改。

【讨论】:

  • 谢谢!我设法通过少量更改使其工作 - 我希望它也对其他人有所帮助! 1. 我将charset=utf8添加到$con,这样“č”或“ř”之类的字符才能正确显示 2. 将($_GET['student'] 替换为(isset($_GET['student']) 3. 将$row['student_id'] 替换为$row['id']
  • 谢谢!对代码进行了更新。我已经有一段时间没有使用 PHP 了,所以我完全忘记了 isset() 函数:)
【解决方案2】:

我认为您需要做的就是从表单 ($variable = $_GET["classinput"];) 中获取输入值,然后连接到数据库并为每个表编写带有输入查询的 mysqli 查询。

像这样:

$query = mysqli_query($connection, "INSERT INTO STUDENTS(id,class,firstname,lastname) VALUES (null,\"$class\",\"$firstname\",\"$lastname\")");

并为所有表格执行此操作。

【讨论】:

  • 我应该提到表“学生”和“体育”已经包含了所有数据,我只需要使用这些数据插入新的表演
猜你喜欢
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
  • 1970-01-01
  • 2015-09-08
  • 1970-01-01
  • 2015-05-27
相关资源
最近更新 更多