PHP / SQL从表中删除数据

时间:2017-05-23 16:28:22

标签: php mysql sql

我目前正在努力处理我的代码。我想从我的表中删除某些行,但我无法弄清楚出了什么问题。

这是我删除函数的.php:

<?php 
$connect = mysqli_connect("localhost", "root", "", "produktai") or die (mysql_error());
mysqli_select_db($connect,'dazai');
if (isset($_GET['recordID']))
{
    $id = $_GET['recordID'];
    $query = "DELETE FROM dazai WHERE id = '$id'";
    header("refresh:0; url=Dazai.php");
}
else
{
    echo "Not Delete";
}
?>

那是我的主要.php:

<?php
session_start();
if(!isset($_SESSION['uid']))
{
   header("Location:signup.php");
}
$connect = mysqli_connect("localhost", "root", "", "produktai");
$query = "SELECT * FROM dazai ORDER BY id ASC";
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0) 
{
    while($row = mysqli_fetch_array($result))
    {
?>
  <tr>
    <td><?php echo $row['id'];?></td>
    <td><?php echo $row['pavad'];?></td>
    <td><?php echo $row['Gamintojas'];?></td>
    <td><?php echo $row['Spalva'];?></td>
    <td><?php echo $row['Kiekis'];?></td>
    <td><?php echo $row['Blizgumas'];?></td>
    <td><?php echo $row['Kaina'];?>€</td>
    <td><?php echo $row['Kategorija'];?></td>
    <td><?php echo $row['sandely'];?></td>
    <td><a href="delete.php?recordID=<?php echo $row['id'];?>">X</a>
  </tr>
<?php
    }
}
?>

3 个答案:

答案 0 :(得分:1)

$query = "DELETE FROM dazai WHERE id = '$id'";
$result = mysqli_query($connect, $query);

答案 1 :(得分:1)

你可能不想要这个......

 mysqli_select_db($connect,'dazai');

因为它正在更改您连接的数据库。

答案 2 :(得分:0)

您错过了运行查询并连接到delete.php文件中的bd

试试这个:

$connect = mysqli_connect("localhost", "root", "", "produktai");

if (isset($_GET['recordID'])) {
$id = $_GET['recordID'];

$query = "DELETE FROM dazai WHERE id = $id";
mysqli_query($connect, $query);

header ("refresh:0; url=Dazai.php");
}

else {
echo "Not Delete";
}
?>