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
https://forums.phpfreaks.com/topic/75693-solved-argh-function-call-help/
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>";
}

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.