Jump to content

[SOLVED] Argh.. Function call help!


The Midnighter

Recommended Posts

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?

Link to comment
Share on other sites

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>";
}

Link to comment
Share on other sites

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;
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.