用php在mysql数据库中保存复选框(boolean)值 [英] Saving checkbox (boolean) values in mysql database with php

查看:178
本文介绍了用php在mysql数据库中保存复选框(boolean)值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to save the checkbox value .. I'm doing a enrollment system with a checkbox form. It's function is to know if the student pass the requirement, so I assign it as a boolean type. It must save the value of '1' if the checked and '0' if unchecked.

和错误是:

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

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

and if I uncheck one of the requirement this is what happen:

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

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

I really need some help. Here is the code..

样本形式:1将值设置为"1",数据库中的默认值是"0"

Sample form: 1 set the value as '1' and the default value in my database is '0'

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

php代码add_newstud.php

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

推荐答案

首先,MySQL扩展在PHP 5.5.0中已弃用,而在PHP 7.0.0中已将其删除.相反,应使用MySQLi或PDO_MySQL扩展名.

Firstly, MySQL extensions are deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

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

If you must use MySQL, I would advise:

$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.

This will help ensure that no SQL Injection occurs. Again, I would strongly advise moving to MySQLi or PDO.

这篇关于用php在mysql数据库中保存复选框(boolean)值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆