Page 1 of 1

Putting A Delete Hyperlink On A Table

Posted: Fri Apr 29, 2005 4:39 am
by Arsenal Rule
I was wondering if you anyone could help me again with something!!

I've finished my table view page, and its all working nicely!

But, I would like to have a delete (hyperlink??) function at the end of each row to remove a user and all his data and information stored in the database etc..

this is what my testview.php script currently looks like:

Code: Select all

<?php 


		$conn = mysql_connect("localhost","root","") or die("Could Not Connect To The Database"); 
		mysql_select_db("aziz",$conn) or die("Could Not Select The Database"); 
		


	$result=mysql_query("SELECT user_id,az_firstname,az_surname,username FROM azizpassword ORDER BY user_id;");
	
	$i=0;
	while( $row=mysql_fetch_array($result) )
	{
		if($i>0)
		{
			echo "<tr valign=top>";
			echo "<td bgcolor=#93B0B4 colspan=4><img src=img/blank.gif wuser_idth=1 height=1></td>";
			echo "</tr>";
		}
		echo "<tr valign=center>";
		echo "<td width=100 class=table><b>".$row['user_id']."</b></td>";
		echo "<td width=100 class=table><a href=\"view.php?id=".$row['user_id']."\">".$row['az_surname']."</a></td>";
		echo "<td width=100 class=table>".$row['az_firstname']." </td>";
		echo "<td width=100 class=table><i>".$row['username']."</i> </td>";
		
		echo "<td class=table></td>";
		echo "</tr>";
		$i++;

	}

	echo "<tr valign=bottom>";
        echo "<td bgcolor=#93B0B4 colspan=6 height=8></td>";
        echo "</tr>";

		
		
			
	 
?>
its got the table data there, and this all works fine, but, i would like to install a delete function hyperlink at the end of each row that will delete all the information of that chosen user from the database, ive tried a few methods but they just dont work.

Any idea's anyone would so great??

thanks a lot in advance I really appreciate it!!

Posted: Fri Apr 29, 2005 5:00 am
by phpScott
it would be the same as your view.php link but it should go to delete.php.

There you would have the your script to delete all the relevant data from the relevant tables.

re

Posted: Fri Apr 29, 2005 5:11 am
by harrisonad
i suggest that you should include your table inside a form
with a hidden field to store selected user id such as

Code: Select all

<input type=hidden name=selected_user_id value=0>
The value must be 0 as default(which means nothing to delete).
Then in every table row, add an extra row for delete button

Code: Select all

echo "
<td>
  <input type=button value='delete' 
    onclick=\"
      document.thisform.selected_user_id=$row[user_id];
      document.thisform.submit();
    \">
</td>";
Notice that in onClick attribute, i set the value of the hidden field to the current user_id while looping through query results.

If this doesn't work out, just let me know. ok?

Posted: Fri Apr 29, 2005 5:46 pm
by bob_the _builder
Hi,

Try this

change this line:

Code: Select all

echo "<td width=100 class=table><a href=\"view.php?id=".$row['user_id']."\">".$row['az_surname']."</a> | <a href=\"../delete.php?userid=".$row['userid']."\">Delete</a></td>";
delete.php:

Code: Select all

<?php

if (array_key_exists("confirm",$_REQUEST)) {

$sql = "DELETE FROM azizpassword WHERE userid = '".$_REQUEST['userid']."'";
$result = mysql_query($sql);

if (!$result) {

echo '<center><b>Error!</b> Please contact the administrator.</center><br><br>';
echo '<center><< <a href="../members.php">BACK</a></center>';

return;

}

echo '<center><b>Record was successfully deleted</b></center><br><br>';
echo '<center><< <a href="../members.php">BACK</a></center>';

} else {

echo '<center><b>Do you really want to delete this record?</b></center><br><br>';
echo '<center><a href="../delete.php?userid='.$_REQUEST['userid'].'&confirm=y">YES</a> - <a href="#" onClick="history.go(-1)">NO</a></center>';

} 

?>
Its not tested .. but should give you the general idea.

Bob

Posted: Sat Apr 30, 2005 12:07 am
by sethpackham
Here's one way you could do it: Add a checkbox in each row, select multiple rows to delete, then click a submit button. Then you'll be asked for confirmation whether you really want to delete the selected rows. Then you click another submit button (OK), and they are deleted.

Here's the relevant code snippets for how you could do it, all in the same file:

Code: Select all

<?php
// If they confirm the deletion of selected people
if ($delete_people_confirmed) {
  echo "<font color=red>The following people were deleted:</font><p>";
  foreach($_SESSION['p'] as $key => $value) {
    if(!empty($value)) {
      $sql_delete = "DELETE FROM $table_people WHERE person_id='$value'";
      mysql_query($sql_delete) or die(mysql_error());
      echo "<b>" . $value . "</b><br>";
    }
  }
}

// If they select people to delete, ask for confirmation
if ($delete_selected_people) {
  if (isset($_POST["p"]) && is_array($_POST["p"])) {
    $_SESSION["p"] = $_POST["p"];
    if (isset($_SESSION['p']) && is_array($_SESSION['p'])) {
      echo "<p>Are you sure you want to delete the following people?</p>"; 
      foreach($_SESSION['p'] as $key => $value) {
        if(!empty($value)) {
          echo "<b>" . $value . "</b><br>";				
        }
      }
    }
    
    echo "<p><form method=\"post\" action=\"" . $PHP_SELF . "\">";
    echo "<input type=submit name=\"delete_people_confirmed\" value=\"OK\">";
    echo "<input type=submit name=\"cancel\" value=\"Cancel\">";
    echo "</form>";
		
  } else {
    echo "nothing selected";
  }
}

// ...
// add a checkbox to each row of your main result set
do { ?>
  <tr>
    <td><input type=checkbox name="p[]" value="<?php echo $myrow["person_id"]; ?>"></td>
    <td><?php echo $myrow["person_id"]; ?></td>
  </tr>
<?php
} while ($myrow = mysql_fetch_array($result));
?>
...
//this is your submit button after you select the check boxes
<input type=submit name="delete_selected_people" value="delete selected people">
I'm obviously not showing all the code in the script, and it's probably hacky, but it works and I hope you get the point. It allows for deleting multiple and provides a confirmation step.