我使用的数据库是postgres。数据库连接位于 config.php 中,它可以工作,因为我用它来填充表。但是,我似乎无法使用删除按钮。这是供内部使用,所以我不太担心 sql 注入(inject),所以数据库结构是在没有 id 作为主键的情况下构建的。所以表中名为 common_name 的第一个字段是唯一的,所以我将其用作删除按钮的引用。
自述文件-p.php:
<?php
//the following php code is for displaying the table contents on the same page
include 'config.php';
$query = 'select * from ReadMe';
$result = pg_query($query);
$i = 0;
// code for creating a table structure using css
echo '<html><body><style>
table, td, th {
border: 0.5px solid #D96B27;
text-align: left;
}
th, td {
padding: 10px;
} </style><table><tr>';
//fetching the column names of the db table
while ($i < pg_num_fields($result))
{
$fieldName = pg_field_name($result, $i);
echo '<th>' . $fieldName . '</th>';
$i = $i + 1;
}
echo '</tr>';
$i = 0;
//fetching and displaying the contents of the db table
while ($row = pg_fetch_row($result))
{
echo "<tr>";
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
// Adds the Edit and Delete buttons to every row
echo "<form action=\"readme-p-delete.php?name=" . $row['name'] . "\" method=\"post\">";
echo "<input type='hidden' name='name' value=" . $row['name'] .">";
echo "<td><input type=\"submit\" style='color:#0090C1;' value=\"Edit\"></td>";
echo "<td><input type=\"submit\" name=\"submit\" value=\"Delete\" style='color:#E63462;'></form></td></tr>";
$i = $i + 1;
}
pg_free_result($result);
echo '</table></body></html>';
//Delete button function
?>
readme-p-delete.php:
<?php
include 'config.php';
sql = "DELETE FROM readme WHERE common_name='$_POST[name]'";
$result = pg_query($sql);
pg_free_result($result);
header('location: readme-p.php');
?>
最佳答案
$query = 'select * from ReadMe';
sql = "DELETE FROM readme WHERE common_name='$_POST[name]'";
您使用不同的表自述文件和自述文件。可以吗?
关于php - 使用 postgres 数据库表为表创建删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52280106/