I cannot run DELETE in SQL when submitting form via POST in PHP

Asked

Viewed 564 times

0

I have this code and would like to delete a list of users when the form was submitted, but it is not working: Any hints?

    if (!empty($_POST['username']) && !empty($_POST['email'])) {

    $query = mysql_query('INSERT INTO users (name, email, joined) VAlUES ("'.$_POST['username'].'", "' .$_POST['email'].'", NOW())') or die ('daw');
    echo ('Cool<br>');
    unset($_POST['username']);
    unset($_POST['email']);

}
else {
    echo ('<strong style="color:red;">FILLLL</strong>');
}
$sql = ('SELECT * FROM users');
$mydata = mysql_query($sql, $con);

function deleteAll() {

        mysql_query('DELETE FROM users');

}
?>
<html lang="en">
<head>

    <meta charset="UTF-8">
    <title>Register</title>

    <meta name="viewport" content="width=width-device, initial-scale=1.0"/>
</head>
<body>

    <form action ="register.php" method="POST">
        Username:
        <br>
        <input type ="text" name ="username" value=""/>
        <br><br>
        Email:
        <br>
        <input type ="text" name ="email" value=""/>
        <br><br>
        <input type="submit" name ="submit" value="Register!">
    </form>
    <table>
    <?php
    while ($result = mysql_fetch_array($mydata)) {
        echo ('<tr>
              <td>' .ucfirst($result['name']). '</td>
              <td>' .$result['email']. '</td>
              <td>' .date('F j, Y, H:i ', strtotime($result['joined'])).'</td>
            </tr>');
    }
    ?>
    </table>
<input type="button" value="deleteAll" class="botao" onclick="deleteAll();" />
</body>
</html>
  • Just so you know, I haven’t looked through all the code yet, but the user accessing the database has the privilege of running delete?

  • Actually it’s just curiosity, I’m studying php

  • Right... other by looking at the delete command tbm is wrong.. IS DELETE FROM <table> [WHERE key=value]. And tbm doesn’t pass to a function that way you passed the action. Take a better look at it

  • Obgado, and that 'value' would be what? the key is id... what I want is to delete everyone soon. I used this tip in the action: http://stackoverflow.com/questions/17390488/call-form-submit-action-from-php-function-on-same-page-doesnt-work

  • This value refers to a specific key you want to delete. In your case, DELET FROM users is correct because you want to delete all users. In your code there are 2 <form> tags that you could remove and put only <input type="button" value="deleteAll" class="boot" onclick="deleteAll();" />

  • I did the update on the top, but it’s not working

  • Remember that you are required to pass the connection to mysql_query() when that instruction is within a function.

  • When I put in [] it means that it is not mandatory, just do DELETE FROM users. But to work you must follow the idea of the @NULL answer

Show 3 more comments

1 answer

6


The way your sql query was written is wrong, the correct syntax is: DELETE FROM TABELA

To call the function PHP the way you are trying to do, you need to put your function within the call of a $_GET (Takes arguments via URL), change the form method for GET and change the value of Submit input for deleteAll.

Example:

if($_GET["action"] == "deleteAll")
{
    // Sua função aqui
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.