Solved

mulitple submissions t the same time import into mysql?

Posted on 2011-02-20
7
190 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
showing loader for php/mysql/ajax live search 13 26
Decrypt string by php 7 31
MySQL Backup Strategy 15 25
Complex SQL statement in VB.NET 7 15
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

778 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