作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 html 表单,正在使用 $_POST
将数据提交到 MySQL 数据库。 。除了我的 <option>
之外,所有数据均已正确提交字段。当我尝试使用这些时,数据库提交失败。当我做print_r
时这些值是正确的,但是它们没有被提交。感谢您提供的任何帮助。这是代码:center/center_location
是给我带来问题的人。
PHP
<?php
include('php/functions.php');
$db = mysqli_connect($dbhost,$dbuser,$dbpass, $dbname);
//Test database connection
if(mysqli_connect_errno()){
die("Database connection failed: " .
mysqli_connect_errno() .
" (" . mysqli_connect_errno() . ")"
);
}
$query = "INSERT INTO rehab ".
"(name, center, first_name, initial, last_name, insurance, street1, street2, city, state, zip, contact, tel1, tel2. , callback_time)".
"VALUES ('$name', '$center_location', '$referral_first_name', '$referral_middle_initial', '$referral_last_name', '$insurance','$referral_street1', '$referral_street2', '$referral_city', '$referral_state', '$referral_zip', '$referral_contact', '$referral_email', '$referral_tel1', '$referral_tel2', '$callback_time')";//
$result = mysqli_query($db, $query);
if($result){
}else{
die("Database query failed." . mysql_error() . " " . mysqli_connect_error($db));
}
?>
这是来 self 的functions.php 文件...
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "password";
$dbname = "tc_referral";
//Set form variables
$name = isset($_POST['name']) ? $_POST['name'] : "";
$center_location = isset($_POST['center_location']) ? $_POST['center_location'] : "";
$referral_first_name = isset($_POST['referral_first_name']) ? $_POST['referral_first_name'] : "";
$referral_middle_initial = isset($_POST['referral_middle_initial']) ? $_POST['referral_middle_initial'] : "";
$referral_last_name = isset($_POST['referral_last_name']) ? $_POST['referral_last_name'] : "";
$insurance = isset($_POST['insurance']) ? $_POST['insurance'] : "";
$referral_street1 = isset($_POST['referral_street1']) ? $_POST['referral_street1'] : "";
$referral_street2 = isset($_POST['referral_street2']) ? $_POST['referral_street2'] : "";
$referral_city = isset($_POST['referral_city']) ? $_POST['referral_city'] : "";
$referral_state = isset($_POST['referral_state']) ? $_POST['referral_state'] : "";
$referral_zip = isset($_POST['referral_zip']) ? $_POST['referral_zip'] : "";
$referral_contact = isset($_POST['referral_contact']) ? $_POST['referral_contact'] : "";
$referral_email = isset($_POST['referral_email']) ? $_POST['referral_email'] : "";
$referral_tel1 = isset($_POST['referral_tel1']) ? $_POST['referral_tel1'] : "";
$referral_tel2 = isset($_POST['referral_tel2']) ? $_POST['referral_tel2'] : "";
$callback_time = isset($_POST['callback_time']) ? $_POST['callback_time'] : "";
if(isset($_POST['submit'])){
if($insurance == "yes"){
$message = "<span style=\"color: #0054a6;\">Your form has been submitted. Thank you!</span>";
}else {
$message = "They must have insurance to be submitted.";
}
}else {
$message = "<span style=\"color: #0054a6;\">Please submit the form.</span>";
}
$center_location = "
<option value=\"1\">Central Florida Men's Center</option>
<option value=\"2\">Columbus Girls' Home</option>
<option value=\"3\">Columbus Women's Home</option>
<option value=\"4\">Hosanna House</option>
<option value=\"5\">Jacksonville Men's Center</option>
<option value=\"6\">Kansas City Girls' Home</option>
<option value=\"7\">Lakeland Girls' Home</option>
<option value=\"8\">Macon Men's Center</option>
<option value=\"9\">Middle Georgia Men's Center</option>
<option value=\"10\">Ozarks Boys' Ranch</option>
<option value=\"11\">Pensacola Men's Center</option>
<option value=\"12\">Pensacola Women's Center</option>
<option value=\"13\">Prayer Mountain Boys' Academy</option>
<option value=\"14\">South Florida Davie Women's Home</option>
<option value=\"15\">Southwest Florida Men's Center</option>
<option value=\"16\">Southwest Florida Women's Home</option>
<option value=\"17\">Tallahassee Men's Center</option>
<option value=\"18\">Vero Beach Boys' Ranch</option>
<option value=\"19\">West Florida Boys' Ranch</option>
<option value=\"20\">Western Kentucky Men's Center</option>";
还有 HTML...
<div id="form">
<form action="referral_form.php" method="POST" name="rehab_referral" id="rehab_referral">
<div id="section_header"><h1>Center Information</h1></div>
<div class="block"><label>Name:</label><input type="text" id="name" name="name" autofocus required></div>
<div class="block"><label>Center Location:</label><select id="center_location" name="center_location" required>
<option><?php echo $center_location; ?></option>
</select>
</div>
<br>
<div id="section_header"><h1>Referral Information</h1></div>
<div class="block"><h2>Prospect's Info</h2></div>
<div class="block"><label>First Name:</label><input type="text" id="referral_first_name" name="referral_first_name" required></div>
<div class="block"><label>Middle Initial:</label><input class="middle_initial" type="text" id="referral_middle_initial" name="referral_middle_initial" width="1" maxlength="1"></div>
<div class="block"><label>Last Name:</label><input type="text" id="referral_last_name" name="referral_last_name" required></div>
<div class="block"><label>Do they have insurance?</label><select name="insurance" id="insurance">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</div>
<div class="block"><h2>Address</h2></div>
<div class="block"><label>Street 1:</label><input type="text" id="referral_street1" name="referral_street1"></div>
<div class="block"><label>Street 2:</label><input type="text" id="referral_street2" name="referral_street2"></div>
<div class="block"><label>City:</label><input type="text" id="referral_city" name="referral_city"></div>
<div class="block"><label>State:</label><select id="referral_state" name="referral_state">
<option><?php echo $states; ?></option>
</select>
</div>
<div class="block"><label>Zip: </label><input type="number" id="referral_zip" name="referral_zip" maxlength="5"></div>
<div class="block"><h2>Contact Info</h2></div>
<div class="block"><label>Contact Name: </label><input type="text" id="referral_contact" name="referral_contact" required></div>
<div class="block"><label>Email Address: </label><input type="email" id="referral_email" name="referral_email"></div>
<div class="block"><label>Phone 1: </label><input type="tel" id="referral_tel1" name="referral_tel1" required></div>
<div class="block"><label>Phone 2: </label><input type="tel" id="referral_tel2" name="referral_tel2"></div>
<br>
<div class="block"><label>Preferred callback time: </label>
<select name="callback_time" id="callback_time">
<?php echo $callTimes; ?>
</select>
</div>
<br>
<div class="block"><a><input type="submit" action="submit" value="Submit" name="submit"></a><?php echo $message; ?></div>
</form>
</div>
最佳答案
好吧,我要重写代码,因为这样会更容易。但说实话,您需要的帮助比我们使用堆栈溢出所能提供的要多得多,并且可能会出现一些错误,而且肯定还有很多不好的做法(例如使用 varchar 来表示是/否)。
函数.php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "password";
$dbname = "tc_referral";
//Set form variables
$name = isset($_POST['name']) ? $_POST['name'] : "";
$center_location = isset($_POST['center_location']) ? $_POST['center_location'] : "";
$referral_first_name = isset($_POST['referral_first_name']) ? $_POST['referral_first_name'] : "";
$referral_middle_initial = isset($_POST['referral_middle_initial']) ? $_POST['referral_middle_initial'] : "";
$referral_last_name = isset($_POST['referral_last_name']) ? $_POST['referral_last_name'] : "";
$insurance = isset($_POST['insurance']) ? $_POST['insurance'] : "";
$referral_street1 = isset($_POST['referral_street1']) ? $_POST['referral_street1'] : "";
$referral_street2 = isset($_POST['referral_street2']) ? $_POST['referral_street2'] : "";
$referral_city = isset($_POST['referral_city']) ? $_POST['referral_city'] : "";
$referral_state = isset($_POST['referral_state']) ? $_POST['referral_state'] : "";
$referral_zip = isset($_POST['referral_zip']) ? $_POST['referral_zip'] : "";
$referral_contact = isset($_POST['referral_contact']) ? $_POST['referral_contact'] : "";
$referral_email = isset($_POST['referral_email']) ? $_POST['referral_email'] : "";
$referral_tel1 = isset($_POST['referral_tel1']) ? $_POST['referral_tel1'] : "";
$referral_tel2 = isset($_POST['referral_tel2']) ? $_POST['referral_tel2'] : "";
$callback_time = isset($_POST['callback_time']) ? $_POST['callback_time'] : "";
if(isset($_POST['submit'])){
if($insurance == "yes"){
$message = "<span style='color: #0054a6;'>Your form has been submitted. Thank you!</span>";
}else {
$message = "They must have insurance to be submitted.";
}
}else {
$message = "<span style='color: #0054a6;'>Please submit the form.</span>";
}
referral_form.php
require_once('php/functions.php');
$db = mysqli_connect($dbhost,$dbuser,$dbpass, $dbname);
//Test database connection
if(mysqli_connect_errno()){
die("Database connection failed: " .
mysqli_connect_errno() .
" (" . mysqli_connect_errno() . ")"
);
}
$query = "INSERT INTO rehab ".
"(name, center, first_name, initial, last_name, insurance, street1, street2, city, state, zip, contact, tel1, tel2, callback_time)".
"VALUES ('$name', '$center_location', '$referral_first_name', '$referral_middle_initial', '$referral_last_name', '$insurance','$referral_street1', '$referral_street2', '$referral_city', '$referral_state', '$referral_zip', '$referral_contact', '$referral_email', '$referral_tel1', '$referral_tel2', '$callback_time')";
$result = mysqli_query($db, $query);
if($result){
}else{
die("Database query failed." . mysql_error() . " " . mysqli_connect_error($db));
}
?>
html 文件
<div id="form">
<form action="referral_form.php" method="POST" name="rehab_referral" id="rehab_referral">
<div id="section_header"><h1>Center Information</h1></div>
<div class="block"><label>Name:</label><input type="text" id="name" name="name" autofocus required></div>
<div class="block"><label>Center Location:</label><select id="center_location" name="center_location" required>
<option value="1">Central Florida Men's Center</option>
<option value="2">Columbus Girls' Home</option>
<option value="3">Columbus Women's Home</option>
<option value="4">Hosanna House</option>
<option value="5">Jacksonville Men's Center</option>
<option value="6">Kansas City Girls' Home</option>
<option value="7">Lakeland Girls' Home</option>
<option value="8">Macon Men's Center</option>
<option value="9">Middle Georgia Men's Center</option>
<option value="10">Ozarks Boys' Ranch</option>
<option value="11">Pensacola Men's Center</option>
<option value="12">Pensacola Women's Center</option>
<option value="13">Prayer Mountain Boys' Academy</option>
<option value="14">South Florida Davie Women's Home</option>
<option value="15">Southwest Florida Men's Center</option>
<option value="16">Southwest Florida Women's Home</option>
<option value="17">Tallahassee Men's Center</option>
<option value="18">Vero Beach Boys' Ranch</option>
<option value="19">West Florida Boys' Ranch</option>
<option value="20">Western Kentucky Men's Center</option>
</select>
</div>
<br>
<div id="section_header"><h1>Referral Information</h1></div>
<div class="block"><h2>Prospect's Info</h2></div>
<div class="block"><label>First Name:</label><input type="text" id="referral_first_name" name="referral_first_name" required></div>
<div class="block"><label>Middle Initial:</label><input class="middle_initial" type="text" id="referral_middle_initial" name="referral_middle_initial" width="1" maxlength="1"></div>
<div class="block"><label>Last Name:</label><input type="text" id="referral_last_name" name="referral_last_name" required></div>
<div class="block"><label>Do they have insurance?</label><select name="insurance" id="insurance">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</div>
<div class="block"><h2>Address</h2></div>
<div class="block"><label>Street 1:</label><input type="text" id="referral_street1" name="referral_street1"></div>
<div class="block"><label>Street 2:</label><input type="text" id="referral_street2" name="referral_street2"></div>
<div class="block"><label>City:</label><input type="text" id="referral_city" name="referral_city"></div>
<div class="block"><label>State:</label><select id="referral_state" name="referral_state">
<option value="AZ">Arizona</option>
<option value="AL">Alabama</option>
</select>
</div>
<div class="block"><label>Zip: </label><input type="number" id="referral_zip" name="referral_zip" maxlength="5"></div>
<div class="block"><h2>Contact Info</h2></div>
<div class="block"><label>Contact Name: </label><input type="text" id="referral_contact" name="referral_contact" required></div>
<div class="block"><label>Email Address: </label><input type="email" id="referral_email" name="referral_email"></div>
<div class="block"><label>Phone 1: </label><input type="tel" id="referral_tel1" name="referral_tel1" required></div>
<div class="block"><label>Phone 2: </label><input type="tel" id="referral_tel2" name="referral_tel2"></div>
<br>
<div class="block"><label>Preferred callback time: </label>
<select name="callback_time" id="callback_time">
<option value="9:00">9:00</option>
<option value="10:00">10:00</option>
</select>
</div>
<br>
<div class="block"><a><input type="submit" action="submit" value="Submit" name="submit"></a><?php echo $message; ?></div>
</form>
</div>
关于php - 选项值未提交到 MySQL 数据库 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21642127/
我是一名优秀的程序员,十分优秀!