Page 1 of 1

Array checkbox problem

Posted: Tue Jan 06, 2009 11:25 am
by Addos
Hi,
I’m trying to update my database with the word ‘single’ if the checkbox is ticked in the Form of the coed below but unfortunately it’s not updating it for me and is setting all the values in the ‘menu’ column on the database to 0.

Can anyone help me to figure out what I’m missing?
Thanks very much

Code: Select all

for($i=0; $i < $totalRows_GetMenuUpdate; $i++) {
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
if($_POST["menu_bf"][$i] == ""){ $_POST["menu_bf"][$i] = 0;}
      $updateSQL = sprintf("UPDATE tbl_prdt
       SET menu='%s'
       WHERE cate_ID='%s'",
       $_POST['menu_bf'][$i],
       $_POST['id_bf'][$i]);
    
  mysql_select_db($database_, $);
  $Result1 = mysql_query($updateSQL, $) or die(mysql_error());}
?>
  
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
    <table align="center">
    
      <tr valign="baseline">
        <td><strong> title:</strong></td>
        <td><strong> Tick</strong></td>
      </tr>
        <?php do { ?>
      <tr valign="baseline">
        <td><?php echo $row_GetMenuUpdate['category_Name']; ?></td>
        <td>
 
        <!-- if I use this to phsycially add the details it works so I know that the database query is working<input name="menu_bf[]" type="text"  value="<?php //echo $row_GetMenuUpdate['menu']; ?>" size="3">-->
 
 
  <input type="checkbox" name="menu_bf[]"
  value="<?php echo $row_GetMenuUpdate['menu']; ?>"
   <?php  if($row_GetMenuUpdate['menu'] == 'single'){ 
 echo "checked=\"checked\"" .'/>'.'</td>'.'</tr>'; ?>
 
        <?PHP } else { 
        echo  '/>'.'</td>'.'</tr>'; 
        } // End $row_GetDates['archived']
        ?>       
            
        </td>
      </tr>
          
    <input type="hidden" name="id_bf[]" value="<?php echo $row_GetMenuUpdate['category_ID']; ?>">
      <?php } while ($row_GetMenuUpdate = mysql_fetch_assoc($GetMenuUpdate)); ?>
      <tr valign="baseline">
        <td><input type="submit" value="Update record"></td>
        <td>&nbsp;</td>
      </tr>
    </table>
<input type="hidden" name="MM_update" value="form1">
  </form>

Re: Array checkbox problem

Posted: Tue Jan 06, 2009 6:17 pm
by it2051229
the way i see it you only have a single checkbox and is inside the loop.

do :

Code: Select all

 
$_POST['menu_bf'][0]; // instead of  $_POST['menu_bf'][$i]
 

Re: Array checkbox problem

Posted: Wed Jan 07, 2009 2:58 am
by Addos
Thanks for the reply but unfortunately that made no difference. I do have only one checkbox in that form but it returns a good few checkboxes from the database.

This is a sample of the page when it loads.

Code: Select all

CELTIC TIGER
<input type="checkbox" name="menu_bf[]"  value="single"checked="checked"/>
<input type="hidden" name="id_bf[]" value="5">
 
BRASS
<input type="checkbox" name="menu_bf[]"  value="single" checked="checked"/>
<input type="hidden" name="id_bf[]" value="8">
 
SOLO VOICE
<input type="checkbox" name="menu_bf[]"  value="0"
<input type="hidden" name="id_bf[]" value="9">
One thing I have just noticed when the page loads is that when I un tick a checkbox that is set in the database and hit 'Update Record' it will set the value to 0 in the database but if I try to then go back a tick another box (or the same one) it won't pass that 'ticked' value to the database. So something is getting through with this code but not it all.

Also when the page loads and there is say one checkbox ticked if I leave this alone and hit update it stays ticked.
I'm very puzzled.

Thanks for any help.

Re: Array checkbox problem

Posted: Wed Jan 07, 2009 3:25 am
by mattpointblank
What field type is the checkbox in the database? If it'll accept a 0 but not a string, maybe it's set as an integer or a boolean?

Re: Array checkbox problem

Posted: Wed Jan 07, 2009 3:54 am
by it2051229
i'm quite puzzled with the flow of your code. Checked multiple checkbox that is inside the same form tag are placed in an array when the submit button is triggered. And when you want to access those values you have to use a loop to get all the values that is inside that array. I'm confused with this variable "$totalRows_GetMenuUpdate" which I think is not the size of the array of checkbox values. This might lead to an array index out of bounds(in some other programming languages) or misbehaving code (php).

Re: Array checkbox problem

Posted: Wed Jan 07, 2009 4:58 am
by Addos
Thanks a mil for the reply.

I’m still struggling to learn all this stuff and appreciate your answer. I had another form that I took this from and I was using this to sort asc and desc the order of items listed in the database. It worked perfectly and the code I posted above will work in the same manner if I replace

Code: Select all

 <input type="checkbox" name="menu_bf[]"  value="<?php echo $row_GetMenuUpdate['menu']; ?>"
   <?php  if($row_GetMenuUpdate['menu'] == ‘single’){ 
 echo "checked=\"checked\"" .'/>'.'</td>'.'</tr>'; ?>
 
        <?PHP } else { 
         echo '/>'.'</td>'.'</tr>';  
         
        } // End $row_GetDates['archived']
        ?>      

With

Code: Select all

<input name="menu_bf[]" type="text"  value="<?php //echo $row_GetMenuUpdate['menu']; ?>" size="3">
The problem of course is that I have to do this by hand i.e. add ‘single’ to the text form field if I want to add the correct details to the database however it’s obvious that this is not acceptable as the client would run amuck with this. I thought it would be easy to add a checkbox but obviously this is proving more confusing to my knowledge of PHP but I know I’m close.
Thanks again for any help

Re: Array checkbox problem

Posted: Wed Jan 07, 2009 7:24 pm
by it2051229
i'm still confused.. can you tell me what this variable is for?

I thought this was " $totalRows_GetMenuUpdate" an integer cause you used it to compare in a for loop (condition)
for($i = 0; $i < $totalRows_GetMenuUpdate; $i++)

now it became an associative array?
if($totalRows_GetMenuUpdate['menu'] == 'single')

Re: Array checkbox problem

Posted: Thu Jan 08, 2009 5:01 am
by Addos
Thanks for the reply.
I thought I needed to run my first query to get the data from the database and pass this to the Form so that the checkboxes were populated correctly. Then if the client changes the values of the checkboxes I need to pass this back to the database when the Form is submitted. This is why I thought I needed to loop through the query using for($i=0; $i < $totalRows_GetMenuUpdate; $i++) { to pass and update the database.

This is the entire page of code that I’m using so maybe this will give a little more info.

As I say if I simply remove the "checked=\"checked from the Form below then it all works ok but then it would have to be manually changed.

Thanks again

Code: Select all

<?php
mysql_select_db($database_, $);
$query_GetMenuUpdate = "SELECT * FROM prdts
WHERE archive = 0 ";
$GetMenuUpdate = mysql_query($query_GetMenuUpdate, $) or die(mysql_error());
$row_GetMenuUpdate = mysql_fetch_assoc($GetMenuUpdate);
$totalRows_GetMenuUpdate = mysql_num_rows($GetMenuUpdate);
 
 $editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}  
for($i=0; $i < $totalRows_GetMenuUpdate; $i++) {
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
if($_POST['menu_bf'][$i] == ""){ $_POST['menu_bf'][$i] = 0;}
      $updateSQL = sprintf("UPDATE prdts
       SET menu='%s'
       WHERE cat_ID='%s'",
       $_POST['menu_bf'][$i],
       $_POST['id_bf'][$i]);
       
  mysql_select_db($database_, $);
  $Result1 = mysql_query($updateSQL, $) or die(mysql_error());
 
  $updateGoTo = "menuupdate.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo)); 
  }
} 
?>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
    <table align="center">
      
      <tr valign="baseline">
        <td><strong> title:</strong></td>
        <td><strong>Checkbox:</strong></td>
      </tr>
        <?php do { ?>
      <tr valign="baseline">
        <td><?php echo $row_GetMenuUpdate['cat_Name']; ?></td>
        <td>
          <input type="" name="menu_bf[]"  value="<?php echo $row_GetMenuUpdate['menu']; ?>"
   <?php  if($row_GetMenuUpdate['menu'] == '1'){ 
 echo "checked=\"checked\"" .'/>'.'</td>'.'</tr>'; ?>
 
        <?PHP } else { 
         echo '/>'.'</td>'.'</tr>';  
         
        } // End $row_GetDates['archived']
        ?>       
          
        </td>
      </tr>
         <?PHP // echo print_r($_POST); ?>
    <input type="hidden" name="id_bf[]" value="<?php echo $row_GetMenuUpdate['cat_ID']; ?>">
      <?php } while ($row_GetMenuUpdate = mysql_fetch_assoc($GetMenuUpdate)); ?>
      <tr valign="baseline">
        <td><input type="submit" value="Update record"></td>
        <td>&nbsp;</td>
      </tr>
    </table>
<input type="hidden" name="MM_update" value="form1"> 
  </form>
 

Re: Array checkbox problem

Posted: Thu Jan 08, 2009 7:11 am
by it2051229
I think you're doing it the hard way, you don't need to query all the products that have "0" menu and use loop to the queried products and one by one updating........ but instead you just use a single query like

Code: Select all

 
/*
* I am assuming here that you only have one checkbox and one hidden input type that's why I used "ZERO"
* on the menu bf and id bf
*/
$menuBf  = $_POST["menu_bf"][0];
$idBf = $_POST["id_bf"][0];
 
mysql_query("UPDATE prdts SET menu='".$menuBf."' WHERE cat_id='".$idBf."' AND archive=0") or die(mysql_error());