首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在提交时更新多行

在提交时更新多行
EN

Stack Overflow用户
提问于 2014-01-24 03:41:13
回答 2查看 1.7K关注 0票数 1

我想在一次提交中更新表行。但我不知道该怎么做。

这是我的代码

代码语言:javascript
复制
<form  method="POST" action="<?php $_PHP_SELF ?>">
    <div class="col-md-7" style="margin-left:370px; height:auto;">
    <h3>Service-Level-Agreement</h3>
    <?php

        $query = "select * from tblsla";
        $request = mysql_query($query)or die(mysql_error());?>
        <table class="table table-hover" style="width:auto;">
        <tr>    
                <th>Category</th>
                <th>Easy</th>
                <th>Moderate</th>
                <th>Difficult</th>
                <th></th>
                </tr>
        <?php while($row = mysql_fetch_array($request)){
        ?>
        <tbody>
            <tr>
                <td><?php echo $row['category']; ?></td>

                <td><input type="text" class="form-control" style="width:50px;" name="easySla"/></td>   
                <td><input type="text" class="form-control" style="width:50px;" name="modSla"/></td>    
                <td><input type="text" class="form-control" style="width:50px;" name="difSla"/></td>    
                    <td>Days</td>
            </tr>
        </tbody>
        <?php
        }

?>  
    </table>
    <input type="submit" name="submit" id="submit" value="Edit SLA" class="btn btn-success"/>
    </div>

        <input type="hidden"  name="idUser" value="<?php echo $row['id'];?>" />
    </form>

代码语言:javascript
复制
<?php
    $easy = $_POST["easySla"];
$mod = $_POST["modSla"];
$diff = $_POST["diffSla"];


if(is_int($easy) AND is_int($mod) AND is_int($diff)){
    echo " Invalid Inputs ";
    }
else
  {
  $sql = "UPDATE tblsla SET  easySLA = '$easy', modSLA = '$mod', difSLA = '$diff' WHERE id = '$id'";
if(isset($_POST['submit'])){
$success = mysql_query($sql) or die (mysql_error());
}
if ($success == TRUE){
    ?>
        <script>
            alert('You have successfully update account.');
        </script>

    <?php
}
  }

?>

这就是我想要发生的事情的图像

太谢谢你了!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-24 03:54:19

您有几个同名输入。

在输入名称中使用数组

试着做这样的事情:

代码语言:javascript
复制
 <form  method="POST" action="<?php $_PHP_SELF ?>">
        <div class="col-md-7" style="margin-left:370px; height:auto;">
            <h3>Service-Level-Agreement</h3>
            <?php

            $query = "select * from tblsla";
            $request = mysql_query($query)or die(mysql_error());?>
            <table class="table table-hover" style="width:auto;">
                <tr>
                    <th>Category</th>
                    <th>Easy</th>
                    <th>Moderate</th>
                    <th>Difficult</th>
                    <th></th>
                </tr>
                <?php while($row = mysql_fetch_array($request)){
                    ?>
                    <tbody>
                    <tr>
                        <td><?php echo $row['category']; ?></td>

                        <td><input type="text" class="form-control" style="width:50px;" name="easySla[]"/></td>
                        <td><input type="text" class="form-control" style="width:50px;" name="modSla[]"/></td>
                        <td><input type="text" class="form-control" style="width:50px;" name="difSla[]"/></td>
 <td>  <input type="hidden"  name="idUser[]" value="<?php echo $row['id'];?>" /></td>
                        <td>Days</td>

                    </tr>
                    </tbody>
                <?php
                }

                ?>
            </table>

            <input type="submit" name="submit" id="submit" value="Edit SLA" class="btn btn-success"/>
        </div>


    </form>

在php中,有一个包含所有行的数组。您需要循环遍历这个数组并在数据库中插入数据。

就像这样:

代码语言:javascript
复制
  if (isset($_POST['submit'])) {
$count = count($_POST['easySla']); //get total number of array element
for ($i = 0; $i < $count; $i++) { // loop through array and assign values in variable and insert itin database
    $easy = $_POST['easySla'][$i];
    $mod = $_POST["modSla"][$i];
    $diff =$_POST["difSla"][$i];
    $id = $_POST["idUser"][$i];
    if (is_numeric($easy) && is_numeric($mod) && is_numeric($diff)) {

        $sql = "UPDATE tblsla SET  easySLA = '$easy', modSLA = '$mod', difSLA = '$diff' WHERE id = '$id'";
        $success = mysql_query($sql) or die (mysql_error());
        if ($success == false) {
            break;
        }
    } else {

        echo " <script>
               alert('invalid input not a number.');
               </script>";
        header("location: yourfile_name.php");
    }
}
if ($success) {
    echo " <script>
      alert('You have successfully update account.');
    </script>";
    header("location: yourfile_name.php");
} else {
    echo " <script>
    alert('You have errors.');
     </script>";
    header("location: yourfile_name.php");
}

}

如果你有什么问题请告诉我。

票数 2
EN

Stack Overflow用户

发布于 2014-01-24 03:47:30

可以使用循环更新具有不同数据或相同数据的表中的所有行.

如果对所有行都有不同的数据,则在数组中获取该数据并开始使用该数组长度进行循环,则在该循环中编写一个查询以更新每一行中的数据。

例:$array=[row1row3]用于$array{ $query=的循环,“更新表集行1=‘数据的row1'",行2=’数据行2‘."enter code here }

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21323982

复制
相关文章

相似问题

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