【问题标题】:how does delete function work with ajax and php? [closed]delete 函数如何与 ajax 和 php 一起使用? [关闭]
【发布时间】:2019-07-26 11:41:55
【问题描述】:

我正在做一个简单的学校数据库项目作为家庭作业。我正在学习 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";
    }

【问题讨论】:

  • 你有半个查询:“DELETE FROM courses WHERE id”,你需要指定删除什么ID,例如"从 id = 1 的课程中​​删除"
  • 请参考此链接codewithawa.com/posts/…

标签: php jquery ajax crud


【解决方案1】:

您的 DELETE 查询不正确。

删除查询:

DELETE FROM table_name WHERE some_column = some_value

您需要为删除该行分配 id 值。

DELETE FROM courses WHERE id = 3

id 值是要删除的行的id。

【讨论】:

  • 在他的代码中看不到他对 id 的引用,最好详细说明他此时如何访问行的 id代码。
猜你喜欢
  • 1970-01-01
  • 2015-08-06
  • 1970-01-01
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 2011-03-21
  • 2012-11-20
  • 1970-01-01
相关资源
最近更新 更多