[英]Deleting record from MySQL
I have trouble deleting record from my MYSQL Database(with Slim PHP framework). 我无法从MYSQL数据库(使用Slim PHP框架)中删除记录。 My code is:
我的代码是:
PHP 的PHP
<?php
require 'Slim/Slim.php';
$app = new Slim();
$app->delete('/delete_article', 'deleteArticle');
$app->run();
function deleteArticle() {
$request = Slim::getInstance()->request();
$article = json_decode($request->getBody());
$sql = "DELETE FROM articles WHERE article_name = ':article_name'";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("article_name", $article->name);
$stmt->execute();
$db = null;
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
Template controller: 模板控制器:
'use strict';
app.controller('clankyCtrl', ['$scope', '$http', '$location',
function ($scope, $http, $location) {
$scope.delete_article = function(article) {
$http.delete('data/api/delete_article', article).success(function(){
$location.path('home/clanky');
});
};
}]);
Template: 模板:
<tr ng-repeat="article in articles">
<td>{{article.article_name}}</td>
<td ng-bind-html="article.article_content | cut:true:100"></td>
<td class="text-right">{{article.article_datetime}}</td>
<td>edit/<button ng-click="delete_article(article)">Delete</button></td>
</tr>
Since HTTP response is 200 I suppose the mistake is probably in data selection. 由于HTTP响应为200,我认为错误可能出在数据选择上。
You don't need quotation marks around :article_name
in your query. 您不需要在查询中的
:article_name
周围使用引号。 PDO bindParam takes care of this for you. PDO bindParam会为您解决这个问题。 You also need to colon in front of the name when binding.
绑定时,还需要在名称前面加冒号。 Try this:
尝试这个:
$sql = "DELETE FROM articles WHERE article_name = :article_name";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam(":article_name", $article->name);
$stmt->bindParam(":article_name", $article->name);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.