Mariius Posted December 2, 2012 Share Posted December 2, 2012 I would appreciate if you could help me once again: this time I am trying to insert values to my sql. $k=1; while($k<=$category[0]) { $baze="INSERT INTO Mine (date, category, cost) VALUES (CURDATE(),$category[$k],$cost[$k])"; $k++; } Here $category[0] is the amount of froms that have been printed out. I get this error: query was empty Quote Link to comment https://forums.phpfreaks.com/topic/271481-inserting-information-to-sql-with-while-loop/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 2, 2012 Share Posted December 2, 2012 For your existing code to work, the execution of the query would need to be inside the loop. It's apparently outside of and after the end of the loop. Since it is not inside of the loop, best guess is your $category[0] value isn't what you expect and the loop is being skipped over. There's a couple of problems with what you are doing. 1) You shouldn't execute a query inside of a loop. For inserting multiple rows, there's a mulit-value insert query. 2) You shouldn't need to carry around a count of the number of items in a form, because you should use an array(s) for the form fields, which would let you use php's array functions to iterate over the submitted form data. The snippet of code you posted doesn't show us enough information to help further, either with the error you got or with the things I mentioned. Quote Link to comment https://forums.phpfreaks.com/topic/271481-inserting-information-to-sql-with-while-loop/#findComment-1396914 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.