0

I'm trying to delete one line from a table, however and even following the examples on stackoverflow, I can't reach a solution. One of the Important things before start, is that I have the url's hidden, so I don't see the Get url, even the splice don't delete the right line on the table.

HTML:

 <button id="{{value.username}}" type='button' type="button" ng-click="delete(value.username, $index)" class="btn btn-primary">Delete</button></td>

JS

  $scope.delete = function(deletingId, index){
console.log(deletingId);
        $http.get("../admin/deleted.php?username=" + deletingId)
            .success(function(data){
                $scope.data.splice(index, 1);
                console.log('dadasdas');
            })
    }

PHP

 $id = $_GET ['username'];
 $sql = "SELECT * FROM members";
 $records = mysql_query($sql);

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

 }

Am I doing anything wrong ( the scope is working, the php src is correct, but even doing an echo on php it doesn't fulfill nothing) since I've the url's hidden is there anyway to make it as a post?

Thanks in advance

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Vortex10
  • 47
  • 9
  • 2
    You'll need to change the URL to where the php server is running, i.e. `http://localhost:8080/admin/` and, ideally, send a `DELETE` request, not `GET`. – Phix Jan 24 '19 at 22:44
  • What exactly is the issue? Is the wrong line deleted? Or none at all? –  Jan 24 '19 at 22:48
  • The page is already online, so it should already be working... https://stackoverflow.com/questions/27002494/delete-data-from-mysql-and-angular I was checking this example – Vortex10 Jan 24 '19 at 22:48
  • It's not deleting from the db , and not deleting on the angular table the proper line – Vortex10 Jan 24 '19 at 22:48
  • Ok, so 1) what does the XHR (`$http.get()`) look like in the console? Are you getting status 200 back? And 2) how are you determining the `index` you're passing to your `delete` function? Also, I'd probably return all user rows in the PHP code after deletion, then simply replace `$scope.data` with them. (And please do not use the old `mysql_*` functions; chances are your PHP won't run them!) –  Jan 24 '19 at 22:50
  • Chris it provides the right answer on XHR status(200) deleted.php?username=example1 – Vortex10 Jan 24 '19 at 22:59

1 Answers1

1

Try this (I will use Mysqli, because I don't use Mysql);

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);

$id = $_GET['username'];
$sql = "SELECT * FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        if (strtolower($id) == strtolower($row['username'])) {
            $sql = "DELETE FROM users WHERE id=".row['id'];
            $conn->query($sql);
        }
    }
} else {
    echo "No accounts registered!";
}

So, deleting stuff with SQL, is case sensitive. So, I hope/think you have a auto increment id in your database. Then this will 100% surely work!

Aaron Jonk
  • 473
  • 2
  • 7
  • 21
  • I don't have a autoincrement , the id it's an random generated name, however I changed to ur code and It's not working still – Vortex10 Jan 24 '19 at 23:05
  • @Vortex10 in your database, go to your `users` table. Then go to `structure`. Click on add 1 column. And then change the dropdown to `At the beginning of the table`. Click on `start`. Now, at name fill in; `id`. And tick the `A_I` box, click on start, and then again on start. Now you will have a unique id added to your table, if this doens't work (adding the `id`), it's probably because of there being a row already, you need to delete the table then and recreate it with the `id` column. – Aaron Jonk Jan 24 '19 at 23:14
  • done, will try now with ur code ... p.s. the get method is reaching php properly however it provides an error ...FAILEDAccess denied for user 'user'@'localhost' (using password: NO)... – Vortex10 Jan 24 '19 at 23:25
  • It's working deleting on the db , just the angular table is not removing the right line. – Vortex10 Jan 24 '19 at 23:39
  • @Vortex10 did you set username/password of the sql database? – Aaron Jonk Jan 24 '19 at 23:44
  • it's working now @aaron jonk , just the angular error , that don't delete the right one, however in the db it's the right selection – Vortex10 Jan 24 '19 at 23:46