将删除按钮添加到显示mysql表内容的php代码中

时间:2016-05-03 14:58:36

标签: php html mysql database

我已经搜索了与我的代码相关的答案,但我是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个删除按钮占用整行,然后是实际行。 我希望删除按钮位于每行之后,但我无法理解它!

1 个答案:

答案 0 :(得分:0)

如果我在这里忽略某些内容,我是否道歉,但您是否尝试使用scoped package标记包装删除按钮?

<td>

我删除了所有条形HTML,并在表单周围找到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"; } 标记。