Jump to content


Doing thing to multiple rows


4 replies to this topic

#1 Ryusaurus

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 23 July 2007 - 12:54 AM

I own a gaming website, and it has many places where a user has to do something to multiple rows of the database, like deleting their private messages, updating hop information, etcetera. However, since the newest version of PHP came out, it has not been working correctly. I don't know any other way to do it than the way I have been doing it, and I'm using it in many of codes, which means my website is down at the moment. I have been trying to figure out a solution for many days now, and I finally have resorted to coming to Techtuts and request for help.

The codes are large, so I made a smaller, more simpler version. They're pretty much the same thing. What's wrong with them? D: I was thinking it was mostly the foreach part. I don't know why it used to work and now it's not. D:


<?
 
 include"config.php";
 
 echo"<form name=\"form\" method=\"post\" action=\"delete.php\">";
 
 $result = mysql_query("select * from mail where touser = '$userid' ORDER BY id DESC");
 $rows = mysql_num_rows($result);
 
 while($r=mysql_fetch_array($result)){	
 echo"$r[subject] - <input name=\"checkbox[$r[id]]\" type=\"checkbox\" id=\"checkbox[$r[id]]\" value=\"$r[id]\">
 ";
 }
 
 echo"<input name=\"delete\" type=\"submit\" value=\"Delete\"></form>";
 
 ?>


And this would be delete.php.
<?
 
 include"config.php";
 
 if($_POST[delete]){
 
 if(is_array($_POST['checkbox'])){ 
 foreach ($_POST['checkbox'] as $key => $val){
 $sql = mysql_query("DELETE FROM mail WHERE id='$val'");
 }
 echo"Deleted message $val.";
 
 }
 }
 
 ?>

I've also tried look at other PHP tutorials on multiple rows, and those have not worked for me either, so I'm guessing it's the PHP version. D:

Thanks for the help. ;)

#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 23 July 2007 - 11:22 AM

From what PHP version did you get upgraded from, and to what? I'm assuming by latest, you mean 5.2.3.

I don't see any errors with the code, aside from the fact you aren't using mysql_error() to catch a potential error.
$query = mysql_query($sql) or die(mysql_error());

What exactly do you mean by 'not working'. You know that could mean potentially anything...

Just a tip though, instead of using a separate query for deleting every single message and wasting resources, you could easily combine it all into one query.

<?php

include 'config.php';

if($_POST['delete']){
  if(is_array($_POST['checkbox'])){
	array_walk($_POST['checkbox'], 'intval')
	mysql_query('DELETE FROM `mail` WHERE `id` = '.implode(' OR `id`= ', $_POST['checkbox']));
	echo "Deleted messages.";
  }
}
?>


#3 Ryusaurus

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 23 July 2007 - 06:16 PM

Thanks for the reply & help. Sorry about not fully explaining exactly what was going on. I was kind of in a rush to reopen my site so I must have skipped a few details.

Okay anyways, my problem is that the delete page comes up blank. I think it's mostly the is_array is making t he error not show, because when I take it out, it gives me the error. This is what I get:

Quote

Warning: Invalid argument supplied for foreach() in delete.php on line 8

Which made me believe the foreach was not working correctly. It worked fine before the new php version came out, 5.2.3, and then it just stopped working.

#4 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 23 July 2007 - 09:48 PM

Again, what version did you upgrade from.

In this case, you should do some simple debugging.

var_dump($_POST);


#5 Ryusaurus

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 24 July 2007 - 12:36 AM

Oh FROM..sorry.

I'm not really sure, I didn't really notice. I guess it's the previous one, 5.2.2.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users