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>
To summarize, there are 4 errors above:
The parameters returned by delete can only be obtained using $_GET;
delete The returned parameters must be placed in the URL, not in the body; the parameters in the body are used for query;
SQL statements must be proficient, one wrong step will lead to wrong steps;
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
Front-end JS page:
Public method, log in to the database:
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:
It should be
$userId = $_POST['rowId'];
rightA little more
Print $_GET, it should be that the parameters were not passed
You take
action
放在body里了,自然$_GET
就取不到action
了,要用$_POST
。看你都用了
DELETE
方式提交了,不如改成restful
...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?