简体   繁体   English

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

[英]Adding delete button to php code displaying mysql table contents

I have searched for an answer that relates to my code but I'm new to php and html so I would appreciate the help. 我已经搜索了与我的代码相关的答案,但是我是php和html的新手,所以我将非常感谢您的帮助。

So I've created code that uses loops to display a html table with the contents of the specified table from a mysql database. 因此,我创建了使用循环从mysql数据库显示带有指定表内容的html表的代码。 I would like to give the user an option to delete a row when the results are displayed. 我想给用户一个选项,在显示结果时删除一行。

This is my code so far to display the results of my database table: 到目前为止,这是我的代码,用于显示数据库表的结果:

<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);
?>

No matter where I place my form to delete button, 6 delete buttons take up an entire row followed by the actual rows. 无论我将表单放置在哪里删除按钮,6个删除按钮都会占据整行,然后是实际行。 I would like the delete button to be after each row but I just cant get my head around it! 我希望删除按钮位于每一行之后,但我无法理解它!

Apologies if I am overlooking something here, but have you tried wrapping your delete button with <td> tags? 抱歉,如果我在这里忽略了某些内容,但是您是否尝试过使用<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";
}

I stripped everything bar HTML away from this and found a <td> tag around the form worked a treat. 我从中剥离了所有HTML内容,发现表单周围的<td>标签有效。

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

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