0

I am taking values from my form that has 39 fields, all fields have name's order as

field1,field2,field3 ....... 

and my sql table mytable also have 39 fields with the same name i.e. field1,field2,field3 .......

Now in the submit.php I am looking for a solution where I don't want to write all these 39 variables to store in mytable i.e. INSERT INTO mytable VALUES(field1,field2,field3,......)

But I am looking for a solution in which I don't have to write all these 39 variables and still I can store them in different 39 fields of sql. What I've tried is:

$query=mysql_query("INSERT INTO mytable VALUES (
".
for ($k=1; $k<=39; $k++)
{

$vari=$_POST["field".$k];   


$Temp_previous_total++;

}
.")");
1
  • Is there a question in there somewhere? Is it why your example code didn't work, for which there a various reasons :P. Commented Jan 9, 2013 at 17:20

2 Answers 2

2
$myvars = '';
for ($k=1; $k<=39; $k++)
{
   $myvars .= "'".$_POST["field".$k]."',";   
}
$myvars = substr($myvars,0,-1);
$query = "INSERT INTO mytable VALUES (
".$myvars.")";
mysql_query($query);
Sign up to request clarification or add additional context in comments.

6 Comments

This is how your code should work. But keep in mind this is very bad practice as mentioned by keeg
@JonStirling there are commas separating in the for loop?
Ugh, talk about me getting the wrong end of the stick with my last comment :/. Anyway, might be worth assuming that the OPs fields are actually text inputs and quote accordingly but meh, it's not specifically an issue without more context.
Column count doesn't match value count at row 1 , I counted both of them are 39 39
@Programer I also updated the code to reflect the quotes suggested by Jon, try if that works.
|
1

You should definitely sanitize those fields before you insert them, here is a really helpful class for mysql queries: PHP MySQL wrapper v3 that does exactly that

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.