javascript - PHP has been unsuccessful in deleting data. Please help. I beg you.
某草草
某草草 2017-05-16 13:01:04
0
8
784

After clicking the delete button, the selected data disappears from the page. It seems to have been deleted, but it will be displayed again after refreshing. After refreshing the data in the database table, the data has not been deleted. Please take a look at my data. What's wrong with the code?

The original routing action is the parameter in the url. This is the line 6 $action error in data.php. Now the error message has been modified, but the data still cannot be deleted from the database. What's going on? Woolen cloth? How should I debug code in php?

After modification:
php:

//Make a route. Action is the parameter in the url. This is line 6 $action in data.php.
    $action = $_GET['action'];

    switch($action) {
        case 'init_data_list':
            init_data_list();
            break;
        case 'add_row':
            add_row();
            break;
        case 'del_row':
            del_row();
            break;
        case 'edit_row':
            edit_row();
            break;
    }

//Delete method, how do I debug php in the browser? How can I see whether this method has been executed?
    function del_row(){
        //test
        /*echo "ok!";*/
        
        //Receive the returned parameters
        $rowId = $_GET['rowId'];
        $sql = "delete from t_users where user_id='$rowId'";
        
        if(query_sql($sql)){
            echo "ok!";
        }else{
            echo "Deletion failed!";
        }
    }

After modification:
Front page JS:

var $table = $('#table'),
    $remove = $('#remove');

$(function() {
    searchData();
    delData();
});

function delData() {
                $remove.on('click', function() {
                    if(confirm("Do you want to continue deleting")) {
                        var rows = $.map($table.bootstrapTable('getSelections'), function(row) {
                            //Return the index number of the selected row
                            return row.user_id;
                        });
                    }
                    
                    $.map($table.bootstrapTable('getSelections'),function(row){
                        var del_url = "./php/data.php";
                        //Delete data based on userId, because this id is the parameter passed to the server
                        var rowId = row.user_id;
                        
                        //According to the requirements of PHP, del_row needs to be passed back to activate the deletion method.
                        /*var dataParam = {
                            action: "del_row",
                            rowId:rowId
                        };*/
                        
                        $.ajax({
                            type:"delete",
                            url:del_url + "?action=del_row&rowId=" + rowId,
                            dataType:"html",
                            contentType: 'application/json;charset=utf-8',
                            success: function(data) {
                                $table.bootstrapTable('remove',{
                                    field: 'user_id',
                                    values: rows
                                });
                                $remove.prop('disabled', true);
                            },
                            error:function(data){
                                alert('Deletion failed!');
                            }
                        });
                    });
                })
            }

After modification:
Viewed in Headers in Network, it is displayed as: 200

Request URL:http://localhost/muke/php/data.php?action=del_row&rowId=1
Request Method:DELETE
Status Code:200 OK
Remote Address:[::1]:80

Query String Parameters
action:del_row
rowId:1

Preview in Network, the original error message is: action in data.php on line 6

There is nothing now, it is empty

View Response in Network, the original error message is: action in data.php on line 6

There is nothing now, it is empty

Using Rest Client to test, the result is 200, but the returned data is still deletion failed! It’s really killing me. I can’t figure out what’s wrong and why it can’t be deleted. Judging from the return information, the del_row() method in PHP may not be executed, right?
< br>

What should I do?

Execute the SQL statement in the database: delete from t_users where user_id='1'. The prompt is that the deletion is successful, but the execution is unsuccessful on the page. Is it because the parameters passed back are not received?
< /p>

某草草
某草草

reply all(8)
phpcn_u1582

To summarize, there are 4 errors above:

  1. The parameters returned by delete can only be obtained using $_GET;

  2. delete The returned parameters must be placed in the URL, not in the body; the parameters in the body are used for query;

  3. SQL statements must be proficient, one wrong step will lead to wrong steps;

  4. To execute a SQL statement in the database to check whether the statement is executed correctly, use Rest Client to test whether the URL request is correct;

The following is my correct code. Although it is poorly written, it can be executed without error.
php

<?php
    //测试php是否可以拿到数据库中的数据
    /*echo "44444";*/
    
    //做个路由 action为url中的参数
    $action = $_GET['action'];

    switch($action) {
        case 'init_data_list':
            init_data_list();
            break;
        case 'add_row':
            add_row();
            break;
        case 'del_row':
            del_row();
            break;
        case 'edit_row':
            edit_row();
            break;
    }

//删除方法
    function del_row(){
        //测试
        /*echo "ok!";*/
        
        //接收传回的参数
        $rowId = $_GET['rowId'];
        $sql = "delete from t_users where user_id='$rowId'";
        
        if(query_sql($sql)){
            echo "ok!";
        }else{
            echo "删除失败!";
        }
    }
?>

Front-end JS page:

var $table = $('#table'),
    $remove = $('#remove');

    $(function() {
        searchData();
        delData();
    });

function delData() {
                $remove.on('click', function() {
                    if(confirm("是否继续删除")) {
                        var rows = $.map($table.bootstrapTable('getSelections'), function(row) {
                            //返回选中的行的索引号
                            return row.user_id;
                        });
                    }
                    
                    $.map($table.bootstrapTable('getSelections'),function(row){
                        var del_url = "./php/data.php";
                        //根据userId删除数据,因为这个id就是 传给服务器的参数
                        var rowId = row.user_id;
                        
                        $.ajax({
                            type:"delete",
                            url:del_url + "?action=del_row&rowId=" + rowId,
                            dataType:"html",
                            contentType: 'application/json;charset=utf-8',
                            success: function(data) {
                                $table.bootstrapTable('remove',{
                                    field: 'user_id',
                                    values: rows
                                });
                                $remove.prop('disabled', true);
                            },
                            error:function(data){
                                alert('删除失败!');
                            }
                        });
                    });
                })
            }

Public method, log in to the database:

function query_sql(){
        $mysqli = new mysqli("127.0.0.1", "root", "root", "crud");
        $sqls = func_get_args();
        foreach($sqls as $s){
            $query = $mysqli->query($s);
        }
        $mysqli->close();
        return $query;
    }
洪涛


You submitted the request using delete method, so the backend cannot receive parameters using $_GET and $_POST.

The delete parameter should be placed in the url:

if($_SERVER['REQUEST_METHOD'] == 'DELETE') {
    //参数包含在请求url中
    $uri = $_SERVER['REQUEST_URI'];
}
迷茫

It should be $userId = $_POST['rowId']; right

"DELETE FROM `t_user` where `user_id` = '.$userId'" 

A little more

大家讲道理

Print $_GET, it should be that the parameters were not passed

过去多啦不再A梦

You take action放在body里了,自然$_GET就取不到action了,要用$_POST
看你都用了DELETE方式提交了,不如改成restful...

Ty80
$sql = "DELETE FROM `t_user` where `user_id` = '".$userId."';";

Is there a problem with the sql statement? Print it out when the program is running, copy it to the database and run the test once.

習慣沉默

First print $_GET to see what’s there?

This place of yours is very strange, try changing the dataType to json

The reason for the error is because the action parameter is not found

左手右手慢动作

What about query_sql? What to use?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template