如何纠正mysql php update中的错误?

时间:2018-05-01 09:06:54

标签: php jquery mysql

尝试在数据库表中添加,更新和删除。但使用此代码我能够添加和删除表,但表不更新,需要查找错误。

使用表单发布数据和表格以显示用户的类别。即使没有更新数据库表,update命令也没有显示结果。

 <?php
    require_once $_SERVER['DOCUMENT_ROOT'].'/thelewala/core/init.php';
    include 'includes/head.php';
    include 'includes/navigation.php';

    $sql = "SELECT * FROM categories WHERE parent = 0";
    $result = $db->query($sql);

    $errors = array();
    $category = '';
    $post_parent = '';

    // Edit Categories
    if(isset($_GET['edit']) && !empty($_GET['edit'])){
        $edit_id = (int)$_GET['edit'];
        $edit_id = sanitize($edit_id);
        $edit_sql = "SELECT * FROM  categories WHERE id = '$edit_id'";
        $edit_result = $db->query($edit_sql);
        $edit_category = mysqli_fetch_assoc($edit_result);
    }

    // delete $category
    if(isset($_GET['delete']) && !empty($_GET['delete'])){
        $delete_id = (int)$_GET['delete'];
        $delete_id = sanitize($delete_id);
    // this code is to delete parent category with their child category
    $sql = "SELECT * FROM categories WHERE id = '$delete_id'";
    $result = $db->query($sql);
    $category = mysqli_fetch_assoc($result);
    if($category['parent'] == 0){
        $sql = "DELETE FROM categories WHERE parent ='$delete_id'";
        $db->query($sql);
    }
    // otherwise this code will delete selected child category
    $dsql = "DELETE FROM categories WHERE id = '$delete_id'";
    $db->query($dsql);
    header('Location: categories.php');
    }

    // Process form //
    if(isset($_POST) && !empty($_POST)){
        $post_parent = sanitize($_POST['parent']);
        $category = sanitize($_POST['category']);
        $sqlform = "SELECT * FROM categories WHERE category ='$category' AND parent = '$post_parent'";
        if(isset($_GET['edit'])){
            $id = $edit_category['id'];
            $sqlform = "SELECT * FROM categories WHERE category ='$category' AND parent = '$post_parent' AND id != '$id'";
        }
        $fresult = $db->query($sqlform);
        $count = mysqli_num_rows($fresult);

        // if category is blank //
        if($category == ''){
            $errors[] .= 'The category cannot be blank.';
        }
        // if exists in database-->
        if($count > 0){
            $errors[] .=$category. ' already exits. Please choose anather one.';
        }
        // display errors and update database-->
        if(!empty($errors)){
        // display errors-->
        $display = display_errors($errors); ?>
        <script>
        jQuery('document').ready(function(){
            jQuery('#errors').html('<?=$display; ?>');
        });
        </script>
        <?php
        }else{
        // update database-->
        $updatesql = "INSERT INTO categories (category, parent) VALUES ('$category','$post_parent')";
        if(isset($_GET['edit'])){
            $updatesql = "UPDATE categories SET category = '$category' parent = '$post_parent' WHERE id = '$edit_id'";
        }
        $db->query($updatesql);
        header('Location: categories.php');
        }
    }

    $category_value = '';
    $parent_value = 0;
    if(isset($_GET['edit'])){
        $category_value = $edit_category['category'];
        $parent_value = $edit_category['parent'];
    }else{
        if(isset($_POST)){
            $category_value = $category;
            $parent_value = $post_parent;
        }
    }
    ?>
    <h2 class="text-center">Categories</h2>

    <!-- form to add catagories-->
    <div class="row">
      <div class="col-md-6">
      <legend><?=((isset($_GET['edit']))?'Edit':'Add A');?> Catagory</legend>
        <div id="errors"></div>
        <form class="form" action="categories.php<?=((isset($_GET['edit']))?'?edit='.$edit_id:'');?>" method="post">
            <div class="form-group">
                <label for="parent">Parent</label>
                <select class="form-group" name="parent" id="parent">
                    <option value="0"<?=(($parent_value ==0)?' selected="selected"':'');?>>Parent</option>
                    <?php while($parent = mysqli_fetch_assoc($result)): ?>
                    <option value="<?=$parent['id']; ?>"<?=(($parent_value == $parent['id'])?' selected="selected"':'');?>><?=$parent['category']; ?></option>
                    <?php endwhile; ?>
                </select>
            </div>
            <div class="form-group">
                <label for="category">Catagory</label>
                <input type="text" class="form-control" id="category" name="category" value="<?=$category_value;?>">
                <input type="submit" value="<?=((isset($_GET['edit']))?'Edit':'Add');?> Category" class="btn btn-success">
            </div>
            <div class="form-group">

            </div>
        </form>
      </div>
      <!--table for categories chart-->
      <div class="col-md-6">
        <table class="table table-sm table-bordered">
          <thead>
            <th>Catagory</th><th>Parent</th><th></th>
          </thead>
          <tbody>
          <?php
          $sql = "SELECT * FROM categories WHERE parent = 0";
            $result = $db->query($sql);

          while($parent = mysqli_fetch_assoc($result)):
          $parent_id = (int)$parent['id'];
          $sql2 = "SELECT * FROM categories WHERE parent = '$parent_id'";
          $cresult = $db->query($sql2);
          ?>
            <tr class="bg-primary">
              <td><?=$parent['category'];?></td>
              <td>Parent</td>
              <td>
                <a href="categories.php?edit=<?=$parent['id'];?>" class="btn btn-xs btn-secondary"><span class="glyphicon glyphicon-pencil"></span>Edit</a>
                <a href="categories.php?delete=<?=$parent['id'];?>" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-remove-sign"></span>Delete</a>
              </td>
            </tr>
            <?php while($child = mysqli_fetch_assoc($cresult)): ?>
            <tr class="table-info">
              <td><?=$child['category'];?></td>
              <td><?=$parent['category'];?></td>
              <td>
                <a href="categories.php?edit=<?=$child['id'];?>" class="btn btn-xs btn-secondary"><span class="glyphicon glyphicon-pencil"></span>Edit</a>
                <a href="categories.php?delete=<?=$child['id'];?>" class="btn btn-xs btn-danger"><span class="glyphicon glyphicon-remove-sign"></span>Delete</a>
              </td>
            </tr>

            <?php endwhile; ?>
            <?php endwhile; ?>
          </tbody>
        </table>
      </div>
    </div>
    <?php
    include 'includes/footer.php';
    ?>

1 个答案:

答案 0 :(得分:1)

SQL查询中存在语法错误。

$updatesql = "UPDATE categories SET category = '$category', parent = '$post_parent' WHERE id = '$edit_id'";

您只需在'$ category'之后添加逗号。