【问题标题】:Creating delete button for table with a postgres database table使用 postgres 数据库表为表创建删除按钮
【发布时间】:2018-09-12 00:20:58
【问题描述】:

我使用的数据库是 postgres。数据库连接位于 config.php 中,它可以工作,因为我用它来填充表。但是,我似乎无法让删除按钮工作。这是供内部使用的,所以我并不担心 sql 注入,所以数据库结构是在没有 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');
?>

【问题讨论】:

  • 不确定您的自述表实际上是什么样的,但您正在发布列 'name' ( $row['name'] ) 的值并尝试根据列 ' 删除common_name'。
  • 这是不安全的 SQL,我建议学习绑定变量,它可以完全杀死大多数 sql 注入攻击,并使代码更易于阅读(IMO)
  • 表单上的示例 SQL 注入。考虑如果 $_POST['name'] = "' or '1'='1" 你的 SQL 变成 Delete from readme where common_name='' or '1'='1' 这将删除表中的每一行

标签: php postgresql


【解决方案1】:
$query = 'select * from ReadMe';
sql = "DELETE FROM readme WHERE common_name='$_POST[name]'";

您使用不同的表格自述文件和自述文件。还好吗?

【讨论】:

  • 那部分不是问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 2021-12-01
相关资源
最近更新 更多