Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

same variable multiple times in the same for import into MySQL

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
jaxbrian
Asked:
jaxbrian
  • 3
  • 3
1 Solution
 
Lukasz ChmielewskiCommented:
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
 
StraySodCommented:
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
 
jaxbrianAuthor Commented:
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
jaxbrianAuthor Commented:
StraySod
How would I keep the information together first,last and their email all in the same row doing it like that?
0
 
StraySodCommented:
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
 
jaxbrianAuthor Commented:
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
 
StraySodCommented:
you are welcome. glad to help
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now