gpt4 book ai didi

php - 使用 php 创建多个表单插入并将其输入到 mysql

转载 作者:行者123 更新时间:2023-11-29 18:42:33 26 4
gpt4 key购买 nike

我对 php 相当陌生,我很难弄清楚如何制作一个可以有多个输入并使用 php 将所有输入输入到 sql 表中的表单。我设置了一个表,以便每一行都是一个新条目,这样可以一次全部上传时间表,而不是 1 个一个地上传。

这是我当前的代码:

html:

<table id="register">
<form action="processing_games.php" method="post"/>
<tr>
<td><p>Date<br>(m/dd/yy)</p></td>
<td><p>Time<br>(h:mm AM/PM)</p></td>
<td><p>Opponent</p></td>
<td><p>Location<br>(Rink Name)</p></td>
<td><p>Conference<br>Game?</p></td>
<td><p>Home or Away<br>Game?</p></td>
</tr>
<tr>
<td><input type="date" name="date"></td>
<td><input type="time" name="time"></td>
<td><input type="text" name="opponent"></td>
<td><input type="text" name="location"></td>
<td><input type="radio" name="conference" value="+"> Conference
<br>
<input type="radio" name="conference" value=""> Non-Conference </td>
<td><input type="radio" name="home_away" value=" vs "> Home
<br>
<input type="radio" name="home_away" value=" @ "> Away</td>
</tr>
<tr>
<td><input type="date" name="date"></td>
<td><input type="time" name="time"></td>
<td><input type="text" name="opponent"></td>
<td><input type="text" name="location"></td>
<td><input type="radio" name="conference" value="+"> Conference
<br>
<input type="radio" name="conference" value=""> Non-Conference </td>
<td><input type="radio" name="home_away" value=" vs "> Home
<br>
<input type="radio" name="home_away" value=" @ "> Away</td>
</tr>
</table>
<br><br>
<center>
<input type="submit" value="Submit"/>
</form>

php:

<?php
include '../connection.php';

$game_date = $_POST['date'];
$game_time = $_POST['time'];
$value3 = $_POST['opponent'];
$value4 = $_POST['location'];
$value5 = $_POST['conference'];
$value6 = $_POST['home_away'];

$value = date('n/j/y', strtotime($game_date));
$value2 = date('g:i A', strtotime($game_time));

$sql = "INSERT INTO schedule (date, time, opponent, location, conference, home_away) VALUES ('$value', '$value2', '$value3', '$value4', '$value5', '$value6')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
header("Location: http://www.rit.edu/sg/clubhockey/admin/complete.php");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

mysqli_close($conn);
?>

感谢任何帮助。

最佳答案

您可以转换 html 以将发布数据作为“多维”数组发送。更新所有输入名称(日期、时间、开放...除了提交!):

name="date"

name="items[0][date]" // for first block of inputs
name="items[1][date]" // for second block of inputs
name="items[n][date]" // for nth block ;)

关键“项目”只是一个例子!

将您的表单发布到 processing_games.php 和 var_dump($_POST)。您应该看到 $_POST 数组输出结构,如下所示:

Array
(
[items] => Array
(
[0] => Array
(
[date] => your input date 1
[oppenent] => your input openent 1
)

[1] => Array
(
[date] => your input date 2
[oppenent] => your input openent 2
)
)
)

然后您可以迭代项目数组 ($_POST['items']) 构建插入查询。举个例子:

<?php

$postItems = null;
if (isset($_POST['items'])) {
$postItems = $_POST['items'];

// iterate through and execute single queries
foreach($postItems as $item) {

// $item should be associative array with keys (date, time, oppenent)
var_dump($item); // show $item

$datetime = new \DateTime($item['date']);
$dateFormatted = $datetime->format('m-d-Y');

// build query:
// please DO NOT insert values from POST without escaping, just for explanation!
$sql = 'INSERT INTO schedule (date, time, opponent, ...)
VALUES ("'.$dateFormatted.'", "'.$item['time'].'"
, "'. $item['opponent'].'", ...)';

echo $sql . '<br />';
}
}

?>

<html>

<body>
<form method="post">
<table>
<tr>
<td>
<p>Date<br>(m/dd/yy)</p>
</td>
<td>
<p>Time<br>(h:mm AM/PM)</p>
</td>
<td>
<p>Opponent</p>
</td>
<td>
<p>Location<br>(Rink Name)</p>
</td>
<td>
<p>Conference<br>Game?</p>
</td>
<td>
<p>Home or Away<br>Game?</p>
</td>
</tr>
<tr>
<td><input type="date" name="items[0][date]"></td>
<td><input type="time" name="items[0][time]"></td>
<td><input type="text" name="items[0][opponent]"></td>
<td><input type="text" name="items[0][location]"></td>
<td><input type="radio" name="items[0][conference]" value="+"> Conference
<br>
<input type="radio" name="items[0][conference]" value=""> Non-Conference </td>
<td><input type="radio" name="items[0][home_away]" value=" vs "> Home
<br>
<input type="radio" name="items[0][home_away]" value=" @ "> Away</td>
</tr>
<tr>
<td><input type="date" name="items[1][date]"></td>
<td><input type="time" name="items[1][time]"></td>
<td><input type="text" name="items[1][opponent]"></td>
<td><input type="text" name="items[1][location]"></td>
<td><input type="radio" name="items[1][conference]" value="+"> Conference
<br>
<input type="radio" name="items[1][conference]" value=""> Non-Conference </td>
<td><input type="radio" name="items[1][home_away]" value=" vs "> Home
<br>
<input type="radio" name="items[1][home_away]" value=" @ "> Away</td>
</tr>
</table>
<input type="submit" name="submit" value="submit" />
</form>
</body>

</html>

请注意:在构建插入查询之前请注意注释代码注释! ;)

仅供引用#1您可以试试prepared statements并将值绑定(bind)到语句。无需转义您的值来防止 SQL 注入(inject) - 准备好的语句将完成这项工作。

仅供引用 #2,请检查此问题及其接受的答案: Submitting a multidimensional array via POST with php

关于php - 使用 php 创建多个表单插入并将其输入到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44855550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com