我正在做一个学校项目--一个学生在各种体育活动中表现的网站。我有三张桌子:
TABLE1 -“学生”
TABLE2 -“体育”
TABLE3 -“表演”
我想要创建一个表单,将数据添加到第三个表中。该表格应包括:
...but我不知道如何实现这一点。
我可以创建一个表单,用户可以在其中添加值,然后从下面的表复制粘贴sport_id和student_id,但这是不切实际的。
我搜索互联网已经有一段时间了,但我没有找到任何解决方案,如果我找到了,它只是一个外键。
有人知道怎么做吗?如果是的话,我会非常感激的!)
编辑:我应该提到表“学生”和“体育”已经有了所有的数据,我只需要使用这些数据插入新的表演。
发布于 2019-04-19 00:08:00
由于数据已经在表中为学生和体育,这些信息可以查询一些select
语句,以填充一些HTML下拉列表。使用select查询和下拉列表的优点是可以将选项的value
设置为数据库ID,同时向用户显示可读的文本。然后,页面只需监视表单的提交,并从下拉列表中插入ID以及性能度量。我还没有测试下面的代码,但是下面是一个快速的示例,说明了它是如何工作的。
注意:为了防止注入攻击,我喜欢用于准备SQL查询的PDO接口。
<?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>
编辑:在建议的注释中对代码进行了更改。
发布于 2019-04-18 19:38:36
我认为您所需要做的就是从表单($variable = $_GET["classinput"];
)中获取输入值,然后连接到数据库,并为每个表编写带有输入查询的mysqli查询。
就像这样:
$query = mysqli_query($connection, "INSERT INTO STUDENTS(id,class,firstname,lastname) VALUES (null,\"$class\",\"$firstname\",\"$lastname\")");
对你所有的桌子都这么做。
https://stackoverflow.com/questions/55752447
复制相似问题