php - Processing an unknown amount of checkboxes -



php - Processing an unknown amount of checkboxes -

i'm generating unknown amount of checkboxes in form using mysql, number vary,

$frinfoq = mysql_query($frinfo) or die (mysql_error()); while($frow = mysql_fetch_assoc($frinfoq)) { $username = $frow['username']; $ct = $frow['country']; $fruuid = $frow['uid']; ?> <tr><td><p><?php echo $username; ?></p></td><td><p><?php echo $ct; ?></p></td><td><form method="post" action="<?php echo $_server['php_self']; ?>" id="delf"><input type="hidden" value="<?php echo $fruuid; ?>" /><input type="checkbox" name="add[]" value="<?php echo $fruuid; ?>" id="a_t_game" /><form></td></tr> <?php } ?>

when form submitted , processed "create.php" it's supposed add together checked users mysql table, row per user, determine (i'm not sure if works due problem i'm to) checked this:

if($_post['add'] == true) { $user_uid = $_post['add']; }

i seek add together rows so:

$arr = array($user_uid); foreach($arr $user_uid) { $game = "insert wd_game (game_uid,user_uid,lastmove,startcountry) values ('$gid','$user_uid',now(),'none')"; $gameq = mysql_query($game) or die (mysql_error()); }

all of info inputs fine apart user uid set "array". creates 1 row, , need row per user.

i know it's problem way array beingness processed, that's pretty obvious, haven't foggiest thought how prepare it. help/pointers great help!

$arr = array($user_uid); problem. seek $arr = $user_uid;, or cutting out middleman , utilize $arr = $_post["add"];.

edit

there number of other things improved code. one, it's hard read. suggest cleaning bit, , avoid jumping between html , php.

another thing should escape info going database perchance come user input (like $_post). should utilize mysql_real_escape_string.

a 3rd thing form action pointing $_server["php_self"] not safe. vulnerable cross-site scripting (xss). see this blog more detailed description on vulnerability.

i suggest more this:

<?php if ($_post["add"]) { $arr = $_post["add"]; foreach ((array)$arr $user_uid) { $game = "insert wd_game (game_uid,user_uid,lastmove,startcountry) values ('". mysql_real_escape_string($gid) ."','". mysql_real_escape_string($user_uid) ."', now(), 'none')"; $gameq = mysql_query($game) or die (mysql_error()); } } $frinfoq = mysql_query($frinfo) or die (mysql_error()); while($frow = mysql_fetch_assoc($frinfoq)) { $username = $frow['username']; $ct = $frow['country']; $fruuid = $frow['uid']; echo " <tr> <td> <p>{$username}</p> </td> <td> <p>{$ct}</p> </td> <td> <form method=\"post\" action=\"". htmlentities($_server['php_self']) ."\" id=\"delf\"> <input type=\"hidden\" value=\"{$fruuid}\" /> <input type=\"checkbox\" name=\"add[]\" value=\"{$fruuid}\" id=\"a_t_game\" /> <form> </td> </tr>\n"; } ?>

php mysql arrays checkbox html-form

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -