Jump to content

Recommended Posts

Dear All

 

I've 15 records displaying in a page. Each record has a field called "Sports" which stores values separated by semicolon ";" "Football;Cricket;Basketball"

 

When I'm retrieving the values, i use explode and assign each values of check box. Works perfect with No Problem.

 

I'm storing the Record ID as a hidden field.

 

I'm the admin and I uncheck "Football" or "Basketball" in couple of records and click on Submit and give the following code

 

if ($action == "submit")
{
   for ($j=0; $j < count($recordid); $j++)  
   {
     if (count($sports[$j]) == 0) $sportslist = "None";
      else
       {
         for ($i=0; $i < count($sports[$j]); $i++)
         {
           $sportslist .= $sports[$i].";";
         }                        
      }                

   $utpquery = "UPDATE user_sports SET sports='$sportslist' where urecord = $recordid[$j]";
   $utpresult = mysql_query($utpquery) or die("Query failed 1");
  }

header("Location: verifyrecord.php");
exit;
}

 

Once I execute the above, I check the Database and find the ones which I had Unchecked have become in Multiples

For e.g.

"Basketball;Basketball;Basketball;Basketball;Basketball;"

 

I don't know what am I doing wrong here.

 

Could anyone give me a Hint???

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/56946-solved-multiple-checkbox-problem/
Share on other sites

Hi I did the Print

 

it comes following

id followed by the checkbox selection.

It never adds the Basketball or Cricket though it is ticked.

 

12

Football;

13

Football; Football;

14

Football;Football;Football;

15

Football;Football;Football;Football;

 

I'm going nuts over this.

Hi Thanks for your prompt reply

 

Here is the section that works with no hitch gives me all the selection as per the user had done while filling up the form initially

 

<form name="frm1" method="post" action="sports.php?page=update">
<table cellspacing="0" cellpadding="0" border="0">
<?
$squery = "select * from user_sports";
$sresult = mysql_query($squery) or die ("Query Failed");
while( $row = mysql_fetch_array( $sresult ) )
{
$sports = explode(";",$row['sports']);			
?>
        <tr> 
          <td>
            <input name="recordid[]" type="hidden" value="<? echo $row['urecord'] ?>">
          <input name="sports[]" type="checkbox" id="sports[]" value="Football" <? if (is_array($sports) == TRUE) { if (in_array("Football", $sports)) echo "CHECKED"; } ?>>
              Football<br> <input name="sports[]" type="checkbox" id="sports[]" value="Cricket" <? if (is_array($sports) == TRUE) { if (in_array("Cricket", $sports)) echo "CHECKED"; } ?>>
              Cricket <br> <input name="sports[]" type="checkbox" id="sports[]" value="Basketball" <? if (is_array($sports) == TRUE) { if (in_array("Basketball", $sports)) echo "CHECKED"; } ?>>
              Basketball 
              </td>
        </tr>
<?
}
?>
</table>
</form>

 

Here is the section which does not work, for which i need to be able to update.

 

<?
if ($page == "update")
{
   for ($j=0; $j < count($recordid); $j++)  
   {
     if (count($sports[$j]) == 0) $sportslist = "None";
      else
       {
         for ($i=0; $i < count($sports[$j]); $i++)
         {
           $sportslist .= $sports[$i].";";
         }                        
      }                

	echo $recordid[$j];
	echo "<br>";
	echo $sportslist;
	echo "<br>";

    //$utpquery = "UPDATE user_sports SET sports='$sportslist' where urecord = $recordid[$j]";
    //$utpresult = mysql_query($utpquery) or die("Query failed 1");
 } 
//header("Location: sports.php");
//exit;
}
?>

 

Please let me know where it is going wrong

this is the problam try a diffrent loop ok

<?php

for ($j=0; $j < count($recordid); $j++)  
   {
     if (count($sports[$j]) == 0) $sportslist = "None";
      else
       {
         for ($i=0; $i < count($sports[$j]); $i++)
         {
           $sportslist .= $sports[$i].";";
         }                        
      }
?>

     

1st change your form

<form name="frm1" method="post" action="sports.php?page=update">
<table cellspacing="0" cellpadding="0" border="0">
<?
$squery = "select * from user_sports";
$sresult = mysql_query($squery) or die ("Query Failed");
$number = 0;
while( $row = mysql_fetch_array( $sresult ) )
{
$sports = explode(";",$row['sports']);			
?>
        <tr> 
          <td>
            <input name="recordid[$number]" type="hidden" value="<? echo $row['urecord'] ?>">
          <input name="sports[$number][]" type="checkbox" id="sports[]" value="Football" <? if (is_array($sports) == TRUE) { if (in_array("Football", $sports)) echo "CHECKED"; } ?>>
              Football<br> <input name="sports[$number][]" type="checkbox" id="sports[]" value="Cricket" <? if (is_array($sports) == TRUE) { if (in_array("Cricket", $sports)) echo "CHECKED"; } ?>>
              Cricket <br> <input name="sports[$number][]" type="checkbox" id="sports[]" value="Basketball" <? if (is_array($sports) == TRUE) { if (in_array("Basketball", $sports)) echo "CHECKED"; } ?>>
              Basketball 
              </td>
        </tr>
<?
$number++;}
?>
</table>
</form>

I don't think your update query will work , unless it is something like this

UPDATE user_sports SET sports='$sportslist' where urecord = '$recordid[$j]';

or

UPDATE user_sports SET sports='$sportslist' where urecord = {$recordid['$j]'};

might be missing something? ::)

 

can you insert print_r($_POST); and post the output

i think thet you need somethin like this

<?
if ($page == "update")
{
   for ($j=0; $j < count($recordid); $j++)  
   {
     if (count($sports[$j]) == 0) $sportslist = "None";
      else $sportslist = implode(';', $sports[$j]);
//       {
//         for ($i=0; $i < count($sports[$j]); $i++)
//         {
//           $sportslist .= $sports[$i].";";
//         }                        
//      }                

	echo $recordid[$j];
	echo "<br>";
	echo $sportslist;
	echo "<br>";

    //$utpquery = "UPDATE user_sports SET sports='$sportslist' where urecord = $recordid[$j]";
    //$utpresult = mysql_query($utpquery) or die("Query failed 1");
 } 
//header("Location: sports.php");
//exit;
}
?>

Thanks a million Sasa, it works like a charm...

 

Array

(

    [recordid] => Array

        (

            [0] => 1

            [1] => 2

        )

 

    [sports] => Array

        (

            [0] => Array

                (

                    [0] => Cricket

                    [1] => Basketball

                )

 

            [1] => Array

                (

                    [0] => Basketball

                )

 

        )

 

    [submit] => submit

)

 

How it works is by using Multi Dimensional Arrays. Cool I've understood how this works perfectly.

 

Amazing stuff!!!

 

Thanks a lot

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.