View Single Post
  #2 (permalink)  
Old 09-02-05, 02:59 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
hi,
first you need to display your data in tables, it's ways easier in use, second,
just add another 2 columns to your 'userlist', lke this:

PHP Code:

<table border="1">

<tr>
  <td><b>Username</b></td>
  <td><b>Password</b></td>
  <td><b>Edit</b></td>
  <td><b>Delete</b></td>
</tr>

<?php

//First, we need to connect to the server
//the format is $connection = mysql_connect("address","username","password");
$conn mysql_connect("localhost","username","password");

//now choose the database to use
mysql_select_db("mydatabase");

$getusers mysql_query("SELECT * FROM users ORDER BY id DESC");//query 
//the database for all of the news



while($r=mysql_fetch_array($getusers)){//while there 
//are rows in the table

extract($r);//remove the $r so its just $variable 




echo("<tr><td><font size=3>$username</font></td>
<td><font size=3>Password: 
$password </font></td>
<td><a href=\"edit_user.php?id=
$id\">Edit</a></td>
<td><a href=\"del_user.php?id=
$id\">Delete</a></td></tr>");



}
?>
</table>
Now lets make the edit_user page. It's a basic page (no checking for script tags or invalid usernames/passwords:
PHP Code:

<?php

//open connection here
$id$_GET['id'];
if(
$_POST['edit'])):

if(!empty(
$_POST['username']) && !empty($_POST['password'])):
 
$sql "UPDATE users SET username='".$_POST['username']."', password='".$_POST['password']."' WHERE id='$id'";
 
mysql_query($sql) or die(mysql_error());
 
header("Location: //userlistingscript");
 exit();
else:
 echo 
"Some fields Were left blank";
endif;

else:
$sql "SELECT username,password FROM users WHERE id='$id'";
$r mysql_query($sql) or die(mysql_error());
while(
$all mysql_fetch_object($res)):
$username=$all->username;
$password=$all->password;
endwhile;
?>
<html>
<form method="post"> //action= not defined => page recalls itself
<input type="text" name="username" size="25" value="<? echo $username?>"><br>
<input type="password" name="password" size="25" value="<? echo $password?>"><br><!-- you don't do this normally: displaying a password so you can edit it -->
<input type="submit" name="edit" value="Edit !">
</form>
</html>
so now we've made the edit script, let's make the delete script:
PHP Code:

<?

//open connection here
$id=$_GET['id'];
$sql"DELETE FROM users WHERE id='$id'";
mysql_query($sql) or die(mysql_error());
header("Location: //userlisting page here");
exit();
Hope it helped you
Greetz

UnrealEd
Reply With Quote