提交动态重复表单的最佳方式

时间:2016-07-08 15:55:31

标签: php jquery html mysql forms

我正在尝试自学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);
?>

再次,表格非常粗糙所以请善待。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

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

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

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

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

否则,在尝试访问它们时会出错。