如何纠正mysql php更新中的错误?

t3irkdon  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(336)

正在尝试在数据库表中添加、更新和删除。但是用这个代码我可以添加和删除表,但是表没有更新,需要查找错误。
使用窗体发布数据,并使用表为用户显示类别。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';
    ?>
jdgnovmf

jdgnovmf1#

sql查询中有语法错误。

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

你只需要在“$category”后面加一个逗号。

相关问题