用于删除表条目的按钮

时间:2017-12-11 20:10:31

标签: php html mysql mysqli

我想要一个按钮来删除它旁边的表项。但我很快就会出现。我似乎在输入错误是我的尝试:

<td><a href="delete-quote.php?id=<?php echo $row['id'];?>"><button class="btn-danger-dark">Delete</button></a></td>

但它似乎不起作用。我只收到ELSE错误消息

我尝试了一些小改动,但没有任何影响。我很确定它的东西很小,我不知道。

我的delete-quote.php代码:

<?php

$id = $_GET['id'];
//Connect DB
// on success delete : redirect the page to original page
$dbname = "siyakhat_ibroker";
$conn = mysqli_connect("localhost", "siyakhat_root", "*****", $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// sql to delete a record
$sql = "DELETE FROM prerequest WHERE id = $id"; 

if (mysqli_query($conn, $sql)) {
mysqli_close($conn);
header('Location: manage-quotes.php'); 
exit;
} else {
echo "Error deleting record";
}
?>

更新

我的dbconnect.php

<?php

$mysql_hostname ="localhost";
$mysql_user ="siyakhat_root";
$mysql_password ="****";
$mysql_database ="siyakhat_ibroker";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");

?>

3 个答案:

答案 0 :(得分:2)

您是否尝试过创建数据库连接?

dbCon.php

define('HOST','localhost');
define('USER','root');
define('PASS','your_pass_here');
define('DB','siyakhat_ibroker');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect to the DB');

那么你的delete-quote.php应该是:

 include(dbCon.php);
 $id = $_GET['id'];
 $action = $_GET['action'];
 $tableName = 'prerequest';
if($action=='delete'){
    //deleting row
    $sql = "DELETE FROM $tableName WHERE id='$id'";
    $r = mysqli_query($con,$sql);

    if($r){
        $status="success";
    //assuming this file is in your root folder
    header('Location: manage-quotes.php');
    }else{
        $status="error ".mysqli_error($con) . ": " . mysqli_error($con);
    }
    echo $status;

}mysqli_close($con);

然后用:delete-quote.php?id = 1&amp; action = delete

调用它

我建议您使用Ajax调用之类的动态用法,而不是按钮中的静态用法,方法是使用jQuery创建onClick事件,并使用ajax调用动态删除行。

希望它有效。 干杯

答案 1 :(得分:1)

从delete-quote.php替换“$ sql”,如下所示:

$sql = "DELETE FROM prerequest WHERE id = '".$id."'"; 

答案 2 :(得分:0)

只需使用此解决方案

<?php

 $servername = "localhost";
 $username = "siyakhat_root";
 $password = "";
 $dbname = "siyakhat_ibroker";


 $id = isset($_GET['id']) ? $_GET['id'] : '';


 // Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
 if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
  }

  // sql to delete a record
    $sql = "DELETE FROM prerequest WHERE id = $id";

   if ($conn->query($sql)) {
       mysqli_close($conn);
       header('Location: manage-quotes.php');
      exit;
    } else {
        echo "Error deleting record";
    }
  ?>