zain123 Posted June 2, 2008 Share Posted June 2, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/108392-looping-in-php/ Share on other sites More sharing options...
DarkWater Posted June 2, 2008 Share Posted June 2, 2008 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); } Quote Link to comment https://forums.phpfreaks.com/topic/108392-looping-in-php/#findComment-555669 Share on other sites More sharing options...
zain123 Posted June 2, 2008 Author Share Posted June 2, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/108392-looping-in-php/#findComment-555685 Share on other sites More sharing options...
discomatt Posted June 2, 2008 Share Posted June 2, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108392-looping-in-php/#findComment-555696 Share on other sites More sharing options...
.josh Posted June 2, 2008 Share Posted June 2, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108392-looping-in-php/#findComment-555698 Share on other sites More sharing options...
zain123 Posted June 2, 2008 Author Share Posted June 2, 2008 thanks everyone that helped a lot ..actually not coming from Java but they are being sent from Flash Quote Link to comment https://forums.phpfreaks.com/topic/108392-looping-in-php/#findComment-555707 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.