开发者

Put more than 1 row into mysql from a php array variable in an html form - not working with []?

开发者 https://www.devze.com 2023-04-02 10:30 出处:网络
The line below is part of a form.When the form is submitted, it enters a row into a mysql table. $varw has to go in different numbers of rows.Sometimes there are a lot of $varw\'s.I tried changing \'

The line below is part of a form. When the form is submitted, it enters a row into a mysql table.

$varw has to go in different numbers of rows. Sometimes there are a lot of $varw's. I tried changing 'Weight' to 'Weight[]' in the line below only. That entered 0 (wrong) for $varw in mysql. And it still only added 1 row.

开发者_开发技巧Print "<input type='hidden' name='Weight' value=" . $varw . ">";


You need iteration! In other words .. a loop.

Heavily commented untested example below:

$i = 0; // Needed to have different variable names
foreach($varw as $var){
   print "<input type=\"hidden\" name=\"". weight{$i} . "\" value=\"" . $var . "\">";
   $i++; // increment counter on each iteration ... 
}

//  You will now have as many hidden fields as $varw's.

I am leaving you the pleasure of thinking up the loop that will do the insertions in database.

Read the manual on for loops,

Happy coding friend.

PS: Please, don't start variable names and function names with capital letter.


Use the Mysql deleminator ';' to seprate two queries and use it in a loop


You could always use JSON or comma separates values (via implode())

Print "<input type='hidden' name='Weight' value=" . json_encode($varw) . ">";

On your PHP side when you receive this you could use json_decode() back into an array or use explode() via "," to split it into array if you decided on using comma separated values.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号