简体   繁体   中英

Deleting record from MySQL

I have trouble deleting record from my MYSQL Database(with Slim PHP framework). My code is:

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.

You don't need quotation marks around :article_name in your query. PDO bindParam takes care of this for you. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM