Solved

mulitple submissions t the same time import into mysql?

Posted on 2011-02-20
7
192 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 250 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 250 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

733 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