简体   繁体   中英

Button to delete table entry

i would like a button to delete the table entry next to it. But I am coming up short. I seem to be typing something wrong here is my attempt:

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

but it doesn't seem to work. I only get the ELSE error message

I have tried small changes but with no affect. Im pretty sure its something small that I am missing.

my delete-quote.php code:

<?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";
}
?>

UPDATE

my 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");

?>

did you tried creating a db connection?

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

then your delete-quote.php should looks like:

 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);

then call it with: delete-quote.php?id=1&action=delete

I recommend you to use a dynamic usage like Ajax calls instead an static in your button, by creating your onClick events using jQuery and remove rows dynamically with ajax calls.

Hope it works. Cheers

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

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

Just use this solution

<?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";
    }
  ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM