PHP / MYSQL更新功能不起作用

时间:2017-04-02 21:51:35

标签: php mysql ajax database

我创建了一个包含项目列表和管理面板的数据库。虽然我可以添加删除表格数据,但我无法更新表格数据。

似乎edit_item功能似乎不起作用,我无法发现原因。

档案:item_list.php

<?php include "../includes/db.php"; 
if(isset($_POST['item_submit'])){
    $item_title = mysqli_real_escape_string($conn, strip_tags($_POST['item_title']));
    $item_description = mysqli_real_escape_string($conn, $_POST['item_description']);
    $item_category = mysqli_real_escape_string($conn, strip_tags($_POST['item_category']));
    $item_quantity = mysqli_real_escape_string($conn, strip_tags($_POST['item_quantity']));
    $item_cost = mysqli_real_escape_string($conn, strip_tags($_POST['item_cost']));
    $item_price = mysqli_real_escape_string($conn, strip_tags($_POST['item_price']));
    $item_discount = mysqli_real_escape_string($conn, strip_tags($_POST['item_discount']));
    $item_delivery = mysqli_real_escape_string($conn, strip_tags($_POST['item_delivery']));

    if(isset($_FILES['item_image']['name'])){
        $file_name = $_FILES['item_image']['name'];
        $path_address = "../images/products/$file_name";
        $path_address_db = "images/products/$file_name";
        $img_confirm = 1;
        $file_type = pathinfo($_FILES['item_image']['name'], PATHINFO_EXTENSION);

    if($_FILES['item_image']['size'] > 2000000){
        $img_confirm = 0;
        echo 'The file size is too large';
            }
        if($file_type != 'jpg' && $file_type != 'png' && $file_type != 'gif'){
            $img_confirm=0;
            echo 'The image type is not elligible';
        }
        if($img_confirm == 0){
            alert('Mission Failed');
        }
        else{
            if(move_uploaded_file($_FILES['item_image']['tmp_name'], $path_address)){
                $item_ins_sql = "INSERT INTO items (item_image, item_title, item_description, item_cat, item_quantity, item_cost, item_price, item_discount, item_delivery) VALUES ('$path_address_db', '$item_title', '$item_description', '$item_category', '$item_quantity', '$item_cost', '$item_price', '$item_discount', '$item_delivery')";
                $item_ins_run = mysqli_query($conn, $item_ins_sql);

            }
        }
    }

}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Online Shopping - Admin Panel</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src = "../js/jquery.js"></script>
        <script src = "../js/bootstrap.js"></script>

        <script>
            function get_item_list_data(){
                xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function(){
                    if(xmlhttp.readyState = 4 && xmlhttp.status == 200 ){

                        document.getElementById('get_item_list_data').innerHTML = xmlhttp.responseText;

                    }
                }

                xmlhttp.open('GET', 'item_list_process.php', true);
                xmlhttp.send(); 
            }
            function del_item(item_id){
                 xmlhttp = new XMLHttpRequest();
                xmlhttp.onreadystatechange = function(){
                    if(xmlhttp.readyState = 4 && xmlhttp.status == 200 ){

                        document.getElementById('get_item_list_data').innerHTML = xmlhttp.responseText;

                    }
                }
                xmlhttp.open('GET', 'item_list_process.php?del_item_id='+item_id, true);
                xmlhttp.send();
            }

            function edit_item(){

                item_id = document.getElementById('up_item_id').value;
                item_title = document.getElementById('item_title').value;
                item_description = document.getElementById('item_description').value;
                item_category = document.getElementById('item_category').value;
                item_quantity = document.getElementById('item_quantity').value;
                item_cost = document.getElementById('item_cost').value;
                item_price = document.getElementById('item_price').value;
                item_discount = document.getElementById('item_discount').value;
                item_delivery = document.getElementById('item_delivery').value;

                xmlhttp.open('GET', 'item_list_process.php?up_item_id='+item_id+'&item_title='+item_title+'&item_description='+item_description+'&item_category='+item_category+'&item_quantity='+item_quantity+'&item_cost='+item_cost+'&item_price='+item_price+'&item_discount='+item_discount+'&item_delivery='+item_delivery, true);
                xmlhttp.send();
            }
        </script>

    </head>
    <body onload = "get_item_list_data();">
        <?php include "includes/header.php"; ?>

        <div class="container">
        <button class="btn btn-danger" data-toggle="modal" data-backdrop="static" data-keyboard="false" data-target="#add_new_item">Add New Item</button>
            <div id="add_new_item" class="modal fade">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Add New Item</h4>
                        </div>
                        <div class="modal-body">
                            <form method="post" enctype="multipart/form-data">
                                <div class="form-group">
                                    <label>Item Image</label>
                                    <input type="file" name="item_image" class="form-control" required>
                                </div>
                                <div class="form-group">
                                    <label>Item Title</label>
                                    <input type="text" name="item_title" class="form-control" required>
                                </div>
                                <div class="form-group">
                                    <label>Item Description</label>
                                    <textarea name="item_description" class="form-control" required></textarea>
                                </div>
                                <div class="form-group">
                                    <label>Item Category</label>
                                    <select class="form-control" name="item_category" required>
                                        <option>Select Category</option>
                                        <?php 
                                        $cat_sql = "SELECT * FROM item_cat";
                                        $cat_run = mysqli_query($conn, $cat_sql);
                                        while($cat_rows = mysqli_fetch_assoc($cat_run)){
                                            $cat_name = ucwords($cat_rows['cat_name']);
                                            if($cat_rows['cat_slug'] == ''){
                                                $cat_slug = $cat_rows['cat_name'];
                                            }
                                            else{
                                                $cat_slug = $cat_rows['cat_slug'];
                                            }
                                            echo"
                                            <option value='$cat_slug'>$cat_name</option>
                                            ";

                                        }
                                        ?>

                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>Item Quantity</label>
                                    <input type="number" name="item_quantity" class="form-control" required>
                                </div>
                                <div class="form-group">
                                    <label>Item Cost</label>
                                    <input type="number" name="item_cost" class="form-control" required>
                                </div>
                                <div class="form-group">
                                    <label>Item Price</label>
                                    <input type="number" name="item_price" class="form-control" required>
                                </div>
                                <div class="form-group">
                                    <label>Item Discount</label>
                                    <input type="number" name="item_discount" class="form-control" required>
                                </div>
                                <div class="form-group">
                                    <label>Item Delivery</label>
                                    <input type="number" name="item_delivery" class="form-control">
                                </div>
                                <div class="form-group">
                                    <input type="submit" name="item_submit" class="btn btn-primary btn-block">
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button class="btn btn-danger" data-dismiss="modal">Close</button>
                        </div>

                    </div>
                </div>
            </div>
            <br><br>
            <div id="get_item_list_data">
            <!-- Item List Area -->
            </div>

        </div>
        <br>
        <hr/>
        <br>
        <?php include "includes/footer.php"; ?>
    </body>

</html>

文件:item_list_process.php

<?php include "../includes/db.php";

if(isset($_REQUEST['del_item_id'])){
    $del_sql = "DELETE FROM items WHERE item_id = '$_REQUEST[del_item_id]'";
    mysqli_query($conn, $del_sql);
}
 if(isset($_REQUEST['up_item_id'])){
    $item_title = mysqli_real_escape_string($conn, strip_tags($_REQUEST['item_title']));
    $item_description = mysqli_real_escape_string($conn, $_REQUEST['item_description']);
    $item_category = mysqli_real_escape_string($conn, strip_tags($_REQUEST['item_category']));
    $item_quantity = mysqli_real_escape_string($conn, strip_tags($_REQUEST['item_quantity']));
    $item_cost = mysqli_real_escape_string($conn, strip_tags($_REQUEST['item_cost']));
    $item_price = mysqli_real_escape_string($conn, strip_tags($_REQUEST['item_price']));
    $item_discount = mysqli_real_escape_string($conn, strip_tags($_REQUEST['item_discount']));
    $item_delivery = mysqli_real_escape_string($conn, strip_tags($_REQUEST['item_delivery']));
    $item_id = $_REQUEST['up_item_id'];


    $item_up_sql = "UPDATE items SET item_title='$item_title', item_description='$item_description', item_category='$item_category', item_quantity='$item_quantity', item_cost='$item_cost', item_price='$item_price', item_discount='$item_discount', item_delivery='$item_delivery' WHERE item_id = '$item_id' ";
    $item_up_run = mysqli_query($conn, $item_up_sql);     
}
?>

<table class="table table-bordered table-striped">
                <thead>
                    <tr>
                    <th>Serial No.</th>
                    <th>Item Image</th>
                    <th>Item Title</th>
                    <th>Item Description</th>
                    <th>Item Category</th>
                    <th>Item Qty</th>
                    <th>Item Cost</th>
                    <th>Item Price</th>
                    <th>Item Discount</th>
                    <th>Item Delivery</th>
                    <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $c = 1;
                    $sel_sql = "SELECT * FROM items";
                    $sel_run = mysqli_query($conn, $sel_sql);
                    while($rows = mysqli_fetch_assoc($sel_run)){
                        $discounted_price = $rows['item_price'] - $rows['item_discount'];
                        echo"
                        <tr>
                        <td>$c</td>
                        <td><img src = '../$rows[item_image]' style = 'width:60px'</td>
                        <td>$rows[item_title]</td>
                        <td>$rows[item_description]</td>
                        <td>$rows[item_cat]</td>
                        <td>$rows[item_quantity]</td>
                        <td>$rows[item_cost]</td>
                        <td>$rows[item_price]</td>
                        <td>$discounted_price ($rows[item_discount])</td>
                        <td>$rows[item_delivery]</td>
                        <td>
                            <div class='dropdown'>
                                <button class='btn btn-danger dropdown-toggle' data-toggle='dropdown'>
                                Actions<span class='caret'></span>
                                </button>
                                <ul class='dropdown-menu dropdown-menu-right'>"; ?>
                                    <li>
                                        <a href="#edit_modal_<?php echo $rows['item_id']; ?>" data-toggle='modal'>Edit</a>


                                    </li>
                    <li><a href="javascript:;" onclick="del_item(<?php echo $rows['item_id']; ?>);">Delete</a></li>

    </ul>
</div>

<div class='modal fade' id="edit_modal_<?php echo $rows['item_id']; ?>">
                                        <div class='modal-dialog'>
                                        <div class='modal-content'>
                                        <div class='modal-header'>
                            <button class='close' data-dismiss='modal'>&times;</button>
                            <h4 class='modal-title'>Edit Item</h4>
                        </div>
                                        <div class='modal-body'>
                                        <div id='form1'>

                                <div class='form-group'>
                                    <label>Item Title</label>
                                    <input type='text' id='item_title' value='<?php echo $rows['item_title']; ?>' class='form-control' required>
                                </div>
                                <div class='form-group'>
                                    <label>Item Description</label>
                                    <textarea id='item_description' value='<?php echo $rows['item_description']; ?>' class='form-control' required></textarea>
                                </div>
                                <div class='form-group'>
                                    <label>Item Category</label>
                                    <select class='form-control' id='item_category'  required>
                                        <option>Select Category</option><?php

                                        $cat_sql = "SELECT * FROM item_cat";
                                        $cat_run = mysqli_query($conn, $cat_sql);
                                        while($cat_rows = mysqli_fetch_assoc($cat_run)){
                                            $cat_name = ucwords($cat_rows['cat_name']);
                                            if($cat_rows['cat_slug'] == ''){
                                                $cat_slug = $cat_rows['cat_name'];
                                            }
                                            else{
                                                $cat_slug = $cat_rows['cat_slug'];
                                            }
                                            echo "
                                            <option value='$cat_slug'>$cat_name</option>
                                            ";

                                        }

                                        ?>
                                    </select>
                                </div>
                                <div class='form-group'>
                                    <label>Item Quantity</label>
                                    <input type='number' id='item_quantity' value='<?php echo $rows['item_quantity']; ?>' class='form-control' required>
                                </div>
                                <div class='form-group'>
                                    <label>Item Cost</label>
                                    <input type='number' id='item_cost' value='<?php echo $rows['item_cost']; ?>' class='form-control' required>
                                </div>
                                <div class='form-group'>
                                    <label>Item Price</label>
                                    <input type='number' id='item_price' value='<?php echo $rows['item_price']; ?>' class='form-control' required>
                                </div>
                                <div class='form-group'>
                                    <label>Item Discount</label>
                                    <input type='number' id='item_discount' value='<?php echo $rows['item_discount']; ?>' class='form-control' required>
                                </div>
                                <div class='form-group'>
                                    <label>Item Delivery</label>
                                    <input type='number' id='item_delivery' value='<?php echo $rows['item_delivery']; ?>' class='form-control'>
                                </div>
                                <div class='form-group'>
                                <input type='hidden' id='up_item_id' value='<?php echo $rows['item_id']; ?>'>
                    <button onclick="edit_item()" class='btn btn-success btn-block'>Submit</button>
                                </div>
                            </div>
                                        </div>
                                        <div class='modal-footer'>
                            <button class='btn btn-danger' data-dismiss='modal'>Close</button>
                        </div>
                                        </div>
                                        </div>

                        </td>
                    </tr><?php 

                        $c++;
                    }
                    ?>

                </tbody>
            </table>

0 个答案:

没有答案