简体   繁体   English

如何使用php编辑和保存表中的行

[英]how to edit and save rows in a table using php

I have a page that displays all of the contents of my table. 我有一个页面显示表的所有内容。 Alongisde each row of the table I also have a column containing a checkbox. 沿着表的每一行,我还有一列包含一个复选框。 I have implemented this for deleting rows. 我已经实现了删除行。 If in case i want to edit and save changes in the table how am i supposed to deal with?Below is the code which i have implemented for delete. 如果要在表中编辑和保存更改,该如何处理?下面是我为删除实现的代码。

<?php
// Connect to server and select databse.
mysql_connect("localhost", "root", "")or die("cannot connect"); 
mysql_select_db("project")or die("cannot select DB");

$sql="SELECT * FROM menu";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Vendor_id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Item_id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Item_name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Price</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['item_id']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['vendor_id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['item_id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['item_name']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['Price']; ?></td>
</tr>

<?php
}
?>

<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

<?php

// Check if delete button active, start this 
if(isset($_POST['delete'])){
$checkbox = $_POST['checkbox'];
for($i=0;$i<count($_POST['checkbox']);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM menu WHERE item_id='$del_id'";
$result = mysql_query($sql);}

if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_menu.php\">";}}
mysql_close();
?>
</table></form></td></tr></table>

first of all use PDO instead of mysql... 首先使用PDO代替mysql ...

and for your answer .. 和你的答案..

there are many ways to do this... but the simplest that i can think of is.. creating an edit button/(link) in each table rows. 有很多方法可以做到这一点...但是我能想到的最简单的方法是..在每个表行中创建一个编辑按钮/(链接)。 this sends the id of the data to be edited to a php page where u can do the editand saving it to the database table part... 这会将要编辑的数据的ID发送到php页面,您可以在其中进行编辑并将其保存到数据库表部分...

for an example... 例如...

i will add this to your table.. 我将其添加到您的表中。

HTML 的HTML

<td bgcolor="#FFFFFF"><a href="saveEdit.php?id=".<?php echo $rows['vendor_id']; ?>EDIT</a></td>

this will take u to the saveEdit.php page with an id... 这将带您进入带有id的saveEdit.php页面。

u can get the id value in saveEdit.php by 您可以通过以下方式在saveEdit.php中获取ID值:

$vendor_id=$_GET['id'];

u have an id now...create a form...get related data of that vendor from the table and show it in the form...let user edit it...and save it to the database table..like u did, to delete the vendor.. 您现在有一个ID ...创建一个表格...从表格中获取该供应商的相关数据并在表格中显示...让用户对其进行编辑...并将其保存到数据库表格中。删除了供应商。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM