Passing Checkbox from Form

I am trying to update a Mysql database. $conference is a boolean(tinyint).

The problem is:
If I click the box, it has this message when I submit:
UPDATE contacts SET contact = ‘on’ WHERE contact_id = 3Error: Incorrect integer value: ‘on’ for column ‘contact’ at row 1

If I try to send an empty checkbox, I get this after submit:
Notice: Undefined index: conference in C:\check.php on line
UPDATE contacts SET contact = ‘’ WHERE contact_id = 3Error: Incorrect integer value: ‘’ for column ‘contact’ at row 1

Any help would be appreciated. I have struggled with this.


 
<?php
if (isset($_POST['submit'])) {
    $scheduleid = $_POST['scheduleid'];
    $conference = $_POST['conference'];
    $sql = "UPDATE contacts SET
    contact = '$conference'
  WHERE contact_id = $scheduleid";
    ECHO $sql;
        IF (mysql_query($sql)) {
          ECHO "Game details updated.";
        } ELSE {
          ECHO "Error: " . mysql_error();
          EXIT;
        }
    ECHO "<P><A HREF=\\"check.php\\">Back</A></P>";
}
ELSE
{
    $scheduleid = 3;
    $reportdata = mysql_query("SELECT * FROM contacts WHERE contact_id = $scheduleid");
    IF (!$reportdata) {
      ECHO ("<P>Error fetching game details: " .
              mysql_error() . "</P>");
        EXIT();
    }
 
    $info = mysql_fetch_array($reportdata);
    $id = $info['contact_id'];
    $conference = $info['contact'];
?>
  <FORM ACTION="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD="POST">
    <TABLE>
      <TR>
        <TD align=right valign=top><b>Conference:</b></TD>
        <TD><INPUT TYPE="checkbox" NAME="conference" <?php if ($conference){ECHO "CHECKED";}?>></TD>
      </TR>
    </TABLE>
    <BR />
    <INPUT TYPE=HIDDEN NAME="scheduleid" VALUE="<?php echo $scheduleid; ?>">
    <INPUT TYPE="submit" NAME="submit" VALUE="Submit">
    <BR />
  </FORM>
<?PHP
}
?>
 

Maybe this will help .:slight_smile:


<?php
if('POST' === $_SERVER['REQUEST_METHOD']){
  $sql = sprintf(
    'UPDATE contact SET contact = %s WHERE id = %d',
    isset($_POST['contact']) ? 'true' : 'false' ,
    $_POST['id']
  );
}
?>
<html>
  <head>
    <title>
      Form Demo
    </title>
  </head>
  <body>
    <form action="" method="post">
      <input type="checkbox" name="contact" />
      <input type="hidden" name="id" value="4" />
      <input type="submit" value="submit" />
    </form>
    <?php if(isset($sql)): ?>
    <pre>
      SQL: <?php echo $sql; ?>
    </pre>
    <?php endif; ?>
  </body>
</html>

I tinkered with it a little more.
The only way I can make it work like I want it to is add this line at the top after pressing submit:


<?php
if ( empty($_POST['conference']) ? $conference=0 : $conference=1 );
 
if (isset($_POST['submit'])) {
    $scheduleid = $_POST['scheduleid'];
//    $conference = $_POST['conference'];
    $sql = "UPDATE contacts SET
    contact = '$conference'
  WHERE contact_id = $scheduleid";
 
?>
 

Seems to be the only way I can change conference to a 0 or 1 and save it in the tinyint table allocation.

Well, there is shorter way.


<?php
if('POST' === $_SERVER['REQUEST_METHOD']){
  $sql = sprintf(
    'UPDATE contact SET contact = %d WHERE id = %d',
    isset($_POST['contact']),
    $_POST['id']
  );
}
?>
<html>
  <head>
    <title>
      Form Demo
    </title>
  </head>
  <body>
    <form action="" method="post">
      <input type="checkbox" name="contact" />
      <input type="hidden" name="id" value="4" />
      <input type="submit" value="submit" />
    </form>
    <?php if(isset($sql)): ?>
    <pre>
      SQL: <?php echo $sql; ?>
    </pre>
    <?php endif; ?>
  </body>
</html>

Just to add - the original reason for the error was because you were applying a string value to an integer (TINYINT) field in the database. :slight_smile: