首页 文章

如何将复选框中的值更新为MySQL

提问于
浏览
-2

我的表格是form method =“post”>

input type =“checkbox”name =“facebook”value =“yes”>

提交

所以,我想做的是:

a)如果选中该复选框,我想用yes更新相应的字段

b)如果取消选中该复选框,我想更新字段为no

4 回答

  • 1

    $_POST['chk_ans'] 仅在检查时返回,

    if (isset($_POST['chk_ans']))
    {
    
    ..Update Query here ..
    
    }
    
  • 0

    使用此表格:

    <form method='POST'>
        <input type='checkbox' name='myCheckbox' />
        <input type='submit' name='submit' value='Submit' />
    </form>
    

    您可以通过这种方式处理它:

    <?php
        $mysqli = new mysqli("localhost", "myUser", "myPassword", "myDb");
    
        if(isset($_POST['submit'])) {
            $value = $_POST['myCheckbox'] ? 1 : 0;
            $mysqli->query("UPDATE myTable SET myTinyInt = " . $value);
        }
    ?>
    
  • -1

    首先,您希望在sql中准备好所有内容,希望值进入的表以及要使其工作的过程,您可能希望进行插入过程和更新过程 . 然后,您希望调用该方法,以便可以将值添加到sql中 .

  • 1

    试试这个:

    <?php
    
                // You'll need a connection to db, in my example (mysql connection) this is not included.
    
                // Check if form has been submitted.
                if($_SERVER['REQUEST_METHOD'] == 'POST') {
    
                    $field = (isset($_POST['facebook']) AND $_POST['facebook'] == 'yes') ? 'yes' : 'no';
    
                    // Your query: In this example for mysql extension
                    mysql_query('UPDATE my_table SET field="'.mysql_real_escape_string($field).'"');
    
                }
    
            ?>
    

相关问题