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

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.

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.

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());
   }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.