2016-06-21 22 views
0

「空」 MySQL的領域我有顯示,只要沒有在相應的mysql表提供數據的形式,見下圖:展形式

if(mysql_num_rows($records)==0)) {...} 

一旦在數據可用各表我顯示一個單獨的表,其基本上包含相同的信息爲「空」的表中,但包括從MySQL表取出的值:如何合併這兩個表:

while($result = mysql_fetch_assoc($records)) {...} 

問題1合併爲一個,這樣如果沒有數據可用如果數據被添加到mysql數據庫中,該值將顯示在相應的字段中?

問題2:有沒有關於如何簡化代碼的建議,因爲會有更多的行(隊)被添加到表格中?

代碼:

<?php 
$sql = "SELECT * FROM results WHERE user = '" 
    .$_SESSION['username']."'"; 
$records = mysql_query($sql); 
if(mysql_num_rows($records)==0) { 
    ?> 
    <form action="xxxxx.php" method="post"> 
     <table> 
      <tr> 
       <th width="30%">Home</th> 
       <th width="40%"></th> 
       <th width="30%">Away</th> 
      </tr> 
      <tr> 
       <td><label>Team 1</label></td> 
       <td> 
        <input type="tel" size="1" maxlength="2" 
          name="team1game1"> 
        : 
        <input type="tel" size="1" maxlength="2" 
          name="team2game1"> 
       </td> 
       <td><label>Team 2</label></td> 
      </tr> 
      <tr> 
       <td><label>Team 3</label></td> 
       <td> 
        <input type="tel" size="1" maxlength="2" 
          name="team3game1"> 
        : 
        <input type="tel" size="1" maxlength="2" 
          name="team4game1"> 
       </td> 
       <td><label>Team 4</label></td> 
      </tr> 
     </table> 
     <table> 
      <tr><td><input type="submit" value="Save"></td></tr> 
     </table> 
    </form> 
    <?php 
} 
else { 
    while($result = mysql_fetch_assoc($records)) { 
     ?> 
     <form action="xxxxx.php" method="post"> 
      <table> 
       <tr> 
        <th width="30%">Home</th> 
        <th width="40%"></th> 
        <th width="30%">Away</th> 
       </tr> 
       <tr> 
        <td><label>Team 1</label></td> 
        <td> 
         <input type="tel" size="1" maxlength="2" 
           name="team1game1" 
           value="<?php echo $result['team1game1'] ?>"> 
         : 
         <input type="tel" size="1" maxlength="2" 
           name="team2game1" 
           value="<?php echo $result['team2game1'] ?>"> 
        </td> 
        <td><label>Team 2</label></td> 
       </tr> 
       <tr> 
        <td><label>Team 3</label></td> 
        <td> 
         <input type="tel" size="1" maxlength="2" 
           name="team3game1" 
           value="<?php echo $result['team3game1'] ?>"> 
         : 
         <input type="tel" size="1" maxlength="2" 
           name="team4game1" 
           value="<?php echo $result['team4game1'] ?>"> 
        </td> 
        <td><label>Team 4</label></td> 
       </tr> 
      </table> 
      <table> 
       <tr><td><input type="submit" value="Save"></td></tr> 
      </table> 
     </form> 
     <?php 
    } 
} 
?> 
+0

我注意到,即使沒有數據,你也會顯示出1-4隊輸入的2行。你還提到你會有很多行。問題1:如果你有100個團隊但沒有數據,你會顯示50行空輸入嗎?問題2:當你有數據時,爲每個記錄創建一個新表。所以50個記錄就意味着50個表格。這是故意的嗎? – BeetleJuice

+0

它看起來像數據庫中的表中每個團隊有1列。它可以改變爲你連續存儲團隊的數據嗎? – DBA

+0

@帕特里克我想我必須要更精確一點。我目前正在開發一款博彩遊戲。上面張貼的表格屬於用戶可以輸入他們對每個比賽/比賽的猜測的頁面。現在我不想有兩個不同的頁面 - 一個是用戶還沒有輸入任何猜測/投注(僅插入),一個用於顯示他們進入他們的猜測/投注(更新)。現在有點更清楚了嗎? – mario

回答

0

這不是很清楚,我你問什麼。也許你可以提供一個應該是/應該更好理解的例子。

然而,這裏的至少一個稍微優化代碼的版本:

<?php 
// Always escape values received from the user before using them 
// in SQL queries 
$user = mysql_real_escape_string($_SESSION['username']); 

$template = ' 
<form action="xxxxx.php" method="post"> 
    <table> 
     <tr> 
      <th width="30%">Home</th> 
      <th width="40%"></th> 
      <th width="30%">Away</th> 
     </tr> 
     <tr> 
      <td><label>Team 1</label></td> 
      <td><input type="tel" size="1" maxlength="2" name="team1game1" value="%team1game1%"> : <input type="tel" size="1" maxlength="2" name="team2game1" value="%team2game1%"></td> 
      <td><label>Team 2</label></td> 
     </tr> 
     <tr> 
      <td><label>Team 3</label></td> 
      <td><input type="tel" size="1" maxlength="2" name="team3game1" value="%team3game1%"> : <input type="tel" size="1" maxlength="2" name="team4game1" value="%team4game1%"></td> 
      <td><label>Team 4</label></td> 
     </tr> 
    </table> 
    <table> 
     <tr><td><input type="submit" value="Save"></td></tr> 
    </table> 
</form>'; 

$sql = "SELECT * FROM results WHERE user = '".$user."'"; 
$records = mysql_query($sql); 

$html = ''; 
if (mysql_num_rows($records)) { 
    while ($row = mysql_fetch_assoc($records)) { 
     $html .= $template; 
     $html = str_replace('%team1game1%', $row['team1game1'], $html); 
     $html = str_replace('%team2game1%', $row['team2game1'], $html); 
     $html = str_replace('%team3game1%', $row['team3game1'], $html); 
     $html = str_replace('%team4game1%', $row['team4game1'], $html); 
    } 
} else { 
    $html .= $template; 
    $html = str_replace('%team1game1%', '', $html); 
    $html = str_replace('%team2game1%', '', $html); 
    $html = str_replace('%team3game1%', '', $html); 
    $html = str_replace('%team4game1%', '', $html); 
} 

// Print built HTML 
echo $html; 
?> 

作用:

  • 存儲在變量$模板的表單模板(%VAR%)的佔位符
  • 在每次運行時替換str_replace函數(所有變量(VAR%%))

小號ome tips:

  • 使用PDO或至少MySQLi。 mysql _ *()函數自年齡以來已被棄用,並在當前的PHP版本中被刪除。依賴它們會導致兼容性和安全問題。
  • 不使用ORM時,總是會將您在從用戶接收到的數據放入SQL查詢之前轉義您的值。否則,您將很容易受到SQL注入的影響。