Isset expression error
Asked Answered
N

3

18

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';
      }
    }
    ?>
Natalienatalina answered 21/4, 2014 at 21:1 Comment(2)
Your code is vulnerable to SQL injections. mysql_real_escape_string does only work if the value is used in a MySQL string literal.Quinonez
There is no need to use isset() and !empty() at the same time. empty also works if the input is not defined: #4560425Burdett
B
41

You missed this ):

if(isset($_POST['delete_id']) && !empty($_POST['delete_id']))
                            ^---
Banneret answered 21/4, 2014 at 21:3 Comment(0)
C
7

Others have shown the issue of the missing ) in the expression, but empty() will check isset() so that is redundant. Just check empty():

if(!empty($_POST['delete_id'])) {
Cirenaica answered 21/4, 2014 at 21:16 Comment(0)
E
4

The issue is that this

if(isset($_POST['delete_id'] && !empty($_POST['delete_id'])))

should be

if(isset($_POST['delete_id']) && !empty($_POST['delete_id']))
Episternum answered 21/4, 2014 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.