SQL删除语法错误,还有一些PHP和Jquery
我有一个名为“favoritecats”的数据库表,其中包含以下字段:
- id
- catName
- catId
我正在使用 Jquery 在 DOM Ready 上的元素的单击事件上运行此函数。
// 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);
});
});
这一切都很好,
但是下面 我有 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
“DELETE FROM 'favoritecats' WHERE id='$rowId'”不起作用,我做错了什么?
[编辑]
我通过 SQL 得到以下错误: 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行 ''favoritecats' WHERE id='27'' 附近使用的正确语法
另外,如何使用 $.ajax 方法而不是 $ 编写 Jquery 函数我现在使用的.post方法,真的有区别吗?
I have a database table named 'favoritecats' with the following fields:
- id
- catName
- catId
I am using Jquery to run this function on click event of an element on DOM Ready.
// 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.
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?
[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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有一个 SQL 注入安全漏洞。
请参阅:“Bobby Tables”XKCD 漫画中的 SQL 注入是如何工作的?
更改this
到 this
正确转义您的输入。
回到您的问题
将修复您的错误。
请注意在表名周围使用反引号,不允许使用普通引号,这实际上是一个
语法错误
。You've got a SQL-injection security hole.
See: How does the SQL injection from the "Bobby Tables" XKCD comic work?
Change this
To this
To properly escape your inputs.
Back to your question
Will fix your error.
Note the use of backticks around tablenames, Normal quotes are not allowed and are in fact a
syntax error
.表名不应包含在单引号中。使用反引号或保持原样。
Table name should not be in single quotes. Use backticks or leave it as it is .