需要帮助存储复选框,单选按钮,并在mysql中下拉菜单数据 [英] Need help storing check box, radio button, and drop down menu data in mysql
问题描述
还可以参考这个。
php和mysql - 发送复选框,单选按钮,并从用户下载菜单结果到mysql数据库并存储在那里
session_start();
函数logged_in(){
return isset($ _ SESSION ['user_id']);
}
函数confirm_logged_in(){
if(!logged_in()){
redirect_to(contact info.php);
}
}
//数据库常量
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','wer_application');
函数check_required_fields($ required_array){
$ field_errors = array();
foreach($ required_array as $ fieldname){
if(!isset($ _ POST [$ fieldname])||(空($ _ POST [$ fieldname])&!is_numeric($ _ POST [$ fieldname]))){
$ field_errors [] = $ fieldname;
}
}
return $ field_errors;
}
函数check_max_field_lengths($ field_length_array){
$ field_errors = array();
foreach($ field_length_array as $ fieldname => $ maxlength){
if(strlen(trim(mysql_prep($ _ POST [$ fieldname])))> $ maxlength){$ field_errors [] = $字段名; }
}
return $ field_errors;
}
function display_errors($ error_array){
echo< p class = \errors\>;
echo请查看以下字段:< br />;
foreach($ error_array as $ error){
echo - 。 $错误。 < br />;
}
echo< / p>;
}
// START FORM PROCESSING
if(isset($ _ POST ['submit'])){//表单已提交。
$ errors = array();
//对表单数据执行验证
$ required_fields = array('id','student_gender','student_session_one_preference','student_session_two_preference',
'waldron_scholarship' jesse_van_anterp_huyck_scholarship','edward_raney_scholarship','school_type',
'student_statemenet_consent_check_box','guardian_statemenet_consent_check_box','waiver');
$ errors = array_merge($ errors,check_required_fields($ required_fields,$ _POST));
$ errors = array_merge($ errors,check_max_field_lengths($ fields_with_lengths,$ _POST));
//捕获用户输入的内容
if(!empty($ _ POST)&& isset($ _ POST [submit-btn])){
$ student_gender = isset($ _ POST [student_gender])? $ _POST [student_gender]:;
$ student_session_one_preference = isset($ _ POST [student_session_one_preference])? $ _POST [student_session_one_preference]:;
$ student_session_two_preference = isset($ _ POST [student_session_two_preference])? $ _POST [student_session_two_preference]:;
$ waldron_scholarship = isset($ _ POST [waldron_scholarship])? $ _POST [waldron_scholarship]:;
$ jesse_van_anterp_huyck_scholarship = isset($ _ POST [jesse_van_anterp_huyck_scholarship])? $ _POST [jesse_van_anterp_huyck_scholarship]:;
$ edward_raney_scholarship = isset($ _ POST [edward_raney_scholarship])? $ _POST [edward_raney_scholarship]:;
$ school_type = isset($ _ POST [school_type])? $ _POST [school_type]:;
$ student_statemenet_consent_check_box = isset($ _ POST [student_statemenet_consent_check_box])? $ _POST [student_statemenet_consent_check_box]:;
$ guardian_statemenet_consent_check_box = isset($ _ POST [guardian_statemenet_consent_check_box])? $ _POST [guardian_statemenet_consent_check_box]:;
$ waiver = isset($ _ POST [waiver])? $ _POST [waiver]:;
//将表单中的数据发送到我们使用mysql创建的数据库中。
$ sql =INSERT INTO nature VALUES('id','$ student_gender','$ student_session_one_preference','$ student_session_two_preference',
'$ waldron_scholarship','$ jesse_van_anterp_huyck_scholarship','$ edward_raney_scholarship' ,'$ school_type',
'$ student_statemenet_consent_check_box','$ guardian_statemenet_consent_check_box','$ waiver');
if(!mysql_query($ sql)){
die('Error:'。mysql_error());
}
}
< html>
< head>
< title> WER应用包< / title>
< link href =stylesheets / public.cssmedia =all =stylesheettype =text / css/>
< / head>
< body>
< div id =header>
< h1> WER应用分组< / h1>
< / div>
< div id =main>
< table id =structure>
< tr>
< td id =page>
<?php if(!empty($ message)){echo< p class = \message\> 。 $消息。 < / p>;}?>
<?php if(!empty($ errors)){display_errors($ errors); }?>
< form action =contact info.phpmethod =post>
< form method =POST>
< input type =radioname =student_gendervalue =Male> Male< br>
< input type =radioname =student_gendervalue =Female> Female< br>
< select name =student_session_one_preference>
< option value =Session 1>会话1:6月30日 - 7月21日< / option>
< option value =Session 2> Session 2:July 28 - August 18< / option>
< / select> <峰; br />
首选项2:
< select name =student_session_two_preference>
< option value =Session 1>会话1:6月30日 - 7月21日< / option>
< option value =Session 2> Session 2:July 28 - August 18< / option>
< / select>
< input type =checkboxname =waldron_scholarshipvalue =Waldron奖学金> Waldron奖学金< br />
< input type =checkboxname =jesse_van_anterp_huyck_scholarshipvalue =Jesse Van Anterp Huyck奖学金> Jesse Van Anterp Huyck奖学金< br />
< input type =checkboxname =edward_raney_scholarshipvalue =Edward Raney奖学金> Edward Raney奖学金< br />
< select name =school_type>
< option value =Public> Public< / option>
< option value =Private> Private Parochial< / option>
< option value =Other>其他< / option>
< / select>
< input type =radioname =waivervalue =>我放弃了对我的裁判提交的信息的任何访问权限< br>
< input type =radioname =waivervalue =>我不放弃我的裁判员提交的信息的任何访问权限< br>
< input type =checkboxname =student_statement_consent_check_boxvalue =Agree>通过选中此框,学生确认< br />他们已经阅读了< br />同意并同意。< br>
< input type =checkboxname =guardian_statement_consent_check_boxvalue =Agree>通过选中此框,监护人确认< br />他们已经阅读了< br />同意并同意。< br>
< input type =submitvalue =Submitname =submit-btn>
< / form>
< / td>
< / tr>
< / table>
< / div>
< div id =footer>< / div>
< / body>
< / html>
<?php
// 5.关闭连接
mysql_close($ connection);
?>
CREATE TABLE性质(
ID int NOT NULL auto_increment,
student_gender varchar )
student_session_one_preference varchar(30),
student_session_two_preference varchar(30),
waldron_scholarship tinyint,
jesse_van_anterp_huyck_scholarship tinyint,
edward_raney_scholarship tinyint,
school_type varchar 25),
student_statemenet_consent_check_box tinyint,
guardian_statemenet_consent_check_box tinyint,
waiver tinyint,
PRIMARY KEY(ID)
);
您的豁免都缺少值,所以$ _POST ['waiver']将始终为空。修复如下:
< input type =radioname =waivervalue =yes>
< input type =radioname =waivervalue =no>
您的复选框字段可以设置这样的变量(只需检查是否设置):
$ jesse_van_anterp_huyck_scholarship = isset($ _ POST [jesse_van_anterp_huyck_scholarship])? 1:0;
FYI:您的varchar(30)字段可能不够长,可以存储字符串Jesse Van Anterp惠誉奖学金
I am trying to have my form data stored in a mysql database. I want everything that the user types or clicks to be stored there. I am able to have my text fields stored in mysql successfully. I am having trouble figuring out how to send multiple check boxes, radio buttons, and drop down lists to mysql and have it stored there. I don't even know where to begin. Please help.
Also refer to this too.
session_start();
function logged_in() {
return isset($_SESSION['user_id']);
}
function confirm_logged_in() {
if (!logged_in()) {
redirect_to("contact info.php");
}
}
// Database Constants
define('DB_SERVER', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'wer_application');
function check_required_fields($required_array) {
$field_errors = array();
foreach($required_array as $fieldname) {
if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_numeric($_POST[$fieldname]))) {
$field_errors[] = $fieldname;
}
}
return $field_errors;
}
function check_max_field_lengths($field_length_array) {
$field_errors = array();
foreach($field_length_array as $fieldname => $maxlength ) {
if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $field_errors[] = $fieldname; }
}
return $field_errors;
}
function display_errors($error_array) {
echo "<p class=\"errors\">";
echo "Please review the following fields:<br />";
foreach($error_array as $error) {
echo " - " . $error . "<br />";
}
echo "</p>";
}
// START FORM PROCESSING
if (isset($_POST['submit'])) { // Form has been submitted.
$errors = array();
// perform validations on the form data
$required_fields = array('id','student_gender', 'student_session_one_preference', 'student_session_two_preference',
'waldron_scholarship', 'jesse_van_anterp_huyck_scholarship', 'edward_raney_scholarship', 'school_type',
'student_statemenet_consent_check_box', 'guardian_statemenet_consent_check_box', 'waiver');
$errors = array_merge($errors, check_required_fields($required_fields, $_POST));
$errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));
//Captures what is typed by the user
if(!empty($_POST) && isset($_POST["submit-btn"])){
$student_gender = isset($_POST["student_gender"]) ? $_POST["student_gender"] : "";
$student_session_one_preference = isset($_POST["student_session_one_preference"]) ? $_POST["student_session_one_preference"] : "";
$student_session_two_preference = isset($_POST["student_session_two_preference"]) ? $_POST["student_session_two_preference"] : "";
$waldron_scholarship = isset($_POST["waldron_scholarship"]) ? $_POST["waldron_scholarship"] : "";
$jesse_van_anterp_huyck_scholarship = isset($_POST["jesse_van_anterp_huyck_scholarship"]) ? $_POST["jesse_van_anterp_huyck_scholarship"] : "";
$edward_raney_scholarship = isset($_POST["edward_raney_scholarship"]) ? $_POST["edward_raney_scholarship"] : "";
$school_type = isset($_POST["school_type"]) ? $_POST["school_type"] : "";
$student_statemenet_consent_check_box = isset($_POST["student_statemenet_consent_check_box"]) ? $_POST["student_statemenet_consent_check_box"] : "";
$guardian_statemenet_consent_check_box = isset($_POST["guardian_statemenet_consent_check_box"]) ? $_POST["guardian_statemenet_consent_check_box"] : "";
$waiver = isset($_POST["waiver"]) ? $_POST["waiver"] : "";
//sends the data from the form into our database we made with mysql.
$sql = "INSERT INTO nature VALUES ('id','$student_gender', '$student_session_one_preference', '$student_session_two_preference',
'$waldron_scholarship', '$jesse_van_anterp_huyck_scholarship', '$edward_raney_scholarship', '$school_type',
'$student_statemenet_consent_check_box', '$guardian_statemenet_consent_check_box', '$waiver')";
if(!mysql_query($sql)){
die('Error: ' . mysql_error());
}
}
<html>
<head>
<title>WER Application Packet</title>
<link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
<h1>WER Application Packet</h1>
</div>
<div id="main">
<table id="structure">
<tr>
<td id="page">
<?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
<?php if (!empty($errors)) { display_errors($errors); } ?>
<form action="contact info.php" method="post">
<form method="POST">
<input type="radio" name="student_gender" value="Male">Male<br>
<input type="radio" name="student_gender" value="Female">Female<br>
<select name="student_session_one_preference">
<option value="Session 1">Session 1: June 30 - July 21</option>
<option value="Session 2">Session 2: July 28 - August 18</option>
</select> <br/>
Preference 2:
<select name="student_session_two_preference">
<option value="Session 1">Session 1: June 30 - July 21</option>
<option value="Session 2">Session 2: July 28 - August 18</option>
</select>
<input type="checkbox" name="waldron_scholarship" value="Waldron Scholarship">Waldron Scholarship<br/>
<input type="checkbox" name="jesse_van_anterp_huyck_scholarship" value="Jesse Van Anterp Huyck Scholarship">Jesse Van Anterp Huyck Scholarship<br/>
<input type="checkbox" name="edward_raney_scholarship" value="Edward Raney Scholarship">Edward Raney Scholarship<br/>
<select name="school_type">
<option value="Public">Public</option>
<option value="Private">Private Parochial</option>
<option value="Other">Other</option>
</select>
<input type="radio" name="waiver" value="">I waive any right of access that I may have to information submitted by my referee<br>
<input type="radio" name="waiver" value="">I do not waive any right of access that I may have to information submitted by my referee<br>
<input type="checkbox" name="student_statement_consent_check_box" value="Agree">By checking this box, the student acknowledges <br/> that they have read the statement <br/> of consent and agrees with it.<br>
<input type="checkbox" name="guardian_statement_consent_check_box" value="Agree">By checking this box, the guardian acknowledges <br/> that they have read the statement <br/> of consent and agrees with it.<br>
<input type="submit" value="Submit" name="submit-btn">
</form>
</td>
</tr>
</table>
</div>
<div id="footer"></div>
</body>
</html>
<?php
// 5. Close connection
mysql_close($connection);
?>
CREATE TABLE nature(
ID int NOT NULL auto_increment,
student_gender varchar(6),
student_session_one_preference varchar(30),
student_session_two_preference varchar(30),
waldron_scholarship tinyint,
jesse_van_anterp_huyck_scholarship tinyint,
edward_raney_scholarship tinyint,
school_type varchar(25),
student_statemenet_consent_check_box tinyint,
guardian_statemenet_consent_check_box tinyint,
waiver tinyint,
PRIMARY KEY(ID)
);
Your waiver radio inputs are both missing values, so $_POST['waiver'] will always be empty. Fix like this:
<input type="radio" name="waiver" value="yes">
<input type="radio" name="waiver" value="no">
Your checkbox fields can set the variables like this (just check if set):
$jesse_van_anterp_huyck_scholarship = isset($_POST["jesse_van_anterp_huyck_scholarship"]) ? 1 : 0;
FYI: Your varchar(30) fields may not be long enough to store strings like "Jesse Van Anterp Huyck Scholarship"
这篇关于需要帮助存储复选框,单选按钮,并在mysql中下拉菜单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!