我正在尝试将表单中的数据插入到我的数据库中,但它似乎不起作用。我在插入查询之后放了一个echo,所以我可以验证数据是否已经插入,但它不会回显我写的内容。我的查询或我的php的任何部分有问题吗?
我的PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stat_system";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$fname = $lname = $mname = $contact = $age = $attain = $course = $school = $position = $exp = $ref = $batchtxt = $hiredate = $prevbpo = $remarks = $nho = $nonbpo = $holdAttain = $holdPos = "";
$error_flag = 0;
if (isset($_POST['submit'])) {
$holdAttain = (isset($_POST['cmbAttain']));
$holdPos = (isset($_POST['cmbPosition']));
if (!empty($_POST['firstname'])) {
$fname = $_POST['firstname'];
}
if (!empty($_POST['lastname'])) {
$lname = $_POST['lastname'];
}
if (!empty($_POST['middlename'])) {
$mname = $_POST['middlename'];
}
if (!empty($_POST['contact'])) {
$contact = $_POST['contact'];
}
if (!empty($_POST['age'])) {
$age = $_POST['age'];
}
if (isset($_POST['cmbAttain'])) {
$attain = $_POST['cmbAttain'];
}
if(isset($_POST['school'])) {
$school = $_POST['school'];
}
if(isset($_POST['course'])) {
$course = $_POST['course'];
}
if (isset($_POST['exp'])) {
$exp = $_POST['exp'];
}
if (!empty($_POST['remarks'])) {
$remarks = $_POST['remarks'];
}
if (isset($_POST['nonbpo'])) {
$nonbpo = $_POST['nonbpo'];
}
if (isset($_POST['prevbpo'])) {
$prevbpo = $_POST['prevbpo'];
}
if (!empty($_POST['ref'])) {
$ref = $_POST['ref'];
}
if (isset($_POST['hiredate'])) {
$hiredate = $_POST['hiredate'];
}
if (isset($_POST['batchtxt'])) {
$batchtxt = $_POST['batchtxt'];
}
if (!empty($_POST['nho'])) {
$nho = $_POST['nho'];
}
if($error_flag == 0){
$sql = mysqli_query($conn,"INSERT INTO applicants (appID, appLastName, appFirstName, appMidleName, Age, appPhoneNumber, appBatch, appExperience, appRemarks, appPreviousBPO, appSchool, appCourse, appGraduate, appNonBPO, appPosition, appHireDate, appNHO, appReferrer)
VALUES (NULL, '$lname', '$fname', '$mname', $age, '$contact', $batchtxt, '$exp', $remarks, '$school', '$course', '$attain', '$nonbpo', '$position', $hiredate, $nho, '$ref')");
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#succModal').modal('show');
});
</script>";
$fname = $lname = $mname = $contact = $age = $attain = $course = $school = $batchtxt = $ref = $hiredate = $position = $exp = $prevbpo = $remarks = $nho = $nonbpo = $holdAttain = $holdPos = "";
}
else {
print '<script type="text/javascript">';
print 'alert("Please fill in all the fields!")';
print '</script>';
}
}
mysqli_close($conn);
?>
我的HTML:
<div id="addApplicant" class="addApp-marginleft" style="height:1000px">
<form id="registration" class="form-horizontal" method="post" action="index.php">
<div class="row">
<div align="center">
<h3>Add Applicant</h3>
<br>
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>First name:</small></label>
<div class="col-sm-4">
<input required type="text" name="firstname" autocomplete="off" placeholder="Firstname" id="firstname" class="form-control" value="<?php echo $fname;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small> Last name:</small></label>
<div class="col-sm-4">
<input required type="text" name="lastname" autocomplete="off" id="lastname" placeholder="Lastname" class="form-control" value="<?php echo $lname;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Middle name:</small></label>
<div class="col-sm-4">
<input required type="text" name="middlename" autocomplete="off" id="middlename" placeholder="middlename" class="form-control" value="<?php echo $mname;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Contact number:</small></label>
<span id="errmsg"></span>
<div class="col-sm-4">
<input required type="text" name="contactnum" autocomplete="off" onkeypress="return isNumber(event)" placeholder="Contact number" id="contact" class="form-control" maxlength="11" value="<?php echo $contact;?>"/>
</div>
<?php
echo '<script>';
echo 'function isNumber(evt) {';
echo 'evt = (evt) ? evt : window.event;';
echo 'var charCode = (evt.which) ? evt.which : evt.keyCode;';
echo 'if (charCode > 31 && (charCode < 48 || charCode > 57)) {';
echo 'return false;';
echo '}';
echo 'return true;';
echo '}';
echo '</script>';
?>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Age:</small></label>
<div class="col-sm-4">
<input required type="text" autocomplete="off" placeholder="age" name="age" id="age" class="form-control" value="<?php echo $age;?>">
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Graduate:</small></label>
<div class="col-sm-4">
<select required name="cmbAttain" id="cmbAttain" class="form-control" onChange="disableCmb();">
<option value="">Choose</option>
<option value="Yes" <?php if($holdAttain == "Yes") echo "selected"; ?>>Yes</option>
<option value="No" <?php if($holdAttain == "No") echo "selected"; ?>>No</option>
</select>
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>School:</small></label>
<div class="col-sm-4">
<input type="text" name="school" autocomplete="off" id="school" placeholder="School" class="form-control" value="<?php echo $school;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Course:</small></label>
<div class="col-sm-4">
<input type="text" name="course" autocomplete="off" id="course" placeholder="Course" class="form-control" value="<?php echo $course;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Batch:</small></label>
<div class="col-sm-4">
<input type="text" name="batchtxt" autocomplete="off" id="batchtxt" placeholder="Batch" class="form-control" value="<?php echo $batchtxt;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Experience:</small></label>
<div class="col-sm-4">
<input type="text" name="exp" autocomplete="off" id="exp" placeholder="Experience" class="form-control" value="<?php echo $exp;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Previous BPO:</small></label>
<div class="col-sm-4">
<input type="text" name="prevbpo" autocomplete="off" id="prevbpo" placeholder="Previous BPO" class="form-control" value="<?php echo $prevbpo;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Non-BPO:</small></label>
<div class="col-sm-4">
<input type="text" name="nonbpo" autocomplete="off" id="nonbpo" placeholder="Non-BPO" class="form-control" value="<?php echo $nonbpo;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Remarks:</small></label>
<div class="col-sm-4">
<input type="text" name="remarks" autocomplete="off" id="remarks" placeholder="Remarks" class="form-control" value="<?php echo $remarks;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Hire Date:</small></label>
<div class="col-sm-4">
<input type="date" name="hiredate" id="hiredate" class="form-control" autocomplete="off" value="<?php echo $hiredate;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Position:</small></label>
<div class="col-sm-4">
<select required name="cmbPosition" id="cmbPosition" class="form-control" data-size="5" >
<option selected value="">Choose</option>
<option value="Customer Service Representative" <?php if($holdPos == "Customer Service Representative") echo "selected"; ?>>Customer Service Representative</option>
<option value="Image Enhancer" <?php if($holdPos == "Image Enhancer") echo "selected"; ?>>Image Enhancer</option>
</select>
</div>
<div class="col-sm-4">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>NHO:</small></label>
<div class="col-sm-4">
<input type="date" name="nho" id="nho" class="form-control" autocomplete="off" value="<?php echo $nho;?>">
</div>
</div>
<div class="form-group" align="center" >
<label class="col-sm-4 control-label"><small>Referrer:</small></label>
<div class="col-sm-4">
<input type="text" name="ref" autocomplete="off" id="ref" placeholder="Name of Referrer" class="form-control" value="<?php echo $ref;?>">
</div>
</div>
<div class="form-group" align="center" >
<div class="col-sm-4">
</div>
<div class="col-sm-4">
<div class="btn-group " role="group" aria-label="...">
<input id="submitbtn" type="submit" name="submit" class="btn btn btn-success" value="Submit" data-target="#succModal">
<input type="reset" name="reset" class="btn btn-warning" value="Clear">
</div>
</div>
<div class="col-sm-4">
</div>
</div>
</form>
</div>
<div class="container">
<!-- Register Success Modal -->
<div class="modal fade" id="succModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-body" align="center">
<p>REGISTRATION SUCCESSFUL</p>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
答案 0 :(得分:0)
使用$ _POST代替$ _REQUEST ..
像:
$lname = $_POST['lname'];
答案 1 :(得分:0)
乍一看,你的NULL测试应该由&&
而不是||
分开。你拥有它的方式,只是检查它们中的一个(任何一个)是否包含一个值。
答案 2 :(得分:0)
您查询时出现一些错误,只需将其替换为
即可use CGI qw(escapeHTML);
$string = "泰国";
$string = escapeHTML($string);
$string2 = "مرحبا بالعالم";
$string2 = escapeHTML($string2);
example 1:
$VAR1 = 'Dump out string مرحبا بالعالم';
$VAR1 = 'Dump out after escapeHTML string مرحبا بالعالم';
Example 2:
$VAR1 = 'Dump out string 泰国';
$VAR1 = 'Dump out after escapeHTML string 泰▒›▒';
Example 3:
$VAR1 = 'Dump out string 泰国 - <font>TEST</font>';
$VAR1 = 'Dump out after escapeHTML string 泰▒›▒ - <font>TEST</font>';
这里,没有必要添加appid,因为它是自动递增的。还有一些缺少单引号。
答案 3 :(得分:0)
NULL
声明中发送值insert
if (isset($_POST['submit'])) {
内打印$ _POST 希望这有助于调试。如果仍然有问题,请检查您的error_log