The Midnighter Posted November 1, 2007 Share Posted November 1, 2007 TRying to delete a record from an SQL database, here's my code: <?php mysql_connect("localhost","root","internet") or die(mysql_error()); mysql_select_db("php"); if(isset($_COOKIE['seraphBlack'])) { echo "Member list<br><a href=\"logged.php\">Return home</a><br><br><table border=\"1\""; $data = mysql_query("SELECT * FROM members") or die(mysql_error()); while($info = mysql_fetch_array($data)) { $user = $info['username']; $pass = $info['password']; echo " <tr> <td>Name: </td> <td><b>" . $user . "</b><br></td> <td><a href=\"#\" onMouseDown=deleteMember();>Delete</a> </tr> <tr> <td>Password: </td> <td><b>" . $pass . "</b></td> <td><a href=\"#\" onMouseDown=modifyPass();>Modify</a>"; } echo "</table>"; } else { echo('Could not validate your cookie.<br>Re-directing you so you can login...'); echo "<meta http-equiv=\"REFRESH\" content=\"2;url=index.php\">"; } function deleteMember($user) { $sql = mysql_query("'DELETE FROM `members` WHERE `members`.`name` = ".$user." LIMIT 1;") or die(mysql_error()); echo "User " . $info['username'] . " deleted.<br>"; } ?> As it stands, it just doesn't work It's like it's not calling the deleteMember() function, maybe I'm not passing $user correctly? Quote Link to comment Share on other sites More sharing options...
only one Posted November 1, 2007 Share Posted November 1, 2007 mysql_query("'DELETE FROM `members` WHERE `members`.`name` = '$user' LIMIT 1;") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 1, 2007 Share Posted November 1, 2007 You can't use an html link to call a php function, you can use a form though. if(isset($_POST['action']) && $_POST['action'] != 'false'){ switch($_POST['action']){ case 'delete': deleteMember($_POST['user']); break; case 'modify': modifyPass(); break; default: die("You did not select an action to use"); break; } } else {//did not select something to do with the user, so show the form again... echo "<form name='do_something' method='post'> <select name='action'> <option value='false'>- User Options -</option> <option value='delete'>Delete Member</option> <option value='modify'>Modify Pass</option> </select> <input type='hidden' name='user' value='$user'> <input type='submit'><input type='reset'> </form>"; } Quote Link to comment Share on other sites More sharing options...
Daukan Posted November 1, 2007 Share Posted November 1, 2007 You can also pass a variable via the url <a href="dostuff.php?action=delete">Delete</a> <a href="dostuff.php?action=modify">Modify</a> Quote Link to comment Share on other sites More sharing options...
The Midnighter Posted November 1, 2007 Author Share Posted November 1, 2007 I see what you're trying to do here Kratsg, but I don't quite understand where I'm supposed to put your code.. Dunkan, I know what you're getting at here, but is the action=delete just a forward to function delete() ? Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 1, 2007 Share Posted November 1, 2007 try $sql = mysql_query("DELETE FROM `members` WHERE members.name = ".$user." LIMIT 1;") or die(mysql_error()); echo $sql ; Quote Link to comment Share on other sites More sharing options...
The Midnighter Posted November 1, 2007 Author Share Posted November 1, 2007 I've actually managed to get the solution, thanks to Kratsg. Here's the final code: <?php // Login routine mysql_connect("localhost","root","internet") or die(mysql_error()); mysql_select_db("php"); if(isset($_COOKIE['seraphBlack'])) // Does the cookie exist? { echo "Member list<br><a href=\"logged.php\">Return home</a><br><br><table border=\"1\""; $data = mysql_query("SELECT * FROM members") or die(mysql_error()); while($info = mysql_fetch_array($data)) // Keep looking for more users in the database { $user = $info['username']; $pass = $info['password']; echo " <tr> <td>Name: </td> <td><b>" . $user . "</b></td> <td>Password: </td> <td><b>" . $pass . "</b></td> <td><form name='do_something' method='post'> <select name='action'> <option value='false'>- User Options -</option> <option value='delete'>Delete Member</option> <option value='modify'>Modify Pass</option> </select> <input type='hidden' name='user' value='$user'> <input type='submit'><input type='reset'> </form>"; } echo "</table>"; } else // If they aren't logged in.. { echo('Could not validate your cookie.<br>Re-directing you so you can login...'); echo "<meta http-equiv=\"REFRESH\" content=\"2;url=index.php\">"; } function deleteMember($user) // NOT WORKING!!! { $sql = mysql_query('DELETE FROM `members` WHERE `members`.`username` = "'.$user.'" LIMIT 1;') or die(mysql_error()); echo "User " .$user . " deleted.<br>"; } if(isset($_POST['action']) && $_POST['action'] != 'false') { switch($_POST['action']) { case 'delete': deleteMember($user); break; case 'modify': modifyPass(); break; default: die("You did not select an action to use"); break; } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.