Solved

same variable multiple times in the same for import into MySQL

Posted on 2011-03-06
7
321 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
[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
  • 3
7 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35048012
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
ID: 35048020
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
ID: 35048036
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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 2

Author Comment

by:jaxbrian
ID: 35048069
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
ID: 35055993
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
ID: 35116586
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
ID: 35117687
you are welcome. glad to help
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

738 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