Solved

same variable multiple times in the same for import into MySQL

Posted on 2011-03-06
7
319 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
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
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 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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 the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

821 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