Jump to content

[SOLVED] Update/Edit Database using checkbox


Ell20

Recommended Posts

Hi,

 

I have a simple checkbox for the user to say whether or not their club has a pavilion. Once this has been stored I would like the page to check the database to see whether or not the checkbox should be checked.

 

I tried

<input type="checkbox" name="pavilion" value="<?=$pavilion?>" />

 

But this didnt work, is there anyway I can store a simple "Yes" to the database once ticked and then make the box ticked each time the user views the page?

 

Thanks

Link to comment
Share on other sites

It would be something like this

 

<?php

if (isset($_POST['submit'])){
   
   //check if they checked that they have a pavilion
   if (isset($_POST['pavilion'])){
      
      //update the database with "yes"
      $query = mysql_query("UPDATE table SET pavilion='yes' WHERE pavilion='{$_POST['pavilion']}'")or die(mysql_error());
   }
   
}

//check the databse to figure out if the field should be checked
$query = mysql_query("SELECT pavilion FROM table WHERE pavilion='$pavilion'")or die(mysql_error());
$row = mysql_fetch_assoc($query);

if ($row['pavilion'] == 'yes') $checked = "checked='yes'";
else $checked = "";
      
?>


<form method="post">
   <input type="checkbox" name="pavilion" value="<?=$pavilion?>" <?=$checked?>/><br>
   <input type="submit" name="submit">
</form>

 

Just edit the database information and see if it works for you.

Link to comment
Share on other sites

I have managed to do it for all 10 now and it works at first however, if for some reason any of the checkboxes need to be unticked then submitted again the check box is not updating and stays checked regardless?

 

Thanks

Link to comment
Share on other sites

Makes sense since here

 

//check if they checked that they have a pavilion
   if (isset($_POST['pavilion'])){
      
      //update the database with "yes"
      $query = mysql_query("UPDATE table SET pavilion='yes' WHERE pavilion='{$_POST['pavilion']}'")or die(mysql_error());
   }

 

is checking if its set, if its not set (unchecked), the update query gets skipped.  Put an else in there to set it to "No" if its not set.

Link to comment
Share on other sites

Like this:

 

if (isset($_POST['submit3'])){
   
   if (isset($_POST['pavilion'])){
      $query = mysql_query("UPDATE club SET pavilion='yes' WHERE pavilion='{$_POST['pavilion']}' AND club_id='$id'")or die(mysql_error());
   } else {
   	  $query = mysql_query("UPDATE club SET pavilion='no' WHERE pavilion='{$_POST['pavilion']}' AND club_id='$id'")or die(mysql_error());
   }
}

Link to comment
Share on other sites

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.