为具有postgres数据库表的表创建删除按钮

时间:2018-09-11 16:10:12

标签: php postgresql

我正在使用的数据库是postgres。数据库连接位于config.php中,它可以工作,因为我用它来填充表。但是,我似乎无法使删除按钮正常工作。这是供内部使用的,因此我并不真正担心sql注入,因此数据库结构是在没有id作为主键的情况下构建的。因此,表中称为common_name的第一个字段是唯一的,因此我将其用作删除按钮的引用。

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

1 个答案:

答案 0 :(得分:0)

$query = 'select * from ReadMe';
sql = "DELETE FROM readme WHERE common_name='$_POST[name]'";

您使用不同的表自述文件和自述文件。可以吗?