-1

I want to delete the row in database that has the text output using the button right next to the text. So here is my code:

<?php
...

while ($finresult = mysqli_fetch_array($result)){
        if ($finresult['id'] == $id){
        print_r($finresult['email']);
        $print = $finresult['email'];

        echo '<p id="demo"> </p><button onclick="getElementById(\'demo\').innerHTML=myajax($print)">Delete</button>
            ';

?>

<html>

<script> 
        function myajax(id){

                $.ajax({
                    type: "POST",
                    url: "delete.php",
                    data: id
                    success: function(data){
                        if (data == id + "y"){
                            return "It works";
                        }
                        else{
                            return "Doesn't work";
                        }
                    }
                });


        }
</script>

</html>

This is the delete.php

<?php 
$link = mysqli_connect("localhost","*****","****");
mysqli_select_db($link, 'form');

$id = $_POST['id'];

$query = "DELETE FROM Customers WHERE Customers.email = '$id'";
$result = mysqli_query($link, $query);
if (isset($result)){
    echo "y";
}
else{
    echo "n";
}
?>

I would really appreciate if someone could tell me what is going wrong. Thanks!

John
  • 41
  • 4
  • Well, for starters, your code is wide open to SQL injection. But aside from that, in what way is this not working? What indication do you have that there is a problem? What debugging have you done so far? – David Mar 17 '17 at 08:51
  • does the email equal to an id? – Masivuye Cokile Mar 17 '17 at 08:52
  • one thing I can spot the php you supplied have errors – Masivuye Cokile Mar 17 '17 at 08:52
  • `$query = "DELETE FROM Customers WHERE Customers.email = '$id'";` this is wrong – Masivuye Cokile Mar 17 '17 at 08:54
  • @David I clicked on the button, and nothing popped up. If I replace it with a standard function in js then it works fine. Sorry I'm really new to this. What should I do to protect my database? – John Mar 17 '17 at 08:56
  • @MasivuyeCokile do you mind telling me exactly what is wrong? – John Mar 17 '17 at 08:57
  • @Hayley: "Nothing popped up" isn't much debugging. Use your browser's debugging tools. Is there an error on the browser debugging console? What is the actual HTML/JavaScript being rendered and executed here? Is the HTTP request made? Does it have the data you expect? What is the server's response? Are there any errors in the PHP logs? What happens when you check the SQL query result for errors? What is the actual SQL query being executed? Are there matching records at all? – David Mar 17 '17 at 08:57
  • `$query = "DELETE FROM Customers WHERE Customers.id= '$id'"` I think this is what u need – Masivuye Cokile Mar 17 '17 at 08:58
  • Can we see your Table or Columns so people can decide if you write your query well or not. – ReadyFreddy Mar 17 '17 at 08:59
  • @David The error shown in the console is Uncaught ReferenceError: myajax is not defined. – John Mar 17 '17 at 09:03
  • @Hayley: Then the JavaScript function isn't defined properly. What's the actual resulting HTML/JavaScript being produced here? Also, why on earth are you setting something's `.innerHTML` property to the results of a function that doesn't return anything? What's that even supposed to do? You say "nothing popped up", but you don't have any code anywhere which *would* "pop something up". – David Mar 17 '17 at 09:04

1 Answers1

0

You must wrap the function call params by single quotes:

echo '<p id="demo"></p>
      <button onclick="getElementById(\'demo\').innerHTML=myajax(\'' . $print . '\')">Delete</button>';

Also:

  1. Take care of SQL Injection. Learn how to prevent it
  2. ID is not Email so name the variables accordingly to help you understand the code in the future
Community
  • 1
  • 1
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50