Reputation: 21
I'm running into a problem where i'm trying to delete a row from a Mysql table. This is the code I wrote :
$sql = "DELETE FROM members WHERE name='$_POST[delmember]'";
$retval = mysql_query($sql);
if(!$retval) {
die("Couldn't delete data: " . mysql_error());
}
The code to print my database :
$sql = 'SELECT name FROM members';
$retval = mysql_query($sql, $conn);
if(!$retval) {
die("Couldn't get data: " . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_NUM)) {
if($col == $cols) {
$col = 0;
echo '</tr><tr>';
}
echo '<td align="center"><a class="stats" href="members.html?stats='.$row[0].'">' . $row[0] . '</a></td>';
$col++;
}
This actually 'empties' the row, but leaves an empty cell that actually shows when I print my data. I have to then go to phpMyAdmin to delete the empty space manually. I do not understand why it does this.
I am still a beginner and programming is really just a hobby of mine, but I would appreciate if someone would give me clues and point me to the right direction.
Thanks a lot!
Upvotes: 0
Views: 152
Reputation: 535
try it like this.
$sql = "DELETE FROM members WHERE name='" . mysql_real_escape_string($_POST["delmember"]) . "'";
btw, if name
field is not unique then it is not a good idea deleting users by name, you should use id field as primary key and use id for all operations.
and the other thing is use mysqli or pdo instead of mysql.
Upvotes: 1
Reputation: 2888
To better learn MySQL syntax, fire up a console to a session of MySQL and type some commands directly. Your syntax looks correct, but we don't know the rules of the database you're using or what view you're looking in.
It's hard to tell if you're seeing an artifact of phpMyAdmin, or a consequence of some other aspect of your database, or if it's a consequence of the code you're using to "print" the data.
Upvotes: 1