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

Link to comment
Share on other sites

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.

Link to comment
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.