Jump to content


Photo

Updating Mysql From CheckBoxes


  • Please log in to reply
1 reply to this topic

#1 JackJack

JackJack
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 14 April 2006 - 09:07 AM

I want it so that the boxes which are ticked will change a cell in there row to 'Yes'.

Here is my code so far but i don't think i'm doing it right

if($action == changebankallow){

echo "<br><br>Select Members:<br>";

echo "<form method='post' action=clan.php?action=allowmembers>";

            $members1 = mysql_query("select * from `players` where `clan_name` = '$clan[name]'order by `display_name` asc");
                while($member = mysql_fetch_array($members1)) {


if ($member[clan_bank] == Yes){
  $checked="checked";
}else{
  $checked="";
}

echo "<input type='checkbox' name='MEMBER_ARRAY[]' value='$member[id]' $checked> $member[display_name] ($member[clan_bank])<br>";
}
echo "<input type=submit value=Update>";

echo "</form>";

if($action == allowmembers){
if ($MEMBER_ARRAY)
{
$MEMBER = implode($MEMBER_ARRAY, ",");
$result = mysql_query ("UPDATE players SET clan_bank =Yes where id='$MEMBER'");
echo "Updated Successful";
if(!$result) 
{
echo "<B>UPDATE unsuccessful:</b> ", mysql_error();
exit;
}
}

Example
mysql_query ("UPDATE players SET clan_bank =Yes where id='box(es) that was ticked'");

So i'd want more than one update if you get what i meen.


JJ



#2 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 14 April 2006 - 01:38 PM

you are trying to create your array before the page is submitted. You have to submit the page then get the values into an array then run your query. This is how I have always done it.

if($action == changebankallow){
echo "<br><br>Select Members:<br>";
echo "<form method='post' action=clan.php?action=allowmembers>";
// Start count for values
$i = 1;
            $members1 = mysql_query("select * from `players` where `clan_name` = '$clan[name]'order by `display_name` asc");
                while($member = mysql_fetch_array($members1)) {
if ($member[clan_bank] == Yes){
  $checked="checked";
}else{
  $checked="";
}

echo "<input type='checkbox' name='mem".$i."' value='$member[id]' $checked> $member[display_name] ($member[clan_bank])<br>";
$i++;
}
echo "<input type=submit value=Update>";

echo "</form>";

if($action == allowmembers){
$access = '';
$MEMBER = array();
$initials = 'mem';
foreach ($_POST as $key => $value) {
  $access .= "$key=$value&";
  if (substr($key, 0, 3) == $initials) {
    $MEMBER[]= ''.$value.'';
}
}
$result = mysql_query ("UPDATE players SET clan_bank =Yes where id IN ($MEMBER);
echo "Updated Successful";
if(!$result) 
{
echo "<B>UPDATE unsuccessful:</b> ", mysql_error();
exit;
}
}

I have not tested all of your code so let me know if there is a problem

Ray




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users