Jump to content


Photo

SQL / PHP Arrays help.


  • Please log in to reply
4 replies to this topic

#1 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 16 July 2006 - 11:29 AM

Hi guys,
I have my private messenger inbox and I wanted my members to be-able to tick a box and delete that one ID so they can delete multiple messages at a time, Say if a member has 10 messages and wants to delete 6 at the same time, he ticks the 6 boxs and delets them all.

My problem is When i killed the Code and echod the array list It outputted 1 item out of the two i ticked. any ideas?

my code:
<?PHP
include "Session.php";
$act = $_GET['act'];
	if( !$act ){

include "db.php";
		echo '<form method="post" action="?act=delete">';
$sql = mysql_query("SELECT id,sendto,sendfrom,subject FROM pm WHERE sendto='{$_SESSION['user']['user']}'");
	while( $udat = @mysql_fetch_array($sql, MYSQL_NUM))
		{
		echo '<p>ID: '.$udat[0].'<br>To: '.$udat[1].'<br>From: '.$udat[2].'<br>Subject: '.$udat[3].'<br><input type="checkbox" name="del" value="'.$udat[0].'"></p>';
	}
		echo '<input type="submit" value="Delete"></form>';

	} elseif( $act == delete ) {
		
		if(isset($_POST['del']) && empty($_POST['del']))
			{
			echo "Please hylight the post you wish to delete.";
			echo "<BR>";
			echo "<a href=1test.php>Back</a>";
				exit;
			}
			
			echo $_POST['del'];
			exit;
			
		$del[] = $_POST['del'];
		if(is_array($del))
				{
				foreach($del as $key => $value)
					{
					$sql_delete = "".$value." , ";
				}
			include "db.php";
			$db = mysql_connect("localhost", $login, $pwd);
			mysql_select_db("zroxxco_members");
			
			$dsql = "DELETE * FROM pm WHERE id='$sql_delete'";
			mysql_query($dsql, $db);
			
			echo "You have deleted messages #'.$sql_delete.'";
		}
		exit;
	}
	else
	{
		die('error: unknown location.');	
}	
?>


#2 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 16 July 2006 - 11:56 AM

$del[] = $_POST['del'];
if(is_array($del))
{

foreach($del as $key => $value)
{
$sql_delete = "".$value." , ";
}

Wont the array only itterate once, as $del would only contain $del[0] with the value of $_POST['del'] ?
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#3 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 16 July 2006 - 12:10 PM

probably, thats what confusig me..

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 16 July 2006 - 12:53 PM

Have your checkboxes named something like "del[]" and give each the value of te records id

<input type='checkbox' name='del[]' value'$id' >

When you process the form data you can then delete them all with a single query with

$deleteList = join (',' , $_POST['del']);
mysql_query ("DELETE FROM tablename WHERE id IN ($deleteList)" );
?>


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 16 July 2006 - 02:36 PM

Ah, thanks :]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users