Update value in mysql database column with checkbox in php

This question already has an answer here:

  • Reference - What does this error mean in PHP? 30 answers

  • The problem is your echo on line 7. Your doing <?php echo ... ?> inside of an echo. You should instead concatenate the values into the string.

    Instead of:

     echo '<td><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><input type="hidden" name="id" value="<?php echo $id; ?>" /><input type="checkbox" name="checkinsat" value="1"<?php if($data['checkinsat'] == '1') echo 'checked'; ?>/><input type="submit" ></form></td>';
    

    Try this:

    $checked = ($data['checkinsat'] == '1') ? 'checked' : '';
    echo '<td><form method="post" action="'.$_SERVER['PHP_SELF'].'"><input type="hidden" name="id" value="'. $id.'" /><input type="checkbox" name="checkinsat" value="1" '.$checked.'/><input type="submit" ></form></td>';
    

    Don't echo the parameters into the form using php tags, use a string concatenation instead.

    Change:

    echo '<td><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><input type="hidden" name="id" value="<?php echo $id; ?>" /><input type="checkbox" name="checkinsat" value="1"<?php if($data['checkinsat'] == '1') echo 'checked'; ?>/><input type="submit" ></form></td>';
    

    to:

    echo '<td><form method="post" action="'. $_SERVER['PHP_SELF'] .'"> <input type="hidden" name="id" value="'. $id .'" /><input type="checkbox" name="checkinsat" value="1"'. ($data['checkinsat'] == '1'? 'checked' : '') .'<input type="submit" ></form></td>';
    

    Further, do not use mysql_* functions - it's deprecated (see red box) and vulnerable to sql-injection. Use PDO or MySQLi.


    you have typo here

    echo '<td><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><input type="hidden" name="id" value="<?php echo $id; ?>" /><input type="checkbox" name="checkinsat" value="1"<?php if($data['checkinsat'] == '1') echo 'checked'; ?>/><input type="submit" ></form></td>';
    

    should be

    echo '<td><form method="post" action="'.$_SERVER['PHP_SELF']'"><input type="hidden" name="id" value="'.$id.'" /><input type="checkbox" name="checkinsat" value="1"';
     if($data['checkinsat'] == '1') 
      echo 'checked />';
     echo '<input type="submit" ></form></td>';
    

    you are writing <?PHP ... ?> in php tag and that is causes this error

    链接地址: http://www.djcxy.com/p/69408.html

    上一篇: MySQL查询问题

    下一篇: 使用php中的复选框更新mysql数据库列中的值