使用php在mysql数据库中保存复选框(布尔值)

时间:2015-09-15 17:51:54

标签: php mysql checkbox boolean

我正在尝试保存复选框值..我正在使用复选框表单进行注册系统。它的功能是知道学生是否通过了要求,因此我将其指定为布尔类型。如果选中则必须保存'1'的值,如果未选中则必须保存'0'。

,错误是:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '')' at line 2

如果我取消其中一项要求,则会发生这种情况:

Notice: Undefined index: brigada_form in C:\xampp2\htdocs\es\add_newstud.php on line 12
Notice: Undefined index: two_picture in C:\xampp2\htdocs\es\add_newstud.php on line 18

我真的需要一些帮助。这是代码..

示例表单:1将值设置为“1”,数据库中的默认值为“0”

<input type="checkbox" name="two_picture" id="two_picture" value="1" />

php code add_newstud.php

$requirement_id = $_POST['requirement_id'];     
$enrollment_form = $_POST['enrollment_form'];
$report_card = $_POST['report_card'];
$brigada_form = $_POST['brigada_form'];
$physical_inspection_form= $_POST['physical_inspection_form'];      
$agreement_upon_enrollment = $_POST['agreement_upon_enrollment'];
$nso = $_POST['nso'];
$good_moral = $_POST['good_moral'];
$one_picture = $_POST['one_picture'];       
$two_picture = $_POST['two_picture'];

$insert_req = "INSERT INTO es_req_newstud(requirement_id,enrollment_form,report_card,brigada_form,physical_inspection_form,agreement_upon_enrollment,nso,good_moral,1x1_picture,2x2_picture) `VALUES` ('$requirement_id','$enrollment_form','$report_card','$brigada_form','$physical_inspection_form','$agreement_upon_enrollment','$nso','$good_moral','$one_picture','$two_picture)";
mysql_query($insert_req) or die(mysql_error());

1 个答案:

答案 0 :(得分:1)

首先,在PHP 5.5.0中不推荐使用MySQL扩展,并且它已在PHP 7.0.0中删除。相反,应该使用MySQLi或PDO_MySQL扩展。

如果你必须使用MySQL,我会建议:

$insert_req = sprintf("INSERT INTO es_req_newstud (requirement_id,enrollment_form,report_card,brigada_form,physical_inspection_form,agreement_upon_enrollment,nso,good_moral,1x1_picture,2x2_picture) VALUES ('%d','%s','%s','%s','%s','%s','%s','%s','%d','%d');",
    $_POST['requirement_id'],
    mysql_real_escape_string($_POST['enrollment_form']),
    mysql_real_escape_string($_POST['report_card']),
    mysql_real_escape_string($_POST['brigada_form']),
    mysql_real_escape_string($_POST['physical_inspection_form']),
    mysql_real_escape_string($_POST['agreement_upon_enrollment']),
    mysql_real_escape_string($_POST['nso']),
    mysql_real_escape_string($_POST['good_moral']),
    $_POST['one_picture'],
    $_POST['two_picture']
);
mysql_query($insert_req) or die(mysql_error());

这有助于确保不会发生SQL注入。同样,我强烈建议转移到MySQLi或PDO。