I have a site that im trying to put together and need some help with a checkbox. I have a form where the user enters data and when the user is finished they click on the submit btn witch populates the database. I want to enter a check box into the form that the user will check if they want to. My problem is understanding the whole check box this with the database. I made a field in the table that is a tinyint. I just need the to figure out the php code that goes into the form to update the database.

<html>
<head>
<basefont face="Arial">
</head>
<body bgcolor="white">

<?php
$intPriceBasedOn = (int) $_POST['PriceBasedOn'];
if (!isset($_POST['submit'])) {
// form not submitted
?>

    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
      <p>School:
  <input type="text" name="School">
        <br>
        Teacher: <input type="text" name="Teacher">
        <br>
        First Contact Start: <input type="text" name="dtFirstContactSt" maxlength="10" size="10">
        <br>
        Development Start: <input type="text" value= "yyyy-mm-dd" name="dtDevelopmentSt" maxlength="10" size="10">
        <br>
        Development End: <input type="text" value= "yyyy-mm-dd" name="dtDevelopmentEnd" maxlength="10" size="10">
        <br>
        Pricing Start: <input type="text" value= "yyyy-mm-dd" name="dtPricingSt" maxlength="10" size="10">
        <br>
        Pricing End: <input type="text" value= "yyyy-mm-dd" name="dtPricingEnd" maxlength="10" size="10">
        <br>
        Marketing Start: <input type="text" value= "yyyy-mm-dd" name="dtMarketingSt" maxlength="10" size="10">
        <br>
        Price Based On: <?php echo "<select name=\"PriceBasedOn\">\n";
for( $i=0; $i<=75; $i++) {
     echo "<option value=\"$i\">$i</option>\n";
}
echo "</select>";
?>
        <br>
        Trip Assigned To: <input type="text" value= "" name="TripAssignedTo" maxlength="2" size="10">
        <br>        
        TripMade:<input type="checkbox" name="TripMade">
        <br>
        Notes: 
        <textarea rows="3" cols="40" name="Notes"></textarea>
        <br>
        <input type="submit" name="submit" ONCLICK="window.location.href='http://www.integranite.net/EAToursDemo/welcome.php'">
        <input type="reset" name="reset">
        <INPUT TYPE="BUTTON" VALUE="Home" ONCLICK="window.location.href='http://www.integranite.net/EAToursDemo/welcome.php'">
      
    </form>

<?php
}
else {
// form submitted
// set server access variables
   $host="localhost";  
$username="xxx";
$password="xxx";
$database="xxx";
    
// get form input
    // check to make sure it's all there
    // escape input values for greater safety
    $School = empty($_POST['School']) ? die ("ERROR: Enter a School") : mysql_escape_string($_POST['School']);
    $Teacher = empty($_POST['Teacher']) ? die ("ERROR: Enter an Teacher") : mysql_escape_string($_POST['Teacher']);
	$dtFirstContactSt= mysql_escape_string($_POST['dtFirstContactSt']);
	$dtDevelopmentSt= mysql_escape_string($_POST['dtDevelopmentSt']);
	$dtDevelopmentEnd= mysql_escape_string($_POST['dtDevelopmentEnd']);
	$dtPricingSt= mysql_escape_string($_POST['dtPricingSt']);
	$dtPricingEnd= mysql_escape_string($_POST['dtPricingEnd']);
	$dtMarketingSt= mysql_escape_string($_POST['dtMarketingSt']);
	$TripAssignedTo= empty($_POST['TripAssignedTo']) ? die ("ERROR: Enter Initials for Trip Assigned To") : mysql_escape_string($_POST['TripAssignedTo']);
	$Notes= mysql_escape_string($_POST['Notes']);

    // open connection
    $connection = mysql_connect($host, $username, $password) or die ("Unable to connect!");
    
    // select database
    mysql_select_db($database) or die ("Unable to select database!");
    
    // create query
    $query = "INSERT INTO Project1 (School, Teacher, dtFirstContactSt, dtFirstContactEnd, dtDevelopmentSt, dtDevelopmentEnd, dtPricingSt, dtPricingEnd, dtMarketingSt, dtMarketingEnd, TripAssignedTo, Notes, PriceBasedOn) VALUES ('$School', '$Teacher', '$dtFirstContactSt', '$dtFirstContactEnd', '$dtDevelopmentSt', '$dtDevelopmentEnd', '$dtPricingSt', '$dtPricingEnd', '$dtMarketingSt', '$dtMarketingEnd', '$TripAssignedTo', '$Notes', $intPriceBasedOn)";
    
    // execute query
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
    
    // print message with ID of inserted record
    echo '<a href="welcome.php" class ="licitate"> Info Added. Click to Return Home</a>';


    // close connection
    mysql_close($connection);
}
?>
<FORM METHOD="LINK" ACTION="http://www.integranite.net/EAToursDemo/welcome.php">
</FORM>
</body>
</html>

Well when I am coding checkboxes in the DB I usually put them in as either a 0 or 1. You set it to 1 if they check it. If this is not what you are looking for please let me know. Thanks


I have a site that im trying to put together and need some help with a checkbox. I have a form where the user enters data and when the user is finished they click on the submit btn witch populates the database. I want to enter a check box into the form that the user will check if they want to. My problem is understanding the whole check box this with the database. I made a field in the table that is a tinyint. I just need the to figure out the php code that goes into the form to update the database.

<html>
<head>
<basefont face="Arial">
</head>
<body bgcolor="white">

<?php
$intPriceBasedOn = (int) $_POST['PriceBasedOn'];
if (!isset($_POST['submit'])) {
// form not submitted
?>

    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
      <p>School:
  <input type="text" name="School">
        <br>
        Teacher: <input type="text" name="Teacher">
        <br>
        First Contact Start: <input type="text" name="dtFirstContactSt" maxlength="10" size="10">
        <br>
        Development Start: <input type="text" value= "yyyy-mm-dd" name="dtDevelopmentSt" maxlength="10" size="10">
        <br>
        Development End: <input type="text" value= "yyyy-mm-dd" name="dtDevelopmentEnd" maxlength="10" size="10">
        <br>
        Pricing Start: <input type="text" value= "yyyy-mm-dd" name="dtPricingSt" maxlength="10" size="10">
        <br>
        Pricing End: <input type="text" value= "yyyy-mm-dd" name="dtPricingEnd" maxlength="10" size="10">
        <br>
        Marketing Start: <input type="text" value= "yyyy-mm-dd" name="dtMarketingSt" maxlength="10" size="10">
        <br>
        Price Based On: <?php echo "<select name=\"PriceBasedOn\">\n";
for( $i=0; $i<=75; $i++) {
     echo "<option value=\"$i\">$i</option>\n";
}
echo "</select>";
?>
        <br>
        Trip Assigned To: <input type="text" value= "" name="TripAssignedTo" maxlength="2" size="10">
        <br>        
        TripMade:<input type="checkbox" name="TripMade">
        <br>
        Notes: 
        <textarea rows="3" cols="40" name="Notes"></textarea>
        <br>
        <input type="submit" name="submit" ONCLICK="window.location.href='http://www.integranite.net/EAToursDemo/welcome.php'">
        <input type="reset" name="reset">
        <INPUT TYPE="BUTTON" VALUE="Home" ONCLICK="window.location.href='http://www.integranite.net/EAToursDemo/welcome.php'">
      
    </form>

<?php
}
else {
// form submitted
// set server access variables
   $host="localhost";  
$username="xxx";
$password="xxx";
$database="xxx";
    
// get form input
    // check to make sure it's all there
    // escape input values for greater safety
    $School = empty($_POST['School']) ? die ("ERROR: Enter a School") : mysql_escape_string($_POST['School']);
    $Teacher = empty($_POST['Teacher']) ? die ("ERROR: Enter an Teacher") : mysql_escape_string($_POST['Teacher']);
	$dtFirstContactSt= mysql_escape_string($_POST['dtFirstContactSt']);
	$dtDevelopmentSt= mysql_escape_string($_POST['dtDevelopmentSt']);
	$dtDevelopmentEnd= mysql_escape_string($_POST['dtDevelopmentEnd']);
	$dtPricingSt= mysql_escape_string($_POST['dtPricingSt']);
	$dtPricingEnd= mysql_escape_string($_POST['dtPricingEnd']);
	$dtMarketingSt= mysql_escape_string($_POST['dtMarketingSt']);
	$TripAssignedTo= empty($_POST['TripAssignedTo']) ? die ("ERROR: Enter Initials for Trip Assigned To") : mysql_escape_string($_POST['TripAssignedTo']);
	$Notes= mysql_escape_string($_POST['Notes']);

    // open connection
    $connection = mysql_connect($host, $username, $password) or die ("Unable to connect!");
    
    // select database
    mysql_select_db($database) or die ("Unable to select database!");
    
    // create query
    $query = "INSERT INTO Project1 (School, Teacher, dtFirstContactSt, dtFirstContactEnd, dtDevelopmentSt, dtDevelopmentEnd, dtPricingSt, dtPricingEnd, dtMarketingSt, dtMarketingEnd, TripAssignedTo, Notes, PriceBasedOn) VALUES ('$School', '$Teacher', '$dtFirstContactSt', '$dtFirstContactEnd', '$dtDevelopmentSt', '$dtDevelopmentEnd', '$dtPricingSt', '$dtPricingEnd', '$dtMarketingSt', '$dtMarketingEnd', '$TripAssignedTo', '$Notes', $intPriceBasedOn)";
    
    // execute query
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
    
    // print message with ID of inserted record
    echo '<a href="welcome.php" class ="licitate"> Info Added. Click to Return Home</a>';


    // close connection
    mysql_close($connection);
}
?>
<FORM METHOD="LINK" ACTION="http://www.integranite.net/EAToursDemo/welcome.php">
</FORM>
</body>
</html>

Ya I know it uses 1 and 0 but im just having trouble entering it into the database.

so far i have

TripMade:<input type="checkbox" name="TripMade" value="0">
$TripMade= mysql_escape_string($_POST['TripMade']);
$query = "INSERT INTO Project1 (TripMade) VALUES ('$TripMade')";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

what would i need to do to fix it?

Is the other stuff getting added?

This is the query I run to insert data

$query = "INSERT INTO TABLE_NAME SET TABLE_FIELD = 'VALUE'; ";
$result = mysql_query($query);

Ya the other stuff is being added too. I just shortened it for the forum. Here is my whole code

<html>
<head>
<basefont face="Arial">
</head>
<body bgcolor="white">

<?php
$intPriceBasedOn = (int) $_POST['PriceBasedOn'];
if (!isset($_POST['submit'])) {
// form not submitted
?>

    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
      <p>School:
  <input type="text" name="School">
        <br>
        Teacher: <input type="text" name="Teacher">
        <br>
        First Contact Start: <input type="text" name="dtFirstContactSt" maxlength="10" size="10">
        <br>
        Development Start: <input type="text" value= "yyyy-mm-dd" name="dtDevelopmentSt" maxlength="10" size="10">
        <br>
        Development End: <input type="text" value= "yyyy-mm-dd" name="dtDevelopmentEnd" maxlength="10" size="10">
        <br>
        Pricing Start: <input type="text" value= "yyyy-mm-dd" name="dtPricingSt" maxlength="10" size="10">
        <br>
        Pricing End: <input type="text" value= "yyyy-mm-dd" name="dtPricingEnd" maxlength="10" size="10">
        <br>
        Marketing Start: <input type="text" value= "yyyy-mm-dd" name="dtMarketingSt" maxlength="10" size="10">
        <br>
        Price Based On: <?php echo "<select name=\"PriceBasedOn\">\n";
for( $i=0; $i<=75; $i++) {
     echo "<option value=\"$i\">$i</option>\n";
}
echo "</select>";
?>
        <br>
        Trip Assigned To: <input type="text" value= "" name="TripAssignedTo" maxlength="2" size="10">
        <br>        
        TripMade:<input type="checkbox" name="TripMade" value="">
        <br>
        Notes: 
        <textarea rows="3" cols="40" name="Notes"></textarea>
        <br>
        <input type="submit" name="submit" ONCLICK="window.location.href='http://www.integranite.net/EAToursDemo/welcome.php'">
        <input type="reset" name="reset">
        <INPUT TYPE="BUTTON" VALUE="Home" ONCLICK="window.location.href='http://www.integranite.net/EAToursDemo/welcome.php'">
      
    </form>

<?php
}
else {
// form submitted
// set server access variables
   $host="localhost";  
$username="xxx";
$password="xxx";
$database="xxx";
    
// get form input
    // check to make sure it's all there
    // escape input values for greater safety
    $School = empty($_POST['School']) ? die ("ERROR: Enter a School") : mysql_escape_string($_POST['School']);
    $Teacher = empty($_POST['Teacher']) ? die ("ERROR: Enter an Teacher") : mysql_escape_string($_POST['Teacher']);
	$dtFirstContactSt= mysql_escape_string($_POST['dtFirstContactSt']);
	$dtDevelopmentSt= mysql_escape_string($_POST['dtDevelopmentSt']);
	$dtDevelopmentEnd= mysql_escape_string($_POST['dtDevelopmentEnd']);
	$dtPricingSt= mysql_escape_string($_POST['dtPricingSt']);
	$dtPricingEnd= mysql_escape_string($_POST['dtPricingEnd']);
	$dtMarketingSt= mysql_escape_string($_POST['dtMarketingSt']);
	$TripMade= mysql_escape_string($_POST['TripMade']);
	$TripAssignedTo= empty($_POST['TripAssignedTo']) ? die ("ERROR: Enter Initials for Trip Assigned To") : mysql_escape_string($_POST['TripAssignedTo']);
	$Notes= mysql_escape_string($_POST['Notes']);

    // open connection
    $connection = mysql_connect($host, $username, $password) or die ("Unable to connect!");
    
    // select database
    mysql_select_db($database) or die ("Unable to select database!");
    
    // create query
    $query = "INSERT INTO Project1 (School, Teacher, dtFirstContactSt, dtFirstContactEnd, dtDevelopmentSt, dtDevelopmentEnd, dtPricingSt, dtPricingEnd, dtMarketingSt, dtMarketingEnd, TripMade, TripAssignedTo, Notes, PriceBasedOn) VALUES ('$School', '$Teacher', '$dtFirstContactSt', '$dtFirstContactEnd', '$dtDevelopmentSt', '$dtDevelopmentEnd', '$dtPricingSt', '$dtPricingEnd', '$dtMarketingSt', '$dtMarketingEnd', '$TripMade', '$TripAssignedTo', '$Notes', $intPriceBasedOn)";
    
    // execute query
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
    
    // print message with ID of inserted record
    echo '<a href="welcome.php" class ="licitate"> Info Added. Click to Return Home</a>';


    // close connection
    mysql_close($connection);
}
?>
<FORM METHOD="LINK" ACTION="http://www.integranite.net/EAToursDemo/welcome.php">
</FORM>
</body>
</html>

What if you echo out the variable that holds the input values? Make sure to put it on the line right before you execute your query to see what the variable holds

This is what I got

<html>
<head>
<basefont face="Arial">
</head>
<body bgcolor="white">

<?php
$intPriceBasedOn = (int) $_POST['PriceBasedOn'];
if (!isset($_POST['submit'])) {
// form not submitted
?>

    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
      <p>School:
  <input type="text" name="School">
        <br>
        Teacher: <input type="text" name="Teacher">
        <br>
        First Contact Start: <input type="text" name="dtFirstContactSt" maxlength="10" size="10">
        <br>
        Development Start: <input type="text" value= "yyyy-mm-dd" name="dtDevelopmentSt" maxlength="10" size="10">
        <br>
        Development End: <input type="text" value= "yyyy-mm-dd" name="dtDevelopmentEnd" maxlength="10" size="10">
        <br>
        Pricing Start: <input type="text" value= "yyyy-mm-dd" name="dtPricingSt" maxlength="10" size="10">
        <br>
        Pricing End: <input type="text" value= "yyyy-mm-dd" name="dtPricingEnd" maxlength="10" size="10">
        <br>
        Marketing Start: <input type="text" value= "yyyy-mm-dd" name="dtMarketingSt" maxlength="10" size="10">
        <br>
        Price Based On: <?php echo "<select name=\"PriceBasedOn\">\n";
for( $i=0; $i<=75; $i++) {
     echo "<option value=\"$i\">$i</option>\n";
}
echo "</select>";
?>
        <br>
        Trip Assigned To: <input type="text" value= "" name="TripAssignedTo" maxlength="2" size="10">
        <br>        
        TripMade:<input type="checkbox" name="TripMade" value="">
        <br>
        Notes: 
        <textarea rows="3" cols="40" name="Notes"></textarea>
        <br>
        <input type="submit" name="submit" ONCLICK="window.location.href='http://www.integranite.net/EAToursDemo/welcome.php'">
        <input type="reset" name="reset">
        <INPUT TYPE="BUTTON" VALUE="Home" ONCLICK="window.location.href='http://www.integranite.net/EAToursDemo/welcome.php'">
      
    </form>

<?php
}
else {
// form submitted
// set server access variables
   $host="localhost";  
$username="integrq9_coder";
$password="Terry421";
$database="integrq9_Eurotravel";
    
// get form input
    // check to make sure it's all there
    // escape input values for greater safety
    $School = empty($_POST['School']) ? die ("ERROR: Enter a School") : mysql_escape_string($_POST['School']);
    $Teacher = empty($_POST['Teacher']) ? die ("ERROR: Enter an Teacher") : mysql_escape_string($_POST['Teacher']);
	$dtFirstContactSt= mysql_escape_string($_POST['dtFirstContactSt']);
	$dtDevelopmentSt= mysql_escape_string($_POST['dtDevelopmentSt']);
	$dtDevelopmentEnd= mysql_escape_string($_POST['dtDevelopmentEnd']);
	$dtPricingSt= mysql_escape_string($_POST['dtPricingSt']);
	$dtPricingEnd= mysql_escape_string($_POST['dtPricingEnd']);
	$dtMarketingSt= mysql_escape_string($_POST['dtMarketingSt']);
	$TripMade = ((is_null($_POST['TripMade'])) ? 0 : 1);
	$TripAssignedTo= empty($_POST['TripAssignedTo']) ? die ("ERROR: Enter Initials for Trip Assigned To") : mysql_escape_string($_POST['TripAssignedTo']);
	$Notes= mysql_escape_string($_POST['Notes']);

    // open connection
    $connection = mysql_connect($host, $username, $password) or die ("Unable to connect!");
    
    // select database
    mysql_select_db($database) or die ("Unable to select database!");
    
    // create query
	$sql = "UPDATE Project1 SET TripMade = {$TripMade}";
    $query = "INSERT INTO Project1 (School, Teacher, dtFirstContactSt, dtFirstContactEnd, dtDevelopmentSt, dtDevelopmentEnd, dtPricingSt, dtPricingEnd, dtMarketingSt, dtMarketingEnd, TripMade, TripAssignedTo, Notes, PriceBasedOn) VALUES ('$School', '$Teacher', '$dtFirstContactSt', '$dtFirstContactEnd', '$dtDevelopmentSt', '$dtDevelopmentEnd', '$dtPricingSt', '$dtPricingEnd', '$dtMarketingSt', '$dtMarketingEnd', '$TripMade', '$TripAssignedTo', '$Notes', $intPriceBasedOn)";
    
    // execute query
	$process = mysql_query($sql) or die(mysql_error());
    $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
    
    // print message with ID of inserted record
    echo '<a href="welcome.php" class ="licitate"> Info Added. Click to Return Home</a>';


    // close connection
    mysql_close($connection);
}
?>
<FORM METHOD="LINK" ACTION="http://www.integranite.net/EAToursDemo/welcome.php">
</FORM>
</body>
</html>

At the moment it adds the 1 or 0 into the database if the box is checked or not but now i want to be able to see the box checked or not on a different page. What would I use to see the box checked or not? So far I have this on the other page

$TripMade =  (($row['TripMade'] == 1) ? ' checked="checked"' : null);
<td><input name="TripMade" type="checkbox" value="<?php $TripMade ?>"> </td>

But it dosnt seem to be checking the box if it is a 1 in the database.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.