Solved

same variable multiple times in the same for import into MySQL

Posted on 2011-03-06
7
322 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

627 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