Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 25th, 2006, 07:39 PM
Registered User
 
Join Date: Apr 2006
Location: , , New Zealand.
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>
Reply With Quote
  #2 (permalink)  
Old April 27th, 2006, 09:28 AM
Friend of Wrox
 
Join Date: Dec 2004
Location: London, London, United Kingdom.
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.
Reply With Quote
  #3 (permalink)  
Old May 3rd, 2006, 05:35 PM
Registered User
 
Join Date: Apr 2006
Location: , , New Zealand.
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>
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:56 AM.


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