I have basically coded a code which populates a list of categories from my database then you are able to select which to delete.
I have the issue with the delete code which does not seem to work due to the error:
Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in F:\xamppnew\htdocs\650032\admin\delete.php on line 6
The line causing this is:
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
deletecategory.php
<h3>
Delete Category
</h3>
<?php $result = mysql_query("SELECT * FROM category"); ?>
<table>
<?php while($row = mysql_fetch_array($result)) : ?>
<tr id="<?php echo $row['category_id']; ?>">
<td><?php echo $row['category_Name']; ?></td>
<td>
<button class="del_btn" rel="<?php echo $row['id']; ?>">Delete</button>
</td>
</tr>
<?php endwhile; ?>
</table>
<script>
$(document).ready(function(){
$('.del_btn').click(function(){
var del_id = $(this).attr('rel');
$.post('delete.php', {delete_id:del_id}, function(data) {
if(data == 'true') {
$('#'+del_id).remove();
} else {
alert('Could not delete!');
}
});
});
});
</script>
delete.php
<?php
if(isset($_POST['delete_id'] && !empty($_POST['delete_id']))) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$result = mysql_query("DELETE FROM category WHERE `id`=".$delete_id);
if($result !== false) {
echo 'true';
}
}
?>
Definition and Usage The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.
The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases.
The equivalent of isset($var) for a function return value is func() === null . isset basically does a !== null comparison, without throwing an error if the tested variable does not exist.
Answer: Use the PHP isset() function You can use the PHP isset() function to test whether a variable is set or not. The isset() will return FALSE if testing a variable that has been set to NULL .
You missed this )
:
if(isset($_POST['delete_id']) && !empty($_POST['delete_id']))
^---
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With