删除功能如何与ajax和php一起使用?

时间:2019-07-26 11:41:55

标签: php jquery ajax crud

我正在做一个简单的学校数据库项目作为作业。我正在学习CRUD,与此同时,尝试将其应用于我的项目。但是问题是delete按钮不起作用。

我不太确定自己在做什么错。感谢您的帮助。非常感谢。

到目前为止,我的代码是:

HTML

                    <div class="row">

                        <form method="post" id="add-lecture-form" class="col-xs-6 ajax-form" action="Controller/CourseController.php">
                            <div class="form-group">
                                <input type="text" name="title" class="form-control">
                            </div>
                            <input type="hidden" id="action" name="action" value="create">
                            <div class="form-group">
                                <input type="submit" class="btn btn-success" value="add a lecture">
                            </div>
                        </form>

                        <div class="col-xs-6">  </div>

                            <div id="lecture-result"></div>

                    </div>
                </div>

JQUERY

    $(document).ready(function () {

        function getList() {

            var data = {'action': 'getList'};
            $.ajax({
                url: 'Controller/CourseController.php',
                data: data, // action = getList muss dem Controller ,
                type: 'post',
                success: function (data) {
                    $('#lecture-result').html(data);
                }
            });
        }
        getList();


        $('#add-lecture-form').off('submit');
        $('#add-lecture-form').on('submit', function (e) {
            e.preventDefault();
            var postData = $(this).serialize();
            var url = $(this).attr('action');
            var type = $(this).attr('method');
            $.ajax({
                url: url,
                data: postData,
                type: type,
                success: function (data) {
                    $("#lecture-result").html(data);
                }
            });
        });

        $('.ajax-form').on('submit', function (e) {
            e.preventDefault();
            var postData = $(this).serialize();
            var url = $(this).attr('action');

            $.ajax({

                url: url,
                data: postData,
                type: $(this).val('method'),
                success: function (data) { //we want to see this data as result it will appeare as result

                    if (!data.error) {

                        $('#lecture-result').html(data); //data from above
                    }
                }

PHP

    namespace Database\Controller; 
    include_once '../Service/DbConfig.php';

    use Database\Service\DbConfig as DbConfig;
    use Database\Entity\Course as Course;

    $action = $_POST['action'];

    if ($action == 'create') {
        createCourse($_POST['title']);
        getCourseListAsView();

    }
    if ($action == 'delete') {
        deleteCourse();

    }

    function deleteCourse () {           

    $query = "DELETE FROM courses WHERE id";
    $result=mysql_query($query);
    if(isset($result)) {
       echo "YES";
    } else {
       echo "NO";
    }

1 个答案:

答案 0 :(得分:0)

您的DELETE查询不正确。

删除查询:

DELETE FROM table_name WHERE some_column = some_value

您需要分配ID值以删除该行。

DELETE FROM courses WHERE id = 3

id值是您要删除的行的id。