我已经搜索了一个与我的代码相关的答案,但我是php和html的新手,所以我很感激您的帮助。
因此,我创建了一段代码,它使用循环来显示一个html表,其中包含mysql数据库中指定表的内容。我想给用户一个选项,在显示结果时删除一行。
这是我迄今为止显示数据库表结果的代码:
<html><head><title>MySQL Table Viewer</title></head><body>
<?php
$db_host = 'localhost';
$db_user = 'username';
$db_pwd = 'password';
$database = 'dvdproject';
$table = 'Employee';
$con= mysql_connect($db_host,$db_user,$db_pwd,$database);
if(!$con){
die("Can not connect" . mysql_error());
}
mysql_select_db($database,$con);
// sending query
$result = mysql_query("SELECT * FROM {$table}");
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>'n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
foreach($row as $cell)
echo "<td>$cell</td>";
echo '<form method="POST" name="deleterequest" action =
"deleterequest.php">';
echo "<input name='record_id' type='hidden' value='".$row['id']."'
>";
echo "<input name='delete'type='submit' value='Delete' >";
echo "</form>";
echo "</tr>'n";
}
echo "</table>";
mysql_close($con);
?>
无论我把表单的"删除"按钮放在哪里,6个删除按钮都会占用整行,然后是实际的行。我希望删除按钮在每一行之后,但我就是无法理解!
如果我忽略了这里的内容,我很抱歉,但您是否尝试过用<td>
标记包装删除按钮?
foreach($row as $cell)
echo "<td>$cell</td>";
echo "<td>";
echo '<form method="POST" name="deleterequest" action="deleterequest.php">';
echo "<input name='record_id' type='hidden' value='".$row['id']."'>";
echo "<input name='delete'type='submit' value='Delete' >";
echo "</form>";
echo "</td>";
echo "</tr>'n";
}
我去掉了除HTML之外的所有内容,发现表单周围的<td>
标签很有用。