Solved

same variable multiple times in the same for import into MySQL

Posted on 2011-03-06
7
316 Views
Last Modified: 2012-05-11
I am creating  a form that signs up multiple people at a time. firstName, lastName, and Email are the fields in the form.

I am trying to import it into mysql to save in the table as separate entries. I have tried to do a foreach() but I can not seem to get that to work for the multiple variables. I am not sure how I can use $_REQUEST to get all the different variables into one array or if that would even work.

any input would be greatly appreciated, thank you in advance

JaxB
0
Comment
Question by:jaxbrian
  • 3
  • 3
7 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
Use POST for variables from the form. Then you just use
insert into table (firstname, lastname, email) values ('$_POST[firstname]','$_POST[lastname]','$_POST[email]');
Of course you have to set <form method="post" ...>
0
 
LVL 7

Expert Comment

by:StraySod
Comment Utility
Hi,

I think you would need to declare the names of your inputs as an array. If that's what you need than try something like this:

<input name="email[]" type="text" />

and process it like this:

foreach ($_POST['email'] as $email) {
  //do what you need with $email
}

you can use $_REQUEST as well
0
 
LVL 2

Author Comment

by:jaxbrian
Comment Utility
that wont work because there are multiple values for $_POST[firstname]','$_POST[lastname]','$_POST[email]'

there needs to be some kind of loop that I can not figure out

thank you
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 2

Author Comment

by:jaxbrian
Comment Utility
StraySod
How would I keep the information together first,last and their email all in the same row doing it like that?
0
 
LVL 7

Accepted Solution

by:
StraySod earned 500 total points
Comment Utility
It would help to see the HTML for your form. I suppose it looks similar to the following:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <input name="first[]" type="text" />
  <input name="last[]" type="text" />
  <input name="email[]" type="text" /><br />
  
  <input name="first[]" type="text" />
  <input name="last[]" type="text" />
  <input name="email[]" type="text" /><br />

  <!--...some more inputs...-->
</form>

Open in new window


Processing of such a form should look similar to this:  
$cnt = count($_POST['first']);

for ($i = 0; $i < $cnt; $i++) {
  //calling echo is just an example, you can do whatever you want with the vars
  //you should also probably do some input validation and prevent SQL injection before passing the values to the DB server
  echo $_POST['first'][$i] . " | " $_POST['last'][$i] . " | " $_POST['email'][$i] . "<br />";
}

Open in new window


hope this helps, if you'd need any more assistance, let us know.
0
 
LVL 2

Author Comment

by:jaxbrian
Comment Utility
thank you very much I think that is exactly what I was looking for. sorry about the long delay, my comp was having some issues for a couple of days.

thank you very much
jaxBrian
0
 
LVL 7

Expert Comment

by:StraySod
Comment Utility
you are welcome. glad to help
0

Featured Post

How to run any project with ease

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

Suggested Solutions

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

10 Experts available now in Live!

Get 1:1 Help Now