首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP MySQL单页多表单多提交

PHP MySQL单页多表单多提交
EN

Stack Overflow用户
提问于 2012-03-07 13:38:44
回答 3查看 6.1K关注 0票数 0

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

我已经通过将count设置为会话变量来修复它,但是没有办法通过ID来做到这一点。我不确定为什么count会被擦除,所以如果有人能告诉我,我将不胜感激。

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

代码语言:javascript
运行
复制
<?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\">";
    }
    ?>`

当然,任何帮助都是非常感谢的。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-07 14:18:14

请看看这个。我在您的代码中添加了两行代码,ID正在被获取,您可以相应地更新数据。我已经在这里做了注释..

代码语言:javascript
运行
复制
<?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\">";
    }
    ?>

我希望这会对你有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2012-03-07 13:49:36

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

代码语言:javascript
运行
复制
<input type="hidden" name="id[]" value="<?=$rows['id']?>" />

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

最后,清理你的输入。永远不要将用户提交的数据直接放入查询中。对于整数使用intval,对于字符串使用mysql_real_escape_string,或者使用预准备语句。

票数 2
EN

Stack Overflow用户

发布于 2012-03-07 13:42:45

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

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

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

代码语言:javascript
运行
复制
<?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仅从末尾开始。
  2. 如果你不清理你的输入($_POST)和输出(echo),你很容易受到几种类型的攻击,比如SQL注入和XSS。因此,您始终需要在使用之前清除用户输入,并在回显之前清除输出。
  3. 要构建更新和选择查询,我们将使用implode函数以及数组($user_values (用于UPDATE,$where用于SELECT )。
  4. 我们在SELECT之前进行更新,以便输出显示为已更新。
  5. LIKE子句很难正确构建,因为用户可能希望搜索"100%",但是这个百分比字符不能与SQL命令的百分比混淆。这就是为什么我们在这一部分中有一些复杂而神秘的代码行。
  6. mysql_real_escape_stringintval函数负责清理用户输入。
  7. 我们已经使用safe_outputhtmlspecialchars创建了一个str_replace函数来清理输出。< code >H231
  8. 我们已经努力使一个完整的HTML数组到达输出阶段。
  9. 当HTML启动时,我们已经完成了所有的工作:安全更新、安全选择、过滤器都在$user_filter数组中,结果在$result资源中。我们只需要输出。
  10. 在超文本标记语言中,不能有多个元素具有相同的ID,也不需要每个表单输入都有一个ID。因此,我删除了无用的I。
  11. 我重命名了过滤器输入名称。瞧一瞧。这样,我们就可以拥有一个筛选器数组。
  12. 在这种情况下,"tabindex“属性是无用的,所以它们也被删除了。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9596379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档