python72 Posted January 8, 2011 Share Posted January 8, 2011 Is it possible to use for loop to insert values into database using for loop? Or what would be the another way to insert these values into the database if the number of values being entered differs from one time to another? Here is basically what I am trying to do: mysql_query("INSERT INTO MaxMillionsNum SET Day='$weekday', DrawDate='$CompleteDate', DateTime='$timestamp', for($i=1; $i<=$SetNumber; $i+=1){ for($j=0; $j<7; j+=1){ $NumberName="Number".$i.$v[$j];//Generating name of database field $$NumberName=$Match[0][$j];//Here I would store the $Match value into the fieldname name created above } } Tries='$RunCounter'"); Quote Link to comment https://forums.phpfreaks.com/topic/223749-inserting-values-into-database-using-for-loop/ Share on other sites More sharing options...
trq Posted January 8, 2011 Share Posted January 8, 2011 You cannot use a PHP for loop within an SQL query. Use a loop to build your queries if necessary, but you can't actually use them in a query. Ive asked you this before but, why do your database fields all have id's appended to them? Quote Link to comment https://forums.phpfreaks.com/topic/223749-inserting-values-into-database-using-for-loop/#findComment-1156561 Share on other sites More sharing options...
python72 Posted January 8, 2011 Author Share Posted January 8, 2011 What do you mean by id's? Quote Link to comment https://forums.phpfreaks.com/topic/223749-inserting-values-into-database-using-for-loop/#findComment-1156711 Share on other sites More sharing options...
PFMaBiSmAd Posted January 8, 2011 Share Posted January 8, 2011 You have field names that are a series of named/numbered/named values. The reason thorpe asked is because that generally indicates you are trying to make a database act like a spreadsheet and this results in more code, hard to write queries, and poor performance. Quote Link to comment https://forums.phpfreaks.com/topic/223749-inserting-values-into-database-using-for-loop/#findComment-1156717 Share on other sites More sharing options...
python72 Posted January 9, 2011 Author Share Posted January 9, 2011 I figured name is a name, just trying to separate sets of numbers so they are easier to view when I look at the database. I guess the easiest way to have arrays but as far as I know it is not possible within mysql. Not sure what would be the best way to do it so that is what I chose to do. Quote Link to comment https://forums.phpfreaks.com/topic/223749-inserting-values-into-database-using-for-loop/#findComment-1156923 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.