删除MySQL和角度数据 [英] Delete data from mysql and angular

查看:162
本文介绍了删除MySQL和角度数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习angularJs,我想创建一个PHP和angularJs基本的CRUD应用程序。结果
我被困在删除栏目,请帮助我。结果
这里是我的code:结果
HTML

I'm learning angularJs and i want to create basic crud app with php and angularJs .
i stuck in delete section , please help me .
here is my code :
HTML

<ul>
    <li ng-repeat="data in data">
        {{data.ID}}
        {{data.subject}}
        {{data.body}}
        <a ng-click="delete(data.id)" href="">Delete</a>
    </li>
</ul>  

JS

$scope.delete = function(){
    var id = $scope.data.id;
        that = this;
    $http.get("delete.php?id=" + id)
        .success(function(data){
            $scope.data.splice(that.$index, 1);
        })
}     

的PHP

 include('config.php');
    mysql_select_db('ngdb');
    $id = $_GET ['id'];
    $sql = "SELECT * FROM story";
    $records = mysql_query($sql);

    if(isset($_GET ['id'])){
        $id = $_GET ['id'];
        $delete = "DELETE FROM story WHERE id= '$id'";
        $res = mysql_query($delete) or die ("FAILED" .mysql_error());

    }

我在哪里错了?

推荐答案

您需要将 $指数传递给删除方法,并从数据删除项目对象

You need to pass the $index to the delete method and remove that item from data object

HTML

<ul>
    <li ng-repeat="row in data">
        {{row.ID}}
        {{row.subject}}
        {{row.body}}
        <a ng-click="delete(row.ID, $index)">Delete</a>
    </li>
</ul>  

JS

$scope.delete = function(deletingId, index){

    $http.get("delete.php?id=" + deletingId)
        .success(function(data){
            $scope.data.splice(index, 1);
        })
}    

也别做的。


  • 为NG-重复键使用不同的名称(现在我改成而不是数据

  • 请不要空属性HREF =当你点击这个整个页面可以becase的刷新(现在我删除了这一点)

  • 请不要去在PHP precated语法使用。使用 PDO 的MySQL istead _ *

  • Use different name for ng-repeat key (Now I changed to row instead of data)
  • Don't empty the attribute href="" becase when you click this entire page may refresh (Now I removed this)
  • Don't use deprecated syntax in PHP. Use PDO istead of mysql_*

这篇关于删除MySQL和角度数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆