如何更新表中的行?

时间:2014-09-08 18:39:52

标签: php mysql-workbench

我正在尝试更新和删除表格中的行。我的代码有效,但我注意到只有最新的行可以更新和删除。这些代码有什么问题? : - \

这是我的代码:

 <?php
 /*code for displaying database tbl_category to html table*/
$con=mysqli_connect("localhost","root","admin","learning_assessment");
$sql="Select * from tbl_category";
$rs=mysqli_query($con,$sql);
$rows=mysqli_fetch_array($rs); 

?>
<fieldset class="fieldset590">
<fieldset class="fieldset1" style="background-color:gray">
<fieldset><legend class="legend">Question Category</legend>
<form action="category_.php" method="post">
<input class="inputbox" type="text" name="category" id="category" />
<input type="submit" value="Save" class="submitbtns" /></fieldset></form>
<table border="1">
<tr>
<th width="500px"><font color="#6b0c36">QUESTION CATEGORIES</font></th>
<th width="300px"></th>
<th width="300px"></th>
</tr>
<?php do { ?>
<tr><form action="AdminCategory.php" method="post">
<td><input class="inputboxlargest" type="text" name="category" value="<?php echo $rows['category']; ?>" /></td>
<input type="hidden" name="idtbl_category" value="<?php echo $rows['idtbl_category'];?>"  />
<td><input class="submitbtns" type="submit" name="update" value="UPDATE" /></td>
<td><input class="submitbtns" type="submit" name="delete" value="DELETE" /></td>
</tr>
<?php } while($rows=mysqli_fetch_array($rs));?>
</table><br /><br /><Br /><br /><br />

<br /><br />
</fieldset>
</fieldset>

</th>
</tr>
</table>
</body>
</html>
<?php
mysqli_free_result($rs);
?>

<?php
if(isset($_POST['delete'])){
$deletequery="delete from tbl_category where idtbl_category='$_POST[idtbl_category]'";
$deleters=mysqli_query($con,$deletequery);

}

if(isset($_POST['update'])){
$updatequery="update tbl_category set category='$_POST[category]' where idtbl_category='$_POST[idtbl_category]'";
$updaters=mysqli_query($con,$updatequery);
}
?>

谢谢!!!!

1 个答案:

答案 0 :(得分:0)

那是因为你提交了一份表格。一次只能提交一份表格。您应该将所有category输入放在一个表单中,并创建一个category值数组。之后,您可以在提交之前使用javascript自定义表单上的action标记。这不是'美丽'的方式,但它有效。

您的HTML / javascript代码:

 <?php
 /*code for displaying database tbl_category to html table*/
$con=mysqli_connect("localhost","root","admin","learning_assessment");
$sql="Select * from tbl_category";
$rs=mysqli_query($con,$sql);
$rows=mysqli_fetch_array($rs); 

?>
<fieldset class="fieldset590">
<fieldset class="fieldset1" style="background-color:gray">
<fieldset><legend class="legend">Question Category</legend>
<form action="category_.php" method="post">
<input class="inputbox" type="text" name="category" id="category" />
<input type="submit" value="Save" class="submitbtns" /></fieldset></form>
<table border="1">
<tr>
<th width="500px"><font color="#6b0c36">QUESTION CATEGORIES</font></th>
<th width="300px"></th>
<th width="300px"></th>
</tr>
<form action="AdminCategory.php" id="formAdminCategory" method="post">
<?php do { ?>
<tr>
<td><input class="inputboxlargest" type="text" name="category[<?php echo $rows['idtbl_category'];?>]" value="<?php echo $rows['category']; ?>" /></td>
<td><input class="submitbtns" type="submit" name="update" idtbl="<?php echo $rows['idtbl_category'];?>" value="UPDATE" /></td>
<td><input class="submitbtns" type="submit" name="delete" idtbl="<?php echo $rows['idtbl_category'];?>" value="DELETE" /></td>
</tr>
<?php } while($rows=mysqli_fetch_array($rs));?>
</table></form><br /><br /><Br /><br /><br />

<br /><br />
</fieldset>
</fieldset>

</th>
</tr>
</table>

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
    $(document).on('click', 'input[name="update"], input[name="delete"]', function(e)
    {
        e.preventDefault();
        $('#formAdminCategory').attr('action', 'AdminCategory.php?action='+$(this).attr('name')+'&id='+$(this).attr('idtbl'));
        $('#formAdminCategory').submit();

    })
</script>

</body>
</html>
<?php
mysqli_free_result($rs);
?>

您可以通过阅读$_GET参数action来了解已提交的操作。如果是update,您可以遍历category数组:

<?php
if(isset($_GET['action']) && $_GET['action'] == 'delete'){
$deletequery="delete from tbl_category where idtbl_category=".$_GET['id'];
$deleters=mysqli_query($con,$deletequery);

}

if(isset($_GET['action']) && $_GET['action'] == 'update'){

$categories = $_POST['category'];
foreach($categories as $id=>$category)
{
    $updatequery="update tbl_category set category='".$category."' where idtbl_category=".$id;
    $updaters=mysqli_query($con,$updatequery);

}

}

另请注意,在显示表单之前,您应该执行updatedelete操作。否则,您需要在提交表单后刷新页面以显示更改的数据。