我有两个php文件,表单和结果页面。表单页面接受用户信息,结果页面将信息插入数据库并显示它。
表单页码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Record Insertion Form</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php
echo "<div style='font-size: large; font-family: sans-serif'><center><h1>
<p style='color: white; background-color: black'>Resume Generator</p></h1></center> </div>";
echo "Mandotary fields with <font color='red'>*</font> are required!";
?>
<form id="form1" name="form1" method="post" action="resumeinsert.php">
<fieldset>
<legend>Personal Details</legend>
<input type="hidden" name="id" id="id" />
<br class="clear" />
<label for="name"><font color="red">*</font> Name:</label><input type="text" name="name" id="name" />
<br class="clear" />
<label for="gender"><font color="red">*</font> Gender:</label>
<input type="radio" name="gender" value="Male" id="gender_0" />Male
<input type="radio" name="gender" value="Female" id="gender_1" />Female
<br class="clear" />
<label for="dateofbirth"><font color="red">*</font> Date of birth:</label><input type="text" name="dateofbirth" id="dateofbirth" />
<br class="clear" />
<label for="placeofbirth"><font color="red">*</font> Place of birth :</label><input type="text" name="placeofbirth" id="placeofbirth" />
<br class="clear" />
<label for="address"><font color="red">*</font> Address:</label><textarea name="address" id="address" cols="45" rows="5"></textarea>
<br class="clear" />
</fieldset>
<fieldset>
<legend><b>Educational Details</b></legend>
<label for="schoolname"><font color="red">*</font> School name:</label><input type="text" name="schoolname" id="schoolname" size=50 />
<br class="clear" />
<label for="qualification"><font color="red">*</font> Qualification(s):</label><textarea name="qualification" id="qualification" cols="45" rows="5"></textarea>
<br class="clear" />
<label for="skills1"><font color="red">*</font> Skills 1:</label><textarea name="skills1" id="skills1" cols="45" rows="5"></textarea>
<br class="clear" />
<label for="skills2">Skills 2:</label><textarea name="skills2" id="skills2" cols="45" rows="5"></textarea>
<br class="clear" />
</fieldset>
<fieldset><legend><b>Other Details</b></legend>
<label for="awards">Awards:</label><textarea name="awards" id="awards" cols="45" rows="5"></textarea>
<br class="clear" />
<label for="Volunteer">Volunteer:</label><textarea name="Volunteer" id="Volunteer" cols="45" rows="5"></textarea>
<br class="clear" />
<input type="submit" name="submit" id="submit" value="Submit" />
<br class="clear" />
</fieldset>
</form>
</body>
</html>
resumeinsert.php页面代码:
<?php
$mysqli = mysqli_connect("localhost", "hecton", "ccna", "joomladb");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$id = $_POST['id'];
$name = $_POST['name'];
$gender = $_POST['gender'];
$dateofbirth = $_POST['dateofbirth'];
$placeofbirth = $_POST['placeofbirth'];
$address = $_POST['address'];
$schoolname = $_POST['schoolname'];
$qualification = $_POST['qualification'];
$skills1 = $_POST['skills1'];
$skills2 = $_POST['skills2'];
$awards = $_POST['awards'];
$Volunteer = $_POST['Volunteer'];
$query = " INSERT INTO j71mi_resumeinfo ( id, name, gender, dateofbirth, placeofbirth, address, schoolname, qualification, skills1, skills2, awards, Volunteer ) VALUES ( '$id', '$name', '$gender', '$dateofbirth', '$placeofbirth', '$address', '$schoolname', '$qualification', '$skills1', '$skills2', '$awards', '$Volunteer' ) ";
$result = mysqli_query($mysqli, $query);
if ($result === TRUE) {
echo "A record has been successfully inserted into the database!.";
echo "<b><h1><center>My Resume</h1></b></center>";
echo "<div style='font-size: large; font-family: sans-serif'>
<p style='color: white; background-color: black'>Personal Details</p></div>";
echo "<br>";
echo "<b>Name:</b>".$_POST['name'];
echo "<br>";
echo "<b>Gender:</b>".$_POST['gender'];
echo "<br>";
echo "<b>Date of Birth:</b>".$_POST['dateofbirth'];
echo "<br>";
echo "<b>Place of Birth:</b>".$_POST['placeofbirth'];
echo "<br>";
echo "<b>Home Address:</b>".$_POST['address'];
echo "<div style='font-size: large; font-family: sans-serif'>
<p style='color: white; background-color: black'>Educational Details</p></div>";
echo "<br>";
echo "<b>School Name:</b>".$_POST['schoolname'];
echo "<br>";
echo "<b>Qualification(s):</b>".$_POST['qualification'];
echo "<br>";
echo "<b>Skill 1:</b>".$_POST['skills1'];
echo "<br>";
echo "<b>Skill 2:</b>".$_POST['skills2'];
echo "<br>";
echo "<div style='font-size: large; font-family: sans-serif'>
<p style='color: white; background-color: black'>Other Details</p></div>";
echo "<br>";
echo "<b>Award(s):</b>".$_POST['awards'];
echo "<br>";
echo "<b>Volunteer Activity:</b>".$_POST['Volunteer'];
echo "<br>";
echo "<br>";
echo "<b><h5><center>End of Resume</h5></b></center>";
} else {
printf("Could not insert record: %s\n", mysqli_error($mysqli));
}
mysqli_close($mysqli);
}
?>
我的问题是用户有时提交表单甚至没有填写所有字段,它显示&#34;名称:&#34;,其中不包含任何内容。
2.数据库中的某些字段设置为NULL,这意味着该字段是可选的。如果用户在表单中没有输入任何内容,我怎么能不在显示页面中显示该特定字段?
答案 0 :(得分:1)
您必须验证表单。使用简单检查来确定字段是否正确填充。检查空的,字母数字,以及您需要的其他内容。在将数据发送到服务器之前,最好检查前端。以下是一些可以帮助您入门的示例:
JS表单验证 http://www.w3schools.com/js/js_form_validation.asp
JS表单验证 http://www.tizag.com/javascriptT/javascriptform.php
PHP表单验证 http://www.w3schools.com/php/php_form_validation.asp
查看这些示例并实现与此类似的内容。即使您最终在JS中验证表单,也不要忘记在PHP中实现最低限度的检查。