从MySQL删除记录

时间:2014-05-29 20:46:26

标签: php mysql angularjs

我无法从MYSQL数据库中删除记录(使用Slim 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() .'}}'; 
    }
}

模板控制器:

    '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');
            });
          };
    }]);

模板:

    <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>

由于HTTP响应为200,我认为错误可能在于数据选择。

2 个答案:

答案 0 :(得分:0)

您的查询中不需要:article_name周围的引号。 PDO bindParam会为您解决此问题。绑定时,您还需要在名称前面冒号。试试这个:

$sql = "DELETE FROM articles WHERE article_name = :article_name"; 
try {       
    $db = getConnection();
    $stmt = $db->prepare($sql);         
    $stmt->bindParam(":article_name", $article->name);

答案 1 :(得分:-1)

$stmt->bindParam(":article_name", $article->name);