Giả sử tôi có một bảng danh mục và sau đó tôi có bảng sản phẩm.categories table and then I have products table.
Mỗi sản phẩm thuộc về một số loại.
Bây giờ, tôi muốn xóa một danh mục được gán cho một số sản phẩm.
Điều này sẽ gây ra lỗi, vì tôi không thể xóa một cái gì đó khỏi cơ sở dữ liệu vì quan hệ cơ sở dữ liệu. Mã của tôi bên dưới sẽ loại bỏ một hàng khỏi bảng [sử dụng jQuery] nhưng nó sẽ loại bỏ danh mục thực sự chỉ khi không có sản phẩm nào sử dụng nó.
Tôi đang cố gắng tạo một số cảnh báo lỗi khi tôi nhấp vào biểu tượng Delete
và không thể xóa danh mục. Làm thế nào tôi có thể làm điều đó? Tôi muốn một số cửa sổ bật lên jQuery với cảnh báo, nhưng tôi không thực sự biết tôi nên kiểm tra lỗi ở nơi đầu tiên như thế nào.
EDIT: Tôi đã cố gắng sử dụng echo 1;
và echo 0;
dưới dạng response
cho
0 nhưng tôi nhận được thông báo cảnh báo mỗi khi tôi muốn xóa một cái gì đó, ngay cả khi không có lỗi. I have tried to use $[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
echo 1;
and echo 0;
as response
for
$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
0 but I am getting the warning message every time I want to remove something, even if there is no error.
Đây là kịch bản:
$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
Và truy vấn SQL:
$id = $_POST['id'];
$query = "DELETE FROM games_category WHERE id =".$id;
if[mysqli_query[$conn, $query]]{
echo 1;
}else{
echo 0;
}
Các câu lệnh có thể được thực thi với mysqli :: query [], mysqli :: real_query [] và mysqli :: multi_query []. Hàm MySQLI :: Query [] là phổ biến nhất và kết hợp câu lệnh thực thi với một phần tìm nạp được đệm của tập kết quả của nó, nếu có, trong một cuộc gọi. Gọi mysqli :: query [] giống hệt với gọi mysqli :: real_query [] theo sau là mysqli :: store_result [].mysqli::query[], mysqli::real_query[] and mysqli::multi_query[]. The mysqli::query[] function is the most common, and combines the executing statement with a buffered fetch of its result set, if any, in one call. Calling mysqli::query[] is identical to calling mysqli::real_query[] followed by mysqli::store_result[].
Ví dụ #1 Truy vấn thực thi
$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
1$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
2$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
3Bộ kết quả đệm
Sau khi thực thi câu lệnh, kết quả có thể được truy xuất tất cả cùng một lúc hoặc đọc theo hàng từ máy chủ. Bộ đệm tập kết quả phía máy khách cho phép máy chủ miễn phí các tài nguyên liên quan đến kết quả của câu lệnh càng sớm càng tốt. Nói chung, khách hàng là bộ kết quả tiêu thụ chậm. Do đó, nên sử dụng các bộ kết quả đệm. MySQLI :: Query [] kết hợp thực thi câu lệnh và bộ đệm đặt kết quả.mysqli::query[] combines statement execution and result set buffering.
Các ứng dụng PHP có thể điều hướng tự do thông qua kết quả đệm. Điều hướng nhanh vì các bộ kết quả được giữ trong bộ nhớ máy khách. Xin lưu ý rằng việc khách hàng thường dễ dàng hơn để mở rộng quy mô của máy chủ.
Ví dụ #2 Điều hướng thông qua kết quả được đệm
$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
1$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
2$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
6Ví dụ trên sẽ xuất ra:
Reverse order... id = 3 id = 2 id = 1 Result set order... id = 1 id = 2 id = 3
Bộ kết quả không bị ảnh hưởng
Nếu bộ nhớ máy khách là tài nguyên ngắn và giải phóng tài nguyên máy chủ càng sớm càng tốt để không cần tải máy chủ thấp, có thể sử dụng kết quả không bị ảnh hưởng. Cuộn qua các kết quả không có kết quả là không thể trước khi tất cả các hàng đã được đọc.
Ví dụ #3 Điều hướng thông qua kết quả không bị ảnh hưởng
$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
1$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
8$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
9Kết quả đặt các loại dữ liệu giá trị
Mysqli :: query [], mysqli :: real_query [] và mysqli :: multi_query [] Các hàm được sử dụng để thực hiện các câu lệnh không chuẩn bị. Ở cấp độ của giao thức máy khách MySQL, lệnh
$id = $_POST['id'];
$query = "DELETE FROM games_category WHERE id =".$id;
if[mysqli_query[$conn, $query]]{
echo 1;
}else{
echo 0;
}
0 và giao thức văn bản được sử dụng để thực thi câu lệnh. Với giao thức văn bản, máy chủ MySQL chuyển đổi tất cả dữ liệu của kết quả được đặt thành chuỗi trước khi gửi. Chuyển đổi này được thực hiện bất kể kiểu dữ liệu cột SQL kết quả. Thư viện máy khách MySQL nhận được tất cả các giá trị cột dưới dạng chuỗi. Không có sự đúc phía máy khách nào được thực hiện để chuyển đổi các cột trở lại loại gốc của chúng. Thay vào đó, tất cả các giá trị được cung cấp dưới dạng chuỗi PHP.mysqli::query[], mysqli::real_query[] and
mysqli::multi_query[] functions are used to execute non-prepared statements. At the level of the MySQL Client Server Protocol, the command $id = $_POST['id'];
$query = "DELETE FROM games_category WHERE id =".$id;
if[mysqli_query[$conn, $query]]{
echo 1;
}else{
echo 0;
}
0 and the text protocol are used for statement execution. With the text protocol, the MySQL server converts all data of a result sets into strings before sending. This conversion is done regardless of the SQL result set column data type. The mysql client
libraries receive all column values as strings. No further client-side casting is done to convert columns back to their native types. Instead, all values are provided as PHP strings. Ví dụ #4 Giao thức văn bản trả về chuỗi theo mặc định
$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
1$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
2$id = $_POST['id'];
$query = "DELETE FROM games_category WHERE id =".$id;
if[mysqli_query[$conn, $query]]{
echo 1;
}else{
echo 0;
}
3Ví dụ trên sẽ xuất ra:
id = 1 [string] label = a [string]
Bộ kết quả không bị ảnh hưởng
4 connection
option, if using the mysqlnd library. If set, the mysqlnd library will check the result set meta data column types and convert numeric SQL columns to PHP numbers, if the PHP data type value range allows for it. This way, for example, SQL INT columns are returned as integers. $id = $_POST['id'];
$query = "DELETE FROM games_category WHERE id =".$id;
if[mysqli_query[$conn, $query]]{
echo 1;
}else{
echo 0;
}
Nếu bộ nhớ máy khách là tài nguyên ngắn và giải phóng tài nguyên máy chủ càng sớm càng tốt để không cần tải máy chủ thấp, có thể sử dụng kết quả không bị ảnh hưởng. Cuộn qua các kết quả không có kết quả là không thể trước khi tất cả các hàng đã được đọc.
$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
1$[document].ready[function[]{
// Delete
$['.delete_game_cat'].click[function[]{
var el = this;
var id = this.id;
var splitid = id.split["_"];
// Delete id
var deleteid = splitid[1];
// AJAX Request
$.ajax[{
url: 'delete_game_cat.php',
type: 'POST',
data: { id:deleteid },
success: function[response]{
if[response===1]{
// Removing row from HTML Table
$[el].closest['tr'].css['background','tomato'];
$[el].closest['tr'].fadeOut[600, function[]{
$[this].remove[];
}];
}else{
alert["This category cannot be deleted!"];
}
}
}];
}];
}];
2$id = $_POST['id'];
$query = "DELETE FROM games_category WHERE id =".$id;
if[mysqli_query[$conn, $query]]{
echo 1;
}else{
echo 0;
}
7Ví dụ trên sẽ xuất ra:
id = 1 [integer] label = a [string]
Bộ kết quả không bị ảnh hưởng
- mysqli::__construct[]
- mysqli::options[]
- mysqli::real_connect[]
- mysqli::query[]
- mysqli::multi_query[]
- mysqli::use_result[]
- mysqli::store_result[]
Nếu bộ nhớ máy khách là tài nguyên ngắn và giải phóng tài nguyên máy chủ càng sớm càng tốt để không cần tải máy chủ thấp, có thể sử dụng kết quả không bị ảnh hưởng. Cuộn qua các kết quả không có kết quả là không thể trước khi tất cả các hàng đã được đọc.
Làm thế nào để bạn kiểm tra xem truy vấn SQL có được thực thi trong PHP không?
$id = $_POST['id'];
$query = "DELETE FROM games_category WHERE id =".$id;
if[mysqli_query[$conn, $query]]{
echo 1;
}else{
echo 0;
}
0 và giao thức văn bản được sử dụng để thực thi câu lệnh. Với giao thức văn bản, máy chủ MySQL chuyển đổi tất cả dữ liệu của kết quả được đặt thành chuỗi trước khi gửi. Chuyển đổi này được thực hiện bất kể kiểu dữ liệu cột SQL kết quả. Thư viện máy khách MySQL nhận được tất cả các giá trị cột dưới dạng chuỗi. Không có sự đúc phía máy khách nào được thực hiện để chuyển đổi các cột trở lại loại gốc của chúng. Thay vào đó, tất cả các giá trị được cung cấp dưới dạng chuỗi PHP.