I am unable to delete my data from a database through PHP code, though I can from phpmyadmin. I have attached both of my PHP and HTML. I tried to use the same code which is used by phpmyadmin, but there is no progress. Even with this, I am not getting alert message which I use to debug and there is no outcome.
<?php
$conn2=mysqli_connect("localhost","root","") or die(mysql_error());
mysqli_Select_db($conn2,"editor") or die("connot connect to the database");
if (isset ($_GET ['delid'] ) ) {
$deluser=$_GET['delid'];
$alertMessage = "<div class='alert alert-danger'>
<p> Are you sure you want to delete this record?</p><br>
<form action='".htmlspecialchars($_SERVER['PHP_SELF']). " ?id=$deluser' method='post'>
<input type='submit' class='btn btn-danger' id='con_del' name='con_del' value='Yes' delete!>
<a href='location.. /listNB.php' class='close' data-dismiss='alert' aria-label='close'>X</a>
</form>
</div>
";
}
if (isset($_GET['con_del']))
{
$entry_id= $row["entry_id"];
$sql= "DELETE FROM `editornb` WHERE `editornb`.`entry_id` = '".$entry_id."'";
$que= mysqli_query($conn2,$sql);
if ($que) {
print'<script> alert("Sucessfully deleted!!!");</script>';
} else{
print'<script> alert("error");</script>';
}
}
?>
!DOCTYPE html>
<html>
<head>
<title> CK EDITOR </title>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="fontawesome/fontawesome-free-5.0.6/web-fonts-with-css/css/fontawesome.min.css">
<script src="ckeditor/ckeditor/ckeditor.js" type="text/javascript" ></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
</head>
<body>
<?php
if (isset($alertMessage))
echo $alertMessage;
?>
<br>
<a class="btn btn-success" href="textareaNB.php"> Home </a>
<a class="btn btn-success" href="ListNB.php">List</a>
<br><br>
<table class="table table-striped table-bordered">
<tr>
<th> ID</th>
<th> DATE</th>
<th> Content</th>
<th> Update</th>
<th> Delete</th>
</tr>
<?php
session_start();
$user=($_SESSION['u_uid']);
$query="SELECT * FROM `editornb` where user_uid='".$user."'";
$result= mysqli_query($conn2, $query);
$result_check= mysqli_num_rows ($result);
$date=date("M/d/y");
if ( $result_check > 0 )
{
while ($row= mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>" .$row["entry_id"]."</td>";
echo "<td>" .$row["date"]. "</td>";
echo "<td>" .$row["content"]."</td>";
echo '<td><a href="update.php?upid='.$row['entry_id'].'" type="button" class="btn btn-primary btn-sm">
<span class="fa fa-edit"></span> </a></td>';
echo '<td><a href="ListNB.php?delid='.$row['entry_id'].'" type="button" class="btn btn-danger btn-sm">
<span class="fa fa-trash"></span> </a></td>';
echo"</tr>";
}
}
?>
</table>
</booy>
</html>
</div>
mysqli_Select_db
typo ?
should be: mysqli_select_db
Remove htmlspecialchars()
in htmlspecialchars($_SERVER['PHP_SELF'])
Just do
$_SERVER['PHP_SELF']
You are assigning $entry_id
the wrong value;
From:
$entry_id= $row["entry_id"];
To:
$entry_id= $_GET["id"];
In your second PHP file: Remove the existing session_start();
and move it at the very top of that file. You can't output anything before sending headers. It will cause error.
<?php session_start(); ?> <-- Move it there like that. No whitespace before "<?php"
!DOCTYPE html>
<html>
<head>
<title> CK EDITOR </title>
Also you have an extra space character in your <form>
action attribute. In your <form>
tag, the action=""
attribute, Change:
" ?id=$deluser'
To:
"?id=$deluser'
After making all changes as suggested by @Karlo Kokkak change
$entry_id= $_POST["id"];
To
$entry_id= $_POST["con_del"];
That should work fine. If you echo the query check if correct id is passing to it or not.
OKay guys i found the solution of my own question... what is did was i tried to fetch the emtry number which i was trying to delete. by using the same query that i did for fetching the data for my table. whcih is select * from tablename.
than using mysqli_fetch_assoc i retrived my data used the $row varibale. i works just fine for my program, hope it helps someone,
though i did used @KarloKokkak's and @prasannaputtaswamy suggestion
if (isset($_POST['del']))
{
$user=($_SESSION['u_uid']);
$query="SELECT * FROM `editornb` where user_uid='".$user."'";
$result= mysqli_query($conn2, $query);
$result_check= mysqli_num_rows ($result);
$row= mysqli_fetch_assoc($result);
$id =($row['entry_id']);
$user=($_SESSION['u_uid']);
$sql= "DELETE FROM editornb WHERE user_uid='$user' AND entry_id= '$id' ";
$answer= mysqli_query($conn2,$sql);
if ($answer == TRUE ) {
print'<script> alert("Sucessfully deleted!!!");</script>';
} else{
print'<script> alert("error");</script>';
}
}
</div>