View Full Version : Tick Box, select and update database.

04-03-2007, 12:10 PM
Ok, I am trying to change the value in table with a tick box. If the tick box is selected it changes the value in the field to 'y' and if it is not selected it changes the field to 'n'.

First of all I need to bring back the current state of the field and then I need to be able to update it from 'y' to 'n' as many times as I want.


// Get the ID of the chosen thing and store it in a variable
$id = $_GET['id'];

// If the submit button is pressed, execute the update code
if (isset($_POST['Submit']))
// Store the new values inserted into the form in variables

// Store the update query string in a variable
$update="UPDATE user SET email='$u_email', mobile='$u_mobile', subscribedEmail='$u_sEmail', subscribedMobile='$u_sMobile' WHERE userID='$id'";

// Execute the query on the database
$updated = mysql_query($update);

// Output a confirmation message telling the user that the update was successful
echo 'Record Updated';
// Output error message if the update was not executed successfully
echo 'Error, Update not successful';


// Store the query string to select the chosen things details from the table
$query="SELECT * FROM user WHERE userID='$id'";

// Execute the query on the database
$result = mysql_query($query);

echo "<p class='leftmargin'><font face='arial'><a href='coursedocumentsstudent.php'>Course Documents ></a> Preferences</p>";
echo "<img src='images/seperator.jpg' width='100%' height='20'>";

// Store the results in an array and print them in a clear format
while ($value = mysql_fetch_array($result))
// Display the form with the returned data in it
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post" >';
echo '<table width="90%" align="center">';
echo '<tr><td><b>E-Mail Settings</b><br></td></tr>';
echo '<tr><td><font color="black" size="2px" face="arial">E-Mail Address:</td><td> <input type="text" name="uemail" value="'.$value[4].'" /></td></tr></table>';
echo '<br><img src="images/seperator.jpg" width="100%" height="20"><br>';
echo '<table width="90%" align="center">';
echo '<tr><td><b>Mobile Phone Settings</b><br></td></tr>';
echo '<tr><td><font color="black" size="2px" face="arial">Mobile Number:</td><td> <input type="text" name="umobile" value="'.$value[5].'" /></td></tr><br></table>';
echo '<p align="right"><input type="Submit" value="Update" name="Submit" /></form>';


There's my code and I need to add a checkbox with name="usubscribedEmail" and I guess it's the value of this tick box that I'm after.


name _F1
04-03-2007, 01:59 PM
Not really sure exactly what you're asking. You can check if a checkbox has been checked using isset(), as the variable is not sent if it is left unchecked.

Have you considered using a Boolean for the database column, rather than a string with two possible options? That is the purpose of Booleans, after all.

04-03-2007, 03:34 PM
Only thing I use booleans for now is my soft deletes. I use Yes/No now as its so much cleaner. You don't have to put in if($row['field'] == 1 echo Yes when you display it, you just display it :). Set your checkbox to Yes and if it's set the value will be yes, you will just need to put in if not set value = NO.

But I mainly use select dropdowns and not checkboxes unless I'm doing arrays. So boolean may work better for your ONE checkbox :)