gpt4 book ai didi

php - 提交动态重复表格的最佳方式

转载 作者:行者123 更新时间:2023-11-29 20:48:17 25 4
gpt4 key购买 nike

我正在尝试自学 php 和 mysql,但我在为我正在尝试组合的足球池制作的表单方面遇到了麻烦。我正在寻求有关从此表单提交数据的最佳方式的一些帮助。该表单从我的数据库获取数据并重复适当的行数。提交表单后,应将每一行插入或更新到数据库中。需要提交的数据有:userid、gameid、pick、tbPoints。

表格有点粗糙,因为我还没有完成。我似乎无法获得将每一行作为新条目提交到数据库的表单,它只提交最后一个游戏。我知道我的提交循环有问题,但我似乎不知道如何获取它。如有任何帮助,我们将不胜感激!

这是我的表格:

<?php require_once('Connections/t2016.php'); ?>
<?php
mysql_select_db($database_t2016, $t2016);
$query_gamedays = "SELECT DISTINCT schedule.weekDay, schedule.`date` FROM schedule WHERE schedule.weekNum=1";
$gamedays = mysql_query($query_gamedays, $t2016) or die(mysql_error());
$row_gamedays = mysql_fetch_assoc($gamedays);
$totalRows_gamedays = mysql_num_rows($gamedays);
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>

<body>
<form action="" method="post" name="form1">
<table border="2" cellpadding="3" cellspacing="2">
<tr align="center">
<td>Time</td>
<td colspan="3">Matchup</td>
<td>Current Pick</td>
</tr>
<?php do {
$day = $row_gamedays['weekDay'];
$date = $row_gamedays['date'];
?>
<tr wrap="nowrap">
<td colspan="5"><?php echo '<strong>'.$day.'</strong>, '.$date; ?></td>
</tr>
<?php
mysql_select_db($database_t2016, $t2016);
$query_sched = "SELECT * FROM schedule WHERE schedule.weekNum=1 AND schedule.weekDay='$day'";
$sched = mysql_query($query_sched, $t2016) or die(mysql_error());
$row_sched = mysql_fetch_assoc($sched);
$totalRows_sched = mysql_num_rows($sched);

?>
<?php do {
$gameid = $row_sched['gameID'];
$time = $row_sched['time'];
$vteam = $row_sched['visitorID'];
$hteam = $row_sched['homeID'];

mysql_select_db($database_t2016, $t2016);
$query_picks = "SELECT * FROM picks WHERE picks.gameID=$gameid AND picks.userID=1";
$picks = mysql_query($query_picks, $t2016) or die(mysql_error());
$row_picks = mysql_fetch_assoc($picks);
$totalRows_picks = mysql_num_rows($picks);

if($totalRows_picks > 0) {
$pick = $row_picks['pickID'];
$tbp = $row_picks['tiebreakerPoints'];
} else {
$pick = 'No Pick';
$tbp = '0';
}

$vp = '';
$hp = '';
if($pick == $vteam) {
$vp = 'checked';
} elseif($pick == $hteam) {
$hp = 'checked';
}
?>
<tr align="center">
<td><?php echo $time; ?></td>

<td align="right"><?php echo $vteam.'<input type="radio" name="pickID'.$gameid.'[]" value="'.$vteam.'" '.$vp.'>'; ?></td>
<td align="center"> @ </td>
<td align="left"><?php echo '<input type="radio" name="pickID'.$gameid.'[]" value="'.$hteam.'" '.$hp.'>'.$hteam; ?></td>

<td><?php echo $pick; ?></td>
</tr>
<?php if($row_sched['is_tiebreaker'] > 0) { ?>
<tr>
<td colspan="4" align="right" wrap="nowrap"><?php echo 'Enter Tiebreaker Points: <input type="number" name="tbpoints[]" min="0" value="'.$tbp.'">'; ?></td>
<td align="center"><?php echo $tbp; ?></td>
</tr>
<?php } ?>
<input type="hidden" name="gameID[]" value="$gameid">
<input type="hidden" name="userID" value="1">

<?php } while ($row_sched = mysql_fetch_assoc($sched)); ?>
<tr>
<?php } while ($row_gamedays = mysql_fetch_assoc($gamedays)); ?>
<td colspan="5" align="right" wrap="nowrap">
<input type="submit" name"submit" value="Submit Picks">
</td>
</tr>
</table>
</form>



<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($sched);

mysql_free_result($picks);

mysql_free_result($gamedays);
?>

再说一次,表格非常粗糙,所以请友善。感谢您的帮助

最佳答案

您只需像普通数组一样循环遍历这些值即可。

$gameID = $_POST['gameID'];
for($x=0; $x<count($gameID); $x++ ) { .... }

对于单选按钮/复选框 - 您需要为每个组指定一个唯一的名称。对于这些控件,唯一要记住的是,如果未设置,则不会向服务器提交任何内容。您需要检查它们是否是发布值的一部分,即

if( isset( $_POST['cbPoints'] ) ) { do something }

否则您在尝试访问它们时会收到错误。

关于php - 提交动态重复表格的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38270895/

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