我想在一个提交中更新表行。但我不知道该怎么做。
这是我的代码
<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>
PHP
<?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
}
}
?>
这是我想要发生的图像
非常感谢你!
答案 0 :(得分:2)
你有几个同名的输入。
在输入名称中使用数组
尝试这样的事情:
<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中你有一个包含所有行的数组。您需要遍历此数组并在数据库中插入数据。
试试这样:
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");
}
}
如果您有任何疑问,请与我们联系。
答案 1 :(得分:0)
您可以使用循环更新表中具有不同数据或相同数据的所有行...
如果所有行都有不同的数据,则在数组中获取该数据并开始使用该数组长度循环,在该循环中编写查询以更新每行中的数据。
Ex:$ array = [[row1] [row2] [row3]]
for length of length $ array {
$ query =“update table set row1 ='row1'的数据”,row2 ='row2的数据'...“enter code here
}