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?
– Marcelo Diniz
Actually it’s just curiosity, I’m studying php
– Miguel
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
– Marcelo Diniz
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
– Miguel
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();" />
– Lucas Henrique
I did the update on the top, but it’s not working
– Miguel
Remember that you are required to pass the connection to
mysql_query()
when that instruction is within a function.– rray
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
– Marcelo Diniz