PHP MySQL单页多表单多提交


PHP MySQL Multiple Forms and Multiple Submits on single page

我正在尝试过滤。使用表单过滤数据库,然后提供更新过滤数据的方法。我可能做得不是最好的方式,但它已经接近奏效了。问题似乎是,当我按下"更新"按钮时,一些变量被清除;计数和ID。

我通过将计数设为会话变量来固定计数,但没有办法用ID来完成。我不确定为什么计数会被删除,所以如果有人能告诉我,我将不胜感激。

我的代码的更新部分在没有过滤器部分的情况下使用时工作得很好,同样,问题是id变量是空的。我已经测试了所有其他变量,并在更新语句中为ID添加了一个常量进行测试。这是我的密码。

<?php
session_start();
?>
<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="inventory"; // Database name 
$tbl_name="computers"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form 
$school=$_POST['school'];
$make=$_POST['make'];
$model=$_POST['model'];
$barcode=$_POST['barcode'];
$location=$_POST['location'];
?>
    <?php
    include 'nav-bar.php';
    ?>
    <h2 align="center">Filter Computers</h2>
    <form name="form" method="post" style="margin: 0; text-align: center;">
    <p><label>School Name:</label><input type="text" name="school" size="8" id="school" tabindex="1"</p>
    <p><label>Make:</label><input type="text" name="make" size="25" id="make" tabindex="1"</p>
    <p><label>Model:</label><input type="text" name="model" size="25" id="model" tabindex="1"</p>
    <p><label>Barcode:</label><input type="text" name="barcode" size="12" id="barcode" tabindex="1"</p>
    <p><label>Location:</label><input type="text" name="location" size="25" id="location" tabindex="1"</p>
    <p><label>Serial:</label><input type="text" name="serial" size="25" id="location" tabindex="1"</p>
    <p><label>Date Acquired yyyy-dd-mm:</label><input type="text" name="date" size="8" id="location" tabindex="1"</p>
    <p><label>Processor:</label><input type="text" name="processor" size="25" id="location" tabindex="1"</p>
    <p><label>RAM:</label><input type="text" name="ram" size="25" id="location" tabindex="1"</p>
    <p><input align="center" type="submit" name="Filter" value="Filter">
    </form>
    <?php
    if($_POST['Filter']){
    $sql = "SELECT * FROM $tbl_name WHERE school like '%$school' AND make like '%$make' AND model like '%$model' AND location like '%$location'";
    $result=mysql_query($sql);
    // Count table rows 
    $count=mysql_num_rows($result);
    $_SESSION['count']=$count;

    ?>
    <strong>Update Multiple Computers</strong><br> 
    <table width="500" border="0" cellspacing="1" cellpadding="0">
    <form name="form1" method="post" action="">
    <tr> 
    <td>
    <table width="500" border="0" cellspacing="1" cellpadding="0">

    <tr>
    <td align="center"><strong>Id</strong></td>
    <td align="center"><strong>School</strong></td>
    <td align="center"><strong>Make</strong></td>
    <td align="center"><strong>Model</strong></td>
    <td align="center"><strong>Barcode</strong></td>
    <td align="center"><strong>Location</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
    <td align="center"><input name="school[]" type="text" id="school" value="<?php echo $rows['school']; ?>"></td>
    <td align="center"><input name="make[]" type="text" id="make" value="<?php echo $rows['make']; ?>"></td>
    <td align="center"><input name="model[]" type="text" id="model" value="<?php echo $rows['model']; ?>"></td>
    <td align="center"><input name="barcode[]" type="text" id="barcode" value="<?php echo $rows['barcode']; ?>"></td>
    <td align="center"><input name="location[]" type="text" id="location" value="<?php echo $rows['location']; ?>"></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="4" align="center"><input type="submit" name="Update" value="Update"></td>
    </tr>
    </table>
    </td>
    </tr>
    </form>
    </table>
    <?php
    }
    // Check if button name "Update" is active, do this 
    if(isset($_POST['Update'])){
    for($i=0;$i<$_SESSION['count'];$i++){
    $sql1="UPDATE $tbl_name SET school='$school[$i]', make='$make[$i]', model='$model[$i]' , barcode='$barcode[$i]' , location='$location[$i]' WHERE id='$id[$i]'";
    $result1=mysql_query($sql1);
    }
    session_destroy();
    }
    if(isset($result1)){
    echo "<meta http-equiv='"refresh'" content='"0;URL=update_multiple.php'">";
    }
    ?>`

任何帮助我们都将不胜感激。

这就是隐藏输入的作用。为您的id:添加一个

<input type="hidden" name="id[]" value="<?=$rows['id']?>" />

此外,您不需要保存计数。您可以使用count($id)计算数组中的项数。

最后,对您的输入进行消毒。永远不要将用户提交的数据直接放入查询中。对整数使用intval,对字符串使用mysql_real_escape_string,或者使用准备好的语句。

请检查一下。。。我在您的代码中添加了两行,正在提取ID,您可以相应地更新数据。我在这里作为注释发表了评论。。

<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="inventory"; // Database name 
$tbl_name="computers"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form 
$school=$_POST['school'];
$make=$_POST['make'];
$model=$_POST['model'];
$barcode=$_POST['barcode'];
$location=$_POST['location'];
//Note here
$id = $_POST['id'];
?>
    <?php
    include 'nav-bar.php';
    ?>
    <h2 align="center">Filter Computers</h2>
    <form name="form" method="post" style="margin: 0; text-align: center;">
    <p><label>School Name:</label><input type="text" name="school" size="8" id="school" tabindex="1"</p>
    <p><label>Make:</label><input type="text" name="make" size="25" id="make" tabindex="1"</p>
    <p><label>Model:</label><input type="text" name="model" size="25" id="model" tabindex="1"</p>
    <p><label>Barcode:</label><input type="text" name="barcode" size="12" id="barcode" tabindex="1"</p>
    <p><label>Location:</label><input type="text" name="location" size="25" id="location" tabindex="1"</p>
    <p><label>Serial:</label><input type="text" name="serial" size="25" id="location" tabindex="1"</p>
    <p><label>Date Acquired yyyy-dd-mm:</label><input type="text" name="date" size="8" id="location" tabindex="1"</p>
    <p><label>Processor:</label><input type="text" name="processor" size="25" id="location" tabindex="1"</p>
    <p><label>RAM:</label><input type="text" name="ram" size="25" id="location" tabindex="1"</p>
    <p><input align="center" type="submit" name="Filter" value="Filter">
    </form>
    <?php
    if($_POST['Filter']){
    $sql = "SELECT * FROM $tbl_name WHERE school like '%$school' AND make like '%$make' AND model like '%$model' AND location like '%$location'";
    $result=mysql_query($sql);
    // Count table rows 
    $count=mysql_num_rows($result);
    $_SESSION['count']=$count;

    ?>
    <strong>Update Multiple Computers</strong><br> 
    <table width="500" border="0" cellspacing="1" cellpadding="0">
    <form name="form1" method="post" action="">
    <tr> 
    <td>
    <table width="500" border="0" cellspacing="1" cellpadding="0">

    <tr>
    <td align="center"><strong>Id</strong></td>
    <td align="center"><strong>School</strong></td>
    <td align="center"><strong>Make</strong></td>
    <td align="center"><strong>Model</strong></td>
    <td align="center"><strong>Barcode</strong></td>
    <td align="center"><strong>Location</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
    <td align="center"><input name="school[]" type="text" id="school" value="<?php echo $rows['school']; ?>"></td>
    <td align="center"><input name="make[]" type="text" id="make" value="<?php echo $rows['make']; ?>"></td>
    <td align="center"><input name="model[]" type="text" id="model" value="<?php echo $rows['model']; ?>"></td>
    <td align="center"><input name="barcode[]" type="text" id="barcode" value="<?php echo $rows['barcode']; ?>"></td>
    <td align="center"><input name="location[]" type="text" id="location" value="<?php echo $rows['location']; ?>"></td>
    <!-- Note here-->
    <input type="hidden" name="id[]" id="id" value="<?php echo $rows['id']; ?>" />
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="4" align="center"><input type="submit" name="Update" value="Update"></td>
    </tr>
    </table>
    </td>
    </tr>
    </form>
    </table>
    <?php
    }
    // Check if button name "Update" is active, do this 
    if(isset($_POST['Update'])){
    for($i=0;$i<$_SESSION['count'];$i++){
    $sql1="UPDATE $tbl_name SET school='$school[$i]', make='$make[$i]', model='$model[$i]' , barcode='$barcode[$i]' , location='$location[$i]' WHERE id='$id[$i]'";
    $result1=mysql_query($sql1);
    }
    session_destroy();
    }
    if(isset($result1)){
    echo "<meta http-equiv='"refresh'" content='"0;URL=update_multiple.php'">";
    }
    ?>

我希望这对你有帮助。

你不应该让count成为会话变量,你应该有一个单一的表单-你可以有多个提交按钮。

在表单中,只提交<form></form>之间的内容。

以下是代码的改进重构版本:

<?php
// Database connection settings
$host     = 'localhost';
$username = '';
$password = '';
$db_name  = 'inventory';
$tbl_name = 'computers';
// Connect to server and select database.
mysql_connect($host, $username, $password) or die('cannot connect'); 
mysql_select_db($db_name) or die('cannot select DB');
// Perform updates
if (isset($_POST['Update']))
{
    $value_fields = array('school', 'make', 'model', 'barcode', 'location');
    foreach ((array) $_POST['id'] as $counter => $id)
    {
        $user_values = array();
        foreach($value_fields as $field_name)
        {
            if (isset($_POST[$field_name][$counter]))
            {
                $user_values[] = $field_name . ' = "' . mysql_real_escape_string($_POST[$field_name][$counter]) . '"';
            }
        }
        if (!empty($user_values))
        {
            $sql = 'UPDATE ' . $tbl_name . ' SET ' . implode(', ', $user_values) . ' WHERE id = "' . intval($id) . '"';
            mysql_query($sql);
        }
    }
}
// Build select command
$where = array('TRUE');
$filter_fields = array('school', 'make', 'model', 'barcode', 'location', 'serial', 'date', 'processor', 'ram');
if (empty($_POST['filter']))
{
    $user_filter = array_fill_keys($filter_fields, '');
}
else
{
    $user_filter = (array) $_POST['filter'];
    foreach($filter_fields as $filter_field)
    {
        if (empty($user_filter[$filter_field]))
        {
            $user_filter[$filter_field] = '';
        }
        else
        {
            $term = str_replace(array('=', '_', '%'), array('==', '=_', '=%'), $user_filter[$filter_field]);
            $term = mysql_real_escape_string($term);
            $where[] = $filter_field . ' LIKE "%' . $term . '%" ESCAPE "="';
        }
    }
}
$sql = 'SELECT id, school, make, model, barcode, location FROM ' . $tbl_name . ' WHERE ' . implode(' AND ', $where);
$result = mysql_query($sql);
// Output
function safe_output($str)
{
    return str_replace(array("'", '"'), array("&#39;", "&quot;"), htmlspecialchars($str));
}
?>
<?php include('nav-bar.php'); ?>
<form method="post">
    <h2 align="center">Filter Computers</h2>
    <div style="margin: 0; text-align: center;">
        <p><label>School Name:</label><input type="text" name="filter[school]" size="8" value="<?php echo safe_output($user_filter['school']); ?>" /></p>
        <p><label>Make:</label><input type="text" name="filter[make]" size="25" value="<?php echo safe_output($user_filter['make']); ?>" /></p>
        <p><label>Model:</label><input type="text" name="filter[model]" size="25" value="<?php echo safe_output($user_filter['model']); ?>" /></p>
        <p><label>Barcode:</label><input type="text" name="filter[barcode]" size="12" value="<?php echo safe_output($user_filter['barcode']); ?>" /></p>
        <p><label>Location:</label><input type="text" name="filter[location]" size="25" value="<?php echo safe_output($user_filter['location']); ?>" /></p>
        <p><label>Serial:</label><input type="text" name="filter[serial]" size="25" value="<?php echo safe_output($user_filter['serial']); ?>" /></p>
        <p><label>Date Acquired yyyy-dd-mm:</label><input type="text" name="filter[date]" size="8" value="<?php echo safe_output($user_filter['date']); ?>" /></p>
        <p><label>Processor:</label><input type="text" name="filter[processor]" size="25" value="<?php echo safe_output($user_filter['processor']); ?>" /></p>
        <p><label>RAM:</label><input type="text" name="filter[ram]" size="25" value="<?php echo safe_output($user_filter['ram']); ?>" /></p>
        <p><input align="center" type="submit" value="Filter">
    </div>
    <strong>Update Multiple Computers</strong><br> 
    <table width="500" border="0" cellspacing="1" cellpadding="0">
        <tr>
            <td align="center"><strong>Id</strong></td>
            <td align="center"><strong>School</strong></td>
            <td align="center"><strong>Make</strong></td>
            <td align="center"><strong>Model</strong></td>
            <td align="center"><strong>Barcode</strong></td>
            <td align="center"><strong>Location</strong></td>
        </tr>
<?php while ($row = mysql_fetch_array($result)): ?>
        <tr>
            <td align="center"><input name="id[]" type="hidden" value="<?php echo $row['id']; ?>" /><?php echo $row['id']; ?></td>
            <td align="center"><input name="school[]" type="text" value="<?php echo safe_output($row['school']); ?>" /></td>
            <td align="center"><input name="make[]" type="text" value="<?php echo safe_output($row['make']); ?>" /></td>
            <td align="center"><input name="model[]" type="text" value="<?php echo safe_output($row['model']); ?>" /></td>
            <td align="center"><input name="barcode[]" type="text" value="<?php echo safe_output($row['barcode']); ?>" /></td>
            <td align="center"><input name="location[]" type="text" value="<?php echo safe_output($row['location']); ?>" /></td>
        </tr>
<?php endwhile; ?>
        <tr>
            <td colspan="6" align="center"><input type="submit" name="Update" value="Update" /></td>
        </tr>
    </table>
</form>

这里有很多东西需要学习:

  1. 尽可能将输出或视图(HTML)与逻辑和处理代码分离。请注意,HTML仅从末尾开始。我们有尽可能少的PHP代码和HTML混合。

  2. 如果你不清理你的输入($_POST)和输出(echo),你很容易受到几种类型的攻击,比如SQL注入和XSS。因此,总是在使用之前需要清理用户输入,在回显之前需要清理输出

  3. 为了构建UPDATE和SELECT查询,我们将implode函数与数组一起使用(UDPATE的$user_values和SELECT的$where)。

  4. 我们在SELECT之前进行更新,因此输出显示为更新。

  5. LIKE子句很难正确构建,因为用户可能想搜索"100%",但这个百分比字符不能与SQL命令的百分比混淆。这就是为什么我们在这部分有一些复杂而神秘的代码行。

  6. mysql_real_escape_stringintval功能负责清理用户输入。

  7. 我们使用htmlspecialcharsstr_replace制作了一个safe_output函数来清理输出。

  8. 我们已经努力让一个完整的$user_filter数组到达输出阶段。

  9. 当HTML启动时,我们已经完成了所有操作:安全的UPDATES、安全的SELECT,过滤器在$user_filter数组中,结果在$result资源中。我们只需要输出。

  10. 在HTML中,不能有多个具有相同ID的元素,也不需要每个表单输入都有一个ID。所以,我删除了无用的ID。

  11. 我已经重命名了过滤器输入名称。看一看。这样,我们就可以拥有一组过滤器。

  12. 在这种情况下,"tabindex"属性是无用的,因此它们也被删除了。