Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 25th, 2006, 07:39 PM
Registered User
 
Join Date: Apr 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to dustygn
Default Pass variables from array inside a loop

Ok i have code that can display the users that have been entered into the database, which uses a loop. Inside that loop i have created delete buttons so that the user can delete one of them. The problem is that i cannot pass the $id of just the user to be deleted, instead it passes them all through and no matter which you pick, the last is always chosen to be deleted.
Here is what ive done:

Code:
<?php
include('verifyuser.php');
?>
<html>
<body>
<?php
if(isset($_SESSION['valid_user'])){
    echo "<b>Welcome back " .$_SESSION['valid_user']. "<center><br></b>";

$query="select * from users";
$result=mysql_query($query) or die ("Error in query");
echo'<form action="deleteuser2.php" method="GET">';
echo'<table><tr><th>ID</th><th>User</th><th></th></tr>';
while($row=mysql_fetch_array($result)){

  echo '<tr><td>'.stripslashes($row['id']);

$delete=stripslashes($row['id']);
    echo'<input type="hidden" name="id" value="'.$delete.'"</td>';
<?php
include('verifyuser.php');
?>
<html>
<body>
<?php
if(isset($_SESSION['valid_user'])){
    echo "<b>Welcome back " .$_SESSION['valid_user']. "<center><br></b>";

$query="select * from users";
$result=mysql_query($query) or die ("Error in query");
echo'<form action="deleteuser2.php" method="GET">';
echo'<table><tr><th>ID</th><th>User</th><th></th></tr>';
while($row=mysql_fetch_array($result)){

  echo '<tr><td>'.stripslashes($row['id']);

$delete=stripslashes($row['id']);
 
  echo'<input type="hidden" name="id" value="'.$delete.'"</td>';



  echo '<td>'.stripslashes($row['userid']);
$userid=$row['userid'];
  echo '</td><td><input type="submit" value="Delete"</td></tr>';
   }
   echo"<a href='adminmenu.php'>Administration menu</a>";
}
else{
echo"Please login to view this page<br>";
echo"<a href='authmain.php'>Login</a>";}
   ?>
   </table>

   </form>
   </html>



  echo '<td>'.stripslashes($row['userid']);
$userid=$row['userid'];
  echo '</td><td><input type="submit" value="Delete"</td></tr>';
   }
   echo"<a href='adminmenu.php'>Administration menu</a>";
}
else{
echo"Please login to view this page<br>";
echo"<a href='authmain.php'>Login</a>";}
   ?>
   </table>

   </form>
   </html>
 
Old April 27th, 2006, 09:28 AM
Friend of Wrox
 
Join Date: Dec 2004
Posts: 154
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I coul dprobably do it now better if I had to redo my code but that what I done last year:
<form name="<?php echo "button".$Name."1"; ?>" method="POST" action="adminchange.php?Name=<?php echo "$Name"; ?>">

So basically for each loop - you create a form and pass the variable via the url; obviously you need a seriously good security on the next file, you don't want people to just put a name in the url and the data is deleted.

You can retreive the data from the url this way:
$Name_action=$_GET['Name'];

There is most likely a beter way to do this but this might help you.. good luck.
 
Old May 3rd, 2006, 05:35 PM
Registered User
 
Join Date: Apr 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to dustygn
Default

I figured how to get round it by adding radio buttons and passing the value of the selected radio button through.

Code:
<?php
SESSION_START();
include('verifyuser.php');

?>
<html>
<body>
<?php
if(isset($_SESSION['valid_user'])){
    echo "<b>Welcome back " .$_SESSION['valid_user']. "<center><br></b>";

$query="select * from users";
$result=mysql_query($query) or die ("Error in query");
echo'<form action="deleteuser2.php" method="GET">';
echo'<h2>Choose user to delete</h2>';
echo'<table><tr><th>ID</th><th>User</th><th></th></tr>';
while($row=mysql_fetch_array($result)){

  echo '<tr><td>'.stripslashes($row['id']);//display id number

$delete=stripslashes($row['id']);


  echo '<td>'.stripslashes($row['userid']); //display user name
$userid=$row['userid'];
 
  echo'<td><input type="radio" name="usertodelete" value="'.$userid.'">';

   }
    echo '</tr><tr><td colspan="2"><input type="submit" value="Delete"></td></form>';
    echo '<td><form action ="manusers.php"><input type="submit" value="Cancel"></td></form></table>';

}
else{
echo"Please login to view this page<br>";
echo"<a href='authmain.php'>Login</a>";}
   ?>
   </table>
  <a href='adminmenu.php'>Administration menu</a>
   </form>
   </html>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass Variables obrienkev C# 2005 1 July 18th, 2008 03:58 AM
loop through tables inside .mdb file using VB.NET remya1000 General .NET 3 September 24th, 2007 12:45 PM
Unique Control IDs inside a loop using a varible Joshua Sebastian Classic ASP Basics 2 February 3rd, 2005 10:44 AM
Can session variables be used inside Java? Annoyamouse BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 October 14th, 2004 02:26 PM
How do do a loop with variables jgsteel VB Databases Basics 1 December 15th, 2003 07:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.