Jump to content

[SOLVED] Looping through form data with multiple mysql_select's


Recommended Posts

Hello all,

 

Here's whats up.  I have a form which has a list of groups.  The list of groups correspond with a groupid field in the usersgroups table.  Each user can have multiple groups.  For each group that the user is in there is a record in the table. 

 

The $groups variable is the $_POST['group'] from the form. In the form they can select multiple groups. I have [] in the form so no worries there.

 

I need to select different users in the groups and then add them to an array ($mailer[]) to process through a mailer function that we have.

 

I don't get any mysql errors or anything.  Everything checks out with that.  Yet I don't get the expected results or desired results.  Can you all check my logic and help me out. 

 

When I echo out $row["userid"] 14 comes out ... I don't have a user with 14 as the id or even in my database at all.  I don't know how that happened.

 

Thanks in advance for your wisdom!

 

foreach($groups as $g){
	$query = "SELECT * FROM usersgroups WHERE groupid = '$g'";
	$result = mysql_query($query) or die(mysql_error());
	while ($row = mysql_fetch_array($result) or die(mysql_error())) {
		echo $row["userid"];
    		$query1 = "SELECT * FROM people WHERE id = '".$row["userid"]."'";
		$result1 = mysql_query($query1) or die(mysql_error());
		$row1 = mysql_fetch_array($result1) or die(mysql_error());
		$mailer[] = $row1['phone'].$row1['provider'];
	};
}

Link to comment
Share on other sites

That worked. Thanks for the help!

 

The final code looks like this. Any suggestions?

 


foreach($groups as $g){
	$query = "SELECT * FROM people, usersgroups WHERE usersgroups.userid = people.id AND groupid = '$g'";
	$result = mysql_query($query) or die(mysql_error());
	while ($row = mysql_fetch_assoc($result) or die(mysql_error())) {
		echo $row['phone'];
	};
}
};

[/]

Link to comment
Share on other sites

OK.  I tried it trying to not just echo out the data but add it to an array and that didn't work.  Any suggestions? Here's the code:

 


foreach($groups as $g){
	$query = "SELECT * FROM people, usersgroups WHERE usersgroups.userid = people.id AND groupid = '$g'";
	$result = mysql_query($query) or die(mysql_error());
	while ($row = mysql_fetch_assoc($result) or die(mysql_error())) {
		echo $row['phone'];
                        $test[] = $row['phone'];
	};
};

print_r($test);

 

It echo's out the phone numbers but never adds them to the array ... any thoughts?

Link to comment
Share on other sites

Thanks for the reply michaellunsford!

 

That didn't work ...

 

Here is the entire piece of code I am working with.  I hope this helps.

 

if($error == 'yes') {
$_SESSION['mes']['type'] = 'error';
unset($error);
header('Location: send_message.php');
exit;
} else {
require('../lib/functions.php');
require_once('../db.php'); 

unset($error);
$content = sanitize($_POST['content']);

$groups = $_POST['group'];

foreach($groups as $g){
	$query = "SELECT * FROM people, usersgroups WHERE usersgroups.userid = people.id AND groupid = '$g'";
	$result = mysql_query($query) or die(mysql_error());
	while ($row = mysql_fetch_assoc($result) or die(mysql_error())) {
		echo $row['phone'];
		$test[$i++] = $row['phone'];
	};
echo 'test';
var_dump($test);
};
};

 

It does echo out the $row['phone'] fine.

 

Right now ... It doesn't even echo out the test or do a var_dump of $test.  What am I missing?  It seems like I am just not seeing something. Probably because I have yet to sleep ... go figure.

 

Any suggestions anyone?  Thanks for the help so far!

Link to comment
Share on other sites

After a ton of trial and error I figured it out.

 

The or die(mysql_error()) was the problem.  Cut that out and it works like a champ.

 

Here's the code that works.

 

if($error == 'yes') {
$_SESSION['mes']['type'] = 'error';
unset($error);
header('Location: send_message.php');
exit;
}  else {
require('../lib/functions.php');
require_once('../db.php'); 

$content = sanitize($_POST['content']);

$groups = $_POST['group'];

foreach($groups as $g){
	$query = "SELECT * FROM people, usersgroups WHERE usersgroups.userid = people.id AND groupid = '$g'";
	$result = mysql_query($query) or die(mysql_error());
	while ($row = mysql_fetch_assoc($result)) {
		$test[$i++] = $row['phone'];		
	};
};
};	
var_dump($test);

 

Any ideas why that would make it crap out?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.