?
Solved

mulitple submissions t the same time import into mysql?

Posted on 2011-02-20
7
Medium Priority
?
195 Views
Last Modified: 2012-05-11
what I am trying to do is grab multiple email addresses at the same time from the same form. I want to have multiple fields for the email addresses.
my question is, is it possible to post the same variable to my query at the same time?
like if the DB on has one row called `email` how would I post multiple emails at the same time? i can do it one at a time.
is it clear what I am asking about?
0
Comment
Question by:jaxbrian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Accepted Solution

by:
LAMASE earned 1000 total points
ID: 34939241
You can name your inputs like this

<input name="email[]" value="">
<input name="email[]" value="">
<input name="email[]" value="">

This will generate a request like

script.php?email[]=email@test.com&email[]=...
(the parentesis are encoded in the real url)

In this way you can read an array directly... for example in PHP you will receive an array that you can access with
$array_email = $_REQUEST['email']; // <- this is an array

or you can read the request string and do it by yourself.
0
 
LVL 2

Author Comment

by:jaxbrian
ID: 34939332
I am not sure I understand, I think I tried what you are saying to do but I have not been able to get it to work.
when I try to echo the array, it only says "array" I did this just as a test to see if there was any data there. when i submit the form with multiple emails it doesn't fail, it just doesn't insert them int to eh DB.


thank you, I will keep messing with it.
0
 
LVL 4

Expert Comment

by:LAMASE
ID: 34939422
Output you vars with
print_r($variable);

Open in new window


or

var_dump($variable);

Open in new window

0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 11

Expert Comment

by:Ovid Burke
ID: 34940697
Do you want to put multiple email addresses into one field in your database table? Or do you want to put them into separate records (rows)?

0
 
LVL 2

Author Comment

by:jaxbrian
ID: 34948027
I need them to go into separate rows.

thanks for your time
B
0
 
LVL 11

Assisted Solution

by:Ovid Burke
Ovid Burke earned 1000 total points
ID: 34948385
Try:

 
<?php

if(isset($_POST['submit']) && $_POST['submit'] = "Submit") { // check the form is submitted
	
	unset($_POST['submit']); // destroy the submit button
	
	if(is_array($_POST['email'])) {
		
		$arr_emails = $_POST['email']; // array of email addresses from form
		
		$arr_reject = array(); // array to hold bad or malformed addresses
	
		foreach($arr_emails as $email) { // loop through the array
			if(!is_email($email)) { // if not a valid email...
				$arr_reject[] = $email; // ... add to rejected emails array
			} else { // or perform database insert
				@mysql_query("INSERT INTO _tablename (emailfield) VALUES ('$email')") or die(
				"ERROR INSERTING EMAIL ($email):<br />"
				. mysql_error());
			}
		}
		
		// print out rejected email addresses
		if(!empty($arr_reject)) { 
			echo "THE FOLLOWING EMAILS WERE REJECTED: <br />";
			foreach($arr_reject as $reject) {
				echo "- $reject <br />";	
			}
		}
		
	} else {
		
		echo "Please specify at least one email address.";	
	
	}
	
}

// functions to validate email addresses
function is_host($host) {
	$dns = checkdnsrr($host);
	if(!($dns == 1)) {
		return false;
	}
	return true;
}
function is_email($email) {
	if(!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
		return false;
	}
	list($userid, $domain) = explode("@", $email);
	$userid_array = explode(".", $userid);
	for($i = 0; $i < sizeof($userid_array); $i++) {
		if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $userid_array[$i])) {
    		return false;
    	}
	}
	if (!ereg("^\[?[0-9\.]+\]?$", $domain)) { 
    	$domain_array = explode(".", $domain);
    	if (sizeof($domain_array) < 2) {
       		return false;
    	}
    	for ($i = 0; $i < sizeof($domain_array); $i++) {
      		if(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
        		return false;
      		}
    	}
		if(!is_host($domain)) {
			return false;
		}
  	}
	return true;
}

?>

Open in new window

0
 
LVL 2

Author Comment

by:jaxbrian
ID: 35047422
I could not get that script to work for the submission of emails. I am going to award points for your help with this. I realize now that I really need three form fields to be imported to MySQL. $first, $last, and $email and I guess that is really a new question.
I have tried to work with a foreach() and I just cant seem to get that to work either

thank you for every ones help with this
0

Featured Post

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question