Jump to content

[SOLVED] foreach loop problem


bluebyyou

Recommended Posts

So I have a form that takes a number or emails based on a users input, then if the user exists it updates the table or if the user does not exist creates a new row in the db for that user. The problem is that if the user doesnt already exist in the table the query does not work. I don't know if its the query thats not working or my loop or what.

 

<?php for($i = 1; $i <= $_POST['numtravel']; $i++) {?>
<input name="email[]" type="text" size="18" />
<?php }?><br />

 

 

	
<?php
foreach($_POST['email'] as $email ) 
{
// NEED TO ADD CHECK FOR EMAIL FORMAT HERE!!!

	$query = "SELECT * FROM member WHERE email = '$email'"; // THIS IS THE ONE NOT WORKING!!
	query_db($query);
	$num = mysql_num_rows($result);
	if ($num > 0) // User exists, Update enrollment in year.
	{
		$query2 = "UPDATE member SET `$_POST[year]` = 1 WHERE email = '$email'";
		query_db2($query2);
	}
	else
	{
		$code = mt_rand(11111,999999);
		$query3 = "INSERT INTO member(email,$year,code) VALUES('$email','1','$code')";
		query_db($query3);
	}

}
?>

Link to comment
Share on other sites

replace this line:

<?php
query_db($query);
?>

 

With this:

<?php
$result = query_db($query);
?>

 

I'm assuming that query_db() is a function you created.

 

The reason I told you to change the above code is because on this line of your code:

$num = mysql_num_rows($result);

 

Where did you define $result?

Link to comment
Share on other sites

$result is returned from my function. I dont think thats the problem.

<?php
function query_db($query)
{
$user = "*****";
$host = "*****";
$password = "******";
$database = "******";

global $result;

$connection = mysql_connect($host,$user,$password) or die ("Couldn't connect to server.");
        $db = mysql_select_db($database,$connection) or die ("Couldn't select Intern database.");
$result = mysql_query($query) or die ("Couldn't execute query");

return $result;
}

?>[code]

[/code]

Link to comment
Share on other sites

I guess what im am thinking the problem is that if the user doesnt exist the query isnt working. However I thought it should just not be returning any rows, that way when I use mysql_num_rows() on it i should get 0. Instead it is just saying "Couldn't execute query"

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.