我有一个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));
}
?>
这是来自我的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>
答案 0 :(得分:0)
好的我要重写代码'因为这样会更容易。但坦率地说,你需要的方式比我们使用堆栈溢出提供的帮助更多,可能会有一些错误,当然还有很多不好的做法(比如使用varchar表示是/否)。
的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>";
}
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>