2016-07-08 82 views
0

我想教自己的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); 
?> 

再次,形式是真正的平靜,所以請善待。感謝您的幫助

+0

每個html輸入都需要定義爲一個數組,以便php可以使用多個值,即name ='pickID'to name ='pickID []' – jeff

+0

是Jeff,對不起,我忘了添加這些內容,他們原來並且已經改變了我的代碼,所以名稱現在是:pickid。$ gameid。[],gameID []和tbpoints [] – OldDogTeachingHimselfNewTricks

+0

我只是無法弄清楚循環,並從表單中獲取每個值。我遇到的最麻煩的部分是廣播組,如果我只使用pickID [],那麼每個廣播組將作爲一個組,並且我只能提交pickID值,因此通過將gameid添加到它,它使每個廣播組都是獨一無二的,可以讓他們正常工作 – OldDogTeachingHimselfNewTricks

回答

0

您只需像普通數組一樣遍歷這些值。

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

至於單選按鈕/複選框 - 你需要給每個組的唯一名稱。這些控件唯一需要記住的是,如果沒有設置,那麼沒有任何內容被提交給服務器。你需要檢查,如果他們的崗位值即

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

的一部分,否則,你會得到錯誤,試圖訪問他們的時候。