duanao2585 2016-05-03 14:58
浏览 90
已采纳

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

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.

So I've created code that uses loops to display a html table with the contents of the specified table from a mysql database. 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>
";
// 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>
";

}
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. I would like the delete button to be after each row but I just cant get my head around it!

  • 写回答

1条回答 默认 最新

  • duangu4980 2016-05-03 15:21
    关注

    Apologies if I am overlooking something here, but have you tried wrapping your delete button with <td> tags?

    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>
    ";
    }
    

    I stripped everything bar HTML away from this and found a <td> tag around the form worked a treat.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法