desithugg Posted February 24, 2007 Share Posted February 24, 2007 I'm having a little problem with a script //$albums = Saad1,saad2,saad3 $albums = $_POST['name']; if($albums == "") { $error = "You did not specify name(s) for the album(s)."; setcookie("ERROR", $error); header("location: index.php?action=createalbum"); exit; } $listArray = explode(',', $albums); foreach($listArray as $album){ $query = mysql_query("SELECT * FROM albums where name = '$album' && owner = '$id' or id = '$album' && owner = '$id'") or die(mysql_error()); $row = mysql_fetch_array( $query ); $album_check = $row["name"]; if($album_check != "") { $error = "$error<br>$album already exists.<br>"; } else { $query = mysql_query("insert into albums(id,name,owner) values ('','$album','$id')") or die(mysql_error()); $error = "$error<br>$album has been created.<br>"; } setcookie("ERROR", $error); header("location: index.php?action=createalbum"); exit; } so i enter 3 albums in the form seperating them using commas. But when I run the script and submit. It only takes the action for the first album in this case "saad1" the first time i submit it sad Album Saad1 created. nothing about saad1 or saad2 but when i ran it again it said Album Saad1 alreadt exists nothing about album saad1 or saad2 I checked the database too and only album Saad1 was in there. Link to comment https://forums.phpfreaks.com/topic/39940-foreach-problem/ Share on other sites More sharing options...
Psycho Posted February 24, 2007 Share Posted February 24, 2007 You should really format your code with indents to group code within the logical constructs. It makes it much easier to read and debug. If you had, I thikn you would have seen the problem very easily. The problem is that you have a "header" and "exit" statement at the end of your foreach loop. So, that loop will only ever execute once. There is also some problem with your logic for existing albums. After running the query you just need to check if the result has any records like this: $query = mysql_query("SELECT * FROM albums where (name = '$album' or id = '$album') && owner = '$id'") or die(mysql_error()); if(mysql_num_rows($query) == 0) Link to comment https://forums.phpfreaks.com/topic/39940-foreach-problem/#findComment-193094 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.