php - 使用 postgres 数据库表为表创建删除按钮

标签 php postgresql

我使用的数据库是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/

相关文章:

python - Matlab 检查数据库中的值

python - 如何在 PL/Python 函数中获取数据库用户名

javascript - 通过可滚动条增加谷歌地图上的圆圈半径

php - 为什么当我添加带有链接的表数据时,我的 jquery 数据表不起作用?

php - 使用 PHP 限制登录用户的内容访问

postgresql - Postgres 添加 seq# 以区分数据库中的唯一行

sql - 合并两个表并更新一个属性 WHERE

php - 在 php 和 htaccess 中上传图像的安全方法

php - 使用 jQuery 在 Codeigniter 中使用 Ajax

java - 配置 Apache Cayenne 以与 Vertx 异步方式使用