Jump to content

Inserting Information To Sql With While Loop


Mariius

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.