php php如何在表格中添加编辑按钮并加载数据以编辑表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10830028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php how to add edit button inside a table and load data to edit form
提问by greenpool
what i am trying to do:loading data from mysql database in a table with edit buttons.
我想要做什么:从带有编辑按钮的表中加载 mysql 数据库中的数据。
what is going wrong:when clicking on edit buttons it doesn't load the data on the edit form.
出了什么问题:单击编辑按钮时,它不会加载编辑表单上的数据。
what i have tried:i tried the following code at home on Firefox and it worked perfectly. but at work it fails to load the data in Firefox in the edit form and in IE clicking the button does nothing.
我尝试过的方法:我在家中在 Firefox 上尝试了以下代码,效果很好。但在工作中,它无法在 Firefox 中以编辑形式加载数据,并且在 IE 中单击该按钮没有任何作用。
<?php
echo '<table id="viewAll">';
echo '<tr>';
echo '<th>Product</th>';
echo '<th>Status</th>';
echo '<th>Summary</th>';
echo '<th>Created</th>';
echo '<th>Updated</th>';
echo '<th>Edit</th>';
echo '</tr>';
while ($data=mysqli_fetch_array($result)){
echo '<tr align="center">
<td>'.$data['product'].'</td>
<td>'.$data['status'].'</td>
<td align="left">'.$data['summary'].'</td>
<td>'.$data['created'].'</td>
<td >'.$data['updated'].'</td>';
echo "<td><a href=\"editForm.php?id=$data[id]\" style=\"text-decoration: none\"><input type=\"submit\" value=\"Edit\" /></a></td>";
echo '</tr>';
}#end of while
}#end of if
echo '</table>';
?>
and then i do:
然后我做:
$id=$_GET['id'];
$query="SELECT * FROM tickets WHERE id='$id'";
$result = mysqli_query($db,$query) or die( "My query ($query) generated an error: ".mysql_error());
$data=mysqli_fetch_array($result);
mysqli_close($db);
I use the contents in the data array to load it into the field in the edit form..
我使用数据数组中的内容将其加载到编辑表单中的字段中。
unfortunately this doesn't work in IE. clicking edit button doesn't do anything. Can somebody please tell me how to fix it? Thanks.
不幸的是,这在 IE 中不起作用。单击编辑按钮不执行任何操作。有人可以告诉我如何解决吗?谢谢。
回答by Tepken Vannkorn
You should not use <input type="submit" value="edit" name="edit" />.
你不应该使用<input type="submit" value="edit" name="edit" />.
Only "<a href='editForm.php?id='".$data['id']."' style='text-decoration: none' />Edit</a>"would be enough in sending you the data to the editForm.phppage.
只要"<a href='editForm.php?id='".$data['id']."' style='text-decoration: none' />Edit</a>"将数据发送到editForm.php页面就足够了。
PS: submit button can only be used to submit all form elements within the form.
PS:提交按钮只能用于提交表单内的所有表单元素。
回答by u?nb??s
Try using this:
尝试使用这个:
echo "<td><form action=\"editForm.php\" method=\"get\"><input type=\"hidden\" name=\"id\" value=\"$data['id']\" style=\"text-decoration: none\" /><input type=\"submit\" value=\"Edit\" /></form></td>";
That will mean you can still use a button.
这意味着您仍然可以使用按钮。
Also, I strongly suggest if you are not using tables for tabular data, find an alternative, such as <div>s.
另外,我强烈建议如果您不使用表格来存储表格数据,请寻找替代方法,例如<div>s。
And remember to sanitise your SQL queries, or you may be susceptible to SQL injection attacks.
(e.g. $id=$_GET['id'];=> $id=mysqli_real_escape_string($conn, $_GET['id']);where $connis your MySQL connection object).
并记住清理您的 SQL 查询,否则您可能容易受到 SQL 注入攻击。(例如$id=$_GET['id'];=>你的 MySQL 连接对象$id=mysqli_real_escape_string($conn, $_GET['id']);在哪里$conn)。
You can search for any of these things on Google.
您可以在Google上搜索任何这些内容。
回答by j_freyre
Correct the line where you write the button into this way
以这种方式更正您将按钮写入的行
echo "<td><a href=\"editForm.php?id=$data[id]\" style=\"text-decoration: none\">Edit</a></td>";
Clicking the button try to submit a form. But what you want to do is access the page editForm.php
单击按钮尝试提交表单。但是你想要做的是访问页面editForm.php
PS: You should read some tutorials about PHP and html to learn best practice ;)
PS:你应该阅读一些关于 PHP 和 html 的教程来学习最佳实践;)
回答by jbrtrnd
In the while loop in the first PHP script, the line which echoes the edit button is wrong.
You have to use the concatenate function .and quotes to get the value in $data["id"]
Your line echo "<td><a href=\"editForm.php?id=$data[id]\" style=\"text-decoration: none\">Edit</a></td>";has to be replaced by :
在第一个 PHP 脚本的 while 循环中,与编辑按钮相呼应的行是错误的。
您必须使用连接函数.和引号来获取$data["id"]
您的行中的值echo "<td><a href=\"editForm.php?id=$data[id]\" style=\"text-decoration: none\">Edit</a></td>";必须替换为:
echo "<td><a href=\"editForm.php?id=".$data['id']."\" style=\"text-decoration: none\">Edit</a></td>";
echo "<td><a href=\"editForm.php?id=".$data['id']."\" style=\"text-decoration: none\">Edit</a></td>";

