我已經閱讀了多個解決方案來解決這個問題,但是他們都沒有爲我工作。我有兩個文件manageadmin.php和rejectaction.php。 manageadmin.php文件爲用戶提供了拒絕管理員的按鈕。當一個人點擊拒絕按鈕時,管理員狀態必須設置爲零。PHP文件無法從Jquery讀取多個參數ajax
這是我最初的代碼
manageadmin.php
<script type="text/javascript">
$(document).ready(function()
{
/* reject */
$(".reject_btn").click(function(){
var element = $(this);
var reject_id = element.attr('reject_id');
$.ajax({
type: "POST",
url: "rejectaction.php",
data: {reject_id:reject_id},
success: function(){
alert("Reject Successful");
location.reload();
}
});
});
});
</script>
rejectaction.php
$reject_id=$_POST['reject_id'];
$reject_query="UPDATE tbl_admin set admin_status=0 where admin_id='$reject_id'";
mysql_query($reject_query);
這工作得很好,但後來我意識到,我需要這個拒絕多個代碼頁面。所以我想通過表名,列進行更新和列檢查在哪裏條件作爲參數。 這是修改後的代碼:
<script type="text/javascript">
$(document).ready(function()
{
/* reject */
$(".reject_btn").click(function(){
var element = $(this);
var reject_id = element.attr('reject_id');
var tbl_name = "tbl_admin";
var column_reject = "admin_status";
var column_cond = "admin_id";
$.ajax({
type: "POST",
url: "rejectaction.php",
data: {reject_id:reject_id, tbl_name:tbl_name,
column_cond:column_cond,
column_reject:column_reject},
success: function(){
alert("Reject");
location.reload();
}
});
});
});
</script>
rejectaction.php
$reject_id=$_POST['reject_id'];
$tbl_name = $_POST['tbl_name'];
$column_cond = $_POST['column_cond'];
$column_reject = $_POST['column_reject'];
$reject_query="UPDATE '$tbl_name' set '$column_reject' = 0 where '$column_cond'='$reject_id'";
// $reject_query="UPDATE tbl_admin set admin_status=0 where admin_id='$reject_id'";
mysql_query($reject_query);
這個代碼不工作(即未得到更新的狀態)。但alert("Reject Successful")
正在執行。我是一名初學者。任何人都可以指出我犯的錯誤嗎?謝謝
***請[停止使用'mysql_ *'功能(http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-功能在PHP)。*** [這些擴展](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中刪除。瞭解[準備]( http://en.wikipedia.org/wiki/Prepared_statement)[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http:// php .net/manual/en/mysqli.quickstart.prepared-statements.php)並考慮使用PDO,[這真的很簡單](http://jayblanchard.net/demystifying_php_pdo.html)。 –
[Little Bobby](http://bobby-tables.com/)說*** [你的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/60174/how-can- I-防止-SQL注入式-PHP)***。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –
您是否觀看過瀏覽器開發人員工具中的AJAX請求/響應?你有沒有在項目中包含jQuery庫?是否有任何錯誤報告?你在網絡服務器上運行這個嗎? –