dtvgo28624 2013-04-29 06:15
浏览 58
已采纳

将复选框值插入mysql时出现问题

Update: This is the result I get after query execution:

NewiTem9,desc,video9.swf,9,0,0,1,0,550,500,item9.jpg,0,swf,prev,0,00,0,2013-04-29 03:23:43,1,0,0,0,newitem9,0

For the HTML I have the following checkbox:

<input type="checkbox" name="starter" value="1" id="starter0">

When inserting this into mysql with other elements (with other elements i don't have any problem, they are inserted without a problem and then displaying properly)

mysql_query("
    INSERT INTO datatab (
        name, description, url, category_id, category_parent, width, height,
        image, activado, filetype, instructions, date_added, show_ads, rmode,
        power_id, starter, seo_url, submitter
    )
    VALUES (
        '".escape($_POST['name'])."', '".escape($_POST['description'])."',
        '$url', $_POST[category], $category[parent_id], '$_POST[width]',
        '$_POST[height]', '$img', $_POST[activado], '$ext',
        '".escape($_POST['instructions'])."', '$date', $_POST[show_ads],
        $_POST[rmode], '$_POST[power_id]', '$_POST[starter]', '$seo_url',
        '$_POST[submitter]')
    ") or die ('There was a MySql error when adding the game: '.mysql_error());

Then the data is added but without the mentioned checkbox value.

Database table settings:

Name: starter
Type: TinyINT
Lenght/Values: 1
default: As defined : 0

I don't know why the data of the checkbox is not inserted when all other data are inserted and working?

Could someone help me with this?

Thanks

  • 写回答

4条回答 默认 最新

  • dqpkea9486 2013-04-29 06:26
    关注

    Checkboxes are somewhat special. They only get posted IF they are checked.

    If you have foollowing HTML:

    <input type="checkbox" name="starter" value="1" id="starter0">
    

    and a user only check the checkbox, then result of post would be like

    $_POST['starter'] = 1
    

    You have to check whether or not a checkbox is sent and after that do the correct insert.

    If you have it checked and it still does not work, it is an issue with actual query even if you don't get an explicit error. MySQL is somewhat forgiving when inserting things with "wrong" datatype.

    UPDATE: You can try this:

    if (isset($_POST['starter']) && $_POST['starter'] == 1) {
        $starter = 1;
    }
    else {
        $starter = 0;
    }
    
    mysql_query("
        INSERT INTO datatab (
            name, description, url, category_id, category_parent, width, height,
            image, activado, filetype, instructions, date_added, show_ads, rmode,
            power_id, starter, seo_url, submitter
        )
        VALUES (
            '".escape($_POST['name'])."', '".escape($_POST['description'])."',
            '$url', $_POST[category], $category[parent_id], '$_POST[width]',
            '$_POST[height]', '$img', $_POST[activado], '$ext',
            '".escape($_POST['instructions'])."', '$date', $_POST[show_ads],
            $_POST[rmode], '$_POST[power_id]', $starter, '$seo_url',
            '$_POST[submitter]')
        ") or die ('There was a MySql error when adding the game: '.mysql_error());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能