SQL删除语法错误,也是一些PHP和Jquery

时间:2021-03-30 15:43:21

I have a database table named 'favoritecats' with the following fields:

我有一个名为'favoritecats'的数据库表,其中包含以下字段:

  • id
  • catName
  • catId

I am using Jquery to run this function on click event of an element on DOM Ready.

我正在使用Jquery在DOM Ready上的元素的click事件上运行此函数。

// Delete a Favorite Category from SQL Database
    $('.deleteCatFavs').click(function(){      // On click of .deleteCatFavs
    var actionRequested = "AJAX_delFavCat";    // My Personal PHP Controller Identifier
    var url = "index.php";                     // URL to post to

// Now Im getting the data I want to post into variables.
    var catId = $("input[name=FavCats]:checked").val();
    var rowId = $("input[name=FavCats]:checked").attr("id");

// Now we make the post
    $.post(url, {AJAX_Action: actionRequested, rowId: rowId},
        function(data){
            $("#favCats").fadeIn().html(data);
           });
    });

This all Works Fine,

这一切都很好,

But below I have the PHP Code to delete the selected rowId from above from the database. Here is where im having the issue, Im sure its a SQL error.

但下面我有PHP代码从数据库中删除上面选定的rowId。这是我遇到问题的地方,我确定它是一个SQL错误。

public function AJAX_delFavCat(){

$rowId = isset($_POST['rowId'])?$_POST['rowId']:''; // Get Posted Variable
// Below, I want to delete the posted rowId, from the DB,
$this->database->query("DELETE FROM 'favoritecats' WHERE id='$rowId'");

// My personal Loaders, I need help with the delete query above!!
$data = $this->database->query("SELECT * FROM favoritecats");
$this->load->view('Ajax_addToFavCats.php', $data, $ajax=1);

} // End

The "DELETE FROM 'favoritecats' WHERE id='$rowId'" doesn't work, what am I doing wrong?

“DELETE FROM'lovecats'WHERE id ='$ rowId'”不起作用,我做错了什么?

[EDIT]
I get the following error through SQL: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''favoritecats' WHERE id='27'' at line 1

[编辑]我通过SQL得到以下错误:您的SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在第1行''favoritecats'WHERE id ='27''附近使用正确的语法

Also, How would I write a Jquery function using the $.ajax method instead of the $.post method im using now, does it really make a difference?

另外,我如何使用$ .ajax方法而不是现在使用的$ .post方法编写Jquery函数,它真的有所作为吗?

2 个解决方案

#1


2  

what am I doing wrong?

我究竟做错了什么?

You've got a SQL-injection security hole.
See: How does the SQL injection from the "Bobby Tables" XKCD comic work?

你有一个SQL注入安全漏洞。请参阅:“Bobby Tables”XKCD漫画中的SQL注入如何工作?

Change this

$rowId = isset($_POST['rowId'])?$_POST['rowId']:''; // Get Posted Variable
// Below, I want to delete the posted rowId, from the DB,
$this->database->query("DELETE FROM 'favoritecats' WHERE id='$rowId'");

To this

$rowId = isset($_POST['rowId'])?$_POST['rowId']:''; // Get Posted Variable
$rowId = mysql_real_escape_string($rowId);
// Below, I want to delete the posted rowId, from the DB,
$this->database->query("DELETE FROM `favoritecats` WHERE id='$rowId'");

To properly escape your inputs.

正确地逃避您的输入。

Back to your question

回到你的问题

$this->database->query("DELETE FROM `favoritecats` WHERE id='$rowId'");

Will fix your error.
Note the use of backticks around tablenames, Normal quotes are not allowed and are in fact a syntax error.

将修复您的错误。注意在表名周围使用反引号,不允许使用正常引号,实际上是语法错误。

#2


0  

Table name should not be in single quotes. Use backticks or leave it as it is .

表名不应该是单引号。使用反引号或保持原样。

DELETE FROM 'favoritecats' - wrong
DELETE FROM `favoritecats` - correct
DELETE FROM favoritecats - also correct

#1


2  

what am I doing wrong?

我究竟做错了什么?

You've got a SQL-injection security hole.
See: How does the SQL injection from the "Bobby Tables" XKCD comic work?

你有一个SQL注入安全漏洞。请参阅:“Bobby Tables”XKCD漫画中的SQL注入如何工作?

Change this

$rowId = isset($_POST['rowId'])?$_POST['rowId']:''; // Get Posted Variable
// Below, I want to delete the posted rowId, from the DB,
$this->database->query("DELETE FROM 'favoritecats' WHERE id='$rowId'");

To this

$rowId = isset($_POST['rowId'])?$_POST['rowId']:''; // Get Posted Variable
$rowId = mysql_real_escape_string($rowId);
// Below, I want to delete the posted rowId, from the DB,
$this->database->query("DELETE FROM `favoritecats` WHERE id='$rowId'");

To properly escape your inputs.

正确地逃避您的输入。

Back to your question

回到你的问题

$this->database->query("DELETE FROM `favoritecats` WHERE id='$rowId'");

Will fix your error.
Note the use of backticks around tablenames, Normal quotes are not allowed and are in fact a syntax error.

将修复您的错误。注意在表名周围使用反引号,不允许使用正常引号,实际上是语法错误。

#2


0  

Table name should not be in single quotes. Use backticks or leave it as it is .

表名不应该是单引号。使用反引号或保持原样。

DELETE FROM 'favoritecats' - wrong
DELETE FROM `favoritecats` - correct
DELETE FROM favoritecats - also correct