Solved

mulitple submissions t the same time import into mysql?

Posted on 2011-02-20
7
187 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now