Jump to content

Recommended Posts

Hi my code is as follows and it works fine..but how would i put the insert statements in a loop? Thanks in advance:

 

$name0 = "hello";
$email0 = "[email protected]";
$name1 = "jello";
$email1 = "[email protected]";

$insertSQL = "INSERT INTO players(name, email, date) VALUES ('$name0', '$email0', NOW())";
$rs = mysql_query($insertSQL,$conn);

$insertSQL = "INSERT INTO players(name, email, date) VALUES ('$name1', '$email1', NOW())";
$rs = mysql_query($insertSQL,$conn);

Link to comment
https://forums.phpfreaks.com/topic/108392-looping-in-php/
Share on other sites

Use an array.  And it's pretty simple if you're only having two elements, actually.

 

$players = array("hello" => "[email protected]", "jello" => "[email protected]");
foreach ($players as $name=>$email) {
  $insertSQL = "INSERT INTO players(name, email, date) VALUES ('$name', '$email', NOW())";
  $rs = mysql_query($insertSQL,$conn) OR die(mysql_error);
}

Link to comment
https://forums.phpfreaks.com/topic/108392-looping-in-php/#findComment-555669
Share on other sites

thanks for that dark water ...basically I'm gonna get about 24 name/email pairs POSTED to the php file.

 

example

 

$name0= $POST["name0"];
$name0= $POST["name0"];

$name1= $POST["name1"];
$name1= $POST["name1"];

$name2= $POST["name2"];
$name2= $POST["name2"];

//and so on for 24 pairs

 

How would I actually put all these values into the array to start with...

 

Also just for  learning purposes if i wasnt to use an array ,how would i use a loop to insert it into the database:

 

I tried something like:

 

<?php 
$i=0;
while($i<=24)
  {
  $insertSQL = "INSERT INTO players(name, email, date) VALUES ('$name+$i', '$email+$i', NOW())";
  $rs = mysql_query($insertSQL,$conn);
  }
?>

 

 

but the above doesnt seem to work..

 

Thanks for your help. appreciate it

 

 

Link to comment
https://forums.phpfreaks.com/topic/108392-looping-in-php/#findComment-555685
Share on other sites

Ahh, coming from java I'm guessing? :)

 

To combine strings and variables you use a dot ( period )

 

Ie:

 

$foo = 'Hello ';
$bar = 'World';

echo $foo . $bar;

 

Returns 'Hello World'

 

In your case, you want variable variable names...

 

<?php

$test = 'bar';

$foobar = 'Hello World';

$var = 'foo' . $test;

echo $$var;

# echo $foo$test will not work
# Alternatively, if working in the global
# scope. you could also use:

echo $GLOBALS[ 'foo' . $test ]

?>

 

The easiest way to do this is to simply loop through your $_POST variable with a foreach. If we can see your form we can help you get a good loop to pull that data.

Link to comment
https://forums.phpfreaks.com/topic/108392-looping-in-php/#findComment-555696
Share on other sites

how is your form setup? Are there just 24 pairs of name/email fields? It would be easier if you made their names an array to be posted, that way you can loop through them

 

echo "<form action = 'somewhere.php' method = 'post'>";
for ($x = 0;$x <24; $x++) {
   echo "Name $x <input = 'text' name = 'name[]'> <br />";
   echo "Email $x <input = 'text' name = 'email[]'> <br />";
   echo "<br />";
} // end for $x
echo "<input = 'submit' value = 'submit'>";
echo "</form>";

 

somewhere.php

$name = $_POST['name'];
$email = $_POST['email'];

$x = 0;
while ($name[$x]) {
   $insertSQL = "INSERT INTO players(name, email, date) VALUES ('{$name[$x]}', '{$email[$x]}', NOW())";
   $rs = mysql_query($insertSQL,$conn);
   $x++;
}

 

You should of course sanitize your variables before inserting them into your db.  Also, this script assumes that all of your fields will be completely filled out, so you'll probably want to add some conditions to check and do something about that if they aren't.

Link to comment
https://forums.phpfreaks.com/topic/108392-looping-in-php/#findComment-555698
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.