如何从 jquery 选择的数据库行中删除
how to delete from database row selected with jquery
我有以下代码:
文件lecturer.php:
$(document).ready(function(){
$(".use-address").click(function() {
var $row = $(this).closest("tr"); // Find the row
var names = $row.find(".name").text(); // Find the name
var surname = $row.find(".surname").text(); // Find the surname
$.ajax({
type: "POST",
url: "delete_lecturer.php",
data:{ x: names, y: surname}
});
});
});
文件删除_lecturer.php:
<?php
ob_start(); //eliminates buffer collisions
require_once('connect_db.php');
if (!empty($_POST['x'])){ $name = $_POST['x']; }
if (!empty($_POST['y'])){ $surname = $_POST['y']; }
$result = pg_query(connect(), "delete from lecturer where name='$name' and surname='$surname'");
echo "did it";
//dump the result object
var_dump($result);
// Closing connection
//reloading the page
header("location: lecturer.php?fail=2");
?>
文件 connect_db.php 有效(我已经用其他需要它的文件对其进行了测试)。我正在尝试从 table 中获取名字和姓氏,将其传递给 delete_lecturer.php 文件并从数据库中删除与我的选择相匹配的行。 jquery代码中的名字和姓氏存储正确(我也测试过)。
但是代码还是不行。
任何想法出了什么问题或如何解决?
顺便说一下,我使用 postgresql 作为数据库。
您可以执行此代码:
$.ajax({ type: "POST",
url: "delete_lecturer.php",
data: { x: names, y: surname}
})
.done(function( msg ) {
msg = $.trim(msg);
// Use alert or console.log()
alert(msg);
console.log(msg);
})
.fail(function( msg ) {
// Use alert or console.log()
alert(msg);
console.log(msg);
});
使用该代码,您可以检查您的代码哪里有问题,因为它似乎没有任何问题
我有以下代码:
文件lecturer.php:
$(document).ready(function(){
$(".use-address").click(function() {
var $row = $(this).closest("tr"); // Find the row
var names = $row.find(".name").text(); // Find the name
var surname = $row.find(".surname").text(); // Find the surname
$.ajax({
type: "POST",
url: "delete_lecturer.php",
data:{ x: names, y: surname}
});
});
});
文件删除_lecturer.php:
<?php
ob_start(); //eliminates buffer collisions
require_once('connect_db.php');
if (!empty($_POST['x'])){ $name = $_POST['x']; }
if (!empty($_POST['y'])){ $surname = $_POST['y']; }
$result = pg_query(connect(), "delete from lecturer where name='$name' and surname='$surname'");
echo "did it";
//dump the result object
var_dump($result);
// Closing connection
//reloading the page
header("location: lecturer.php?fail=2");
?>
文件 connect_db.php 有效(我已经用其他需要它的文件对其进行了测试)。我正在尝试从 table 中获取名字和姓氏,将其传递给 delete_lecturer.php 文件并从数据库中删除与我的选择相匹配的行。 jquery代码中的名字和姓氏存储正确(我也测试过)。
但是代码还是不行。 任何想法出了什么问题或如何解决? 顺便说一下,我使用 postgresql 作为数据库。
您可以执行此代码:
$.ajax({ type: "POST",
url: "delete_lecturer.php",
data: { x: names, y: surname}
})
.done(function( msg ) {
msg = $.trim(msg);
// Use alert or console.log()
alert(msg);
console.log(msg);
})
.fail(function( msg ) {
// Use alert or console.log()
alert(msg);
console.log(msg);
});
使用该代码,您可以检查您的代码哪里有问题,因为它似乎没有任何问题