-3

更新:这是我在查询执行后得到的结果:

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

对于 HTML,我有以下复选框:

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

将其与其他元素一起插入 mysql 时(与其他元素我没有任何问题,它们被插入没有问题,然后正确显示)

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());

然后添加数据,但没有提到的复选框值。

数据库表设置:

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

我不知道为什么当所有其他数据都插入并工作时,复选框的数据没有插入?

有人可以帮我解决这个问题吗?

谢谢

4

4 回答 4

1

如果未选中复选框,则绝对不会向服务器发送任何内容。甚至没有空白或 0 的值。未选中的复选框就像表单元素不存在一样。

于 2013-04-29T06:20:41.860 回答
0
if (isset($_POST['starter']))
$starter = 1;
else
$starter = 0;

然后将 $starter 保存在您的数据库中:

mysql_query("INSERT INTO {table name}(..,starter) VALUES (..,'".$starter."');
于 2013-04-29T06:21:30.033 回答
0

您可以使用复选框的一个巧妙技巧是在复选框之前的隐藏输入中提供未选中的值,例如

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

这样,如果复选框仍未选中,则至少有一个键/值对将与表单一起提交。

对于您的查询,这只是字符串连接和糟糕的清理方法的可怕组合。

这些天你应该使用 MySQLi 或 PDO,例如 (PDO)

// Assuming the $pdo variable contains your PDO instance and all $_POST values
// have been verified as existing

$stmt = $pdo->prepare('
    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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
');

$stmt->bindParam(1, $_POST['name']);
$stmt->bindParam(2, $_POST['description']);
// etc
$stmt->bindParam(16, $_POST['starter']);
$stmt->bindParam(17, $seo_url);
$stmt->bindParam(18, $_POST['submitted']);

$stmt->execute();
于 2013-04-29T06:22:59.033 回答
0

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());
于 2013-04-29T06:26:46.897 回答