时间:2016-06-21 20:16:28

标签: php 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
    }
}
?>

1 个答案:

答案 0 :(得分: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;
?>

它的作用:

  • 将表单模板存储在$ template中,并使用变量(%var%)作为占位符
  • 在每次运行中用str_replace()替换所有变量(%var%)

一些提示:

  • 使用PDO或至少使用MySQLi。 mysql _ *()函数自古以来就被弃用,并在当前的PHP版本中被删除。依赖它们会导致兼容性和安全性问题。
  • 当不使用ORM时,在将它们放入SQL查询之前,始终转义从用户收到的值。否则,您将容易受到SQL注入攻击。