Isset表达式错误

时间:2014-04-21 21:01:43

标签: php isset

我基本上编写了一个代码,用于填充数据库中的类别列表,然后您就可以选择要删除的类别。

我遇到了删除代码的问题,由于错误而无法正常工作:

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

造成这种情况的一行是:

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

4 个答案:

答案 0 :(得分:38)

你错过了这个)

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

答案 1 :(得分:5)

其他人已经在表达式中显示了遗失)的问题,但empty()会检查isset(),因此这是多余的。只需查看empty()

即可
if(!empty($_POST['delete_id'])) {

答案 2 :(得分:3)

问题是这个

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

应该是

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

答案 3 :(得分:0)

问题是这个

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

应该是

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