我正在处理一个项目,它有一个表单,应该验证自己,然后提交数据到MySQL数据库。但我面临着一个错误。
这是表格
'
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<table style="line-height: 50px;">
<tr>
<th>Name </th>
<td><input type="text" name="name" placeholder="Your Name" style="height:30px; border:1px; width:300px; border-radius:5px; text-indent:15px"><span class="error">* <?php echo $nameErr;?></span></td>
</tr>
<br>
<tr>
<th>Phone </th>
<td><input type="text" name="contact" placeholder="Your Contact Number" style="height:30px; border:1px; width:300px; border-radius:5px; text-indent:15px"><span class="error">* <?php echo $contactErr;?></span></td>
</tr>
<tr>
<th>City </th>
<td><input type="text" name="city" placeholder="Your City Name" style="height:30px; border:1px; width:300px; border-radius:5px; text-indent:15px"><span class="error">* <?php echo $cityErr;?></span></td>
</tr>
<tr>
<th>Service </th>
<td><select name="service" autocomplete="off" style="height:30px; border:1px; width:300px; border-radius:5px; text-indent:15px">
<option value="">Select your service</option>
<option value=service1>Service 1</option>
<option value=service2>Service 2</option>
<option value=service3>Service 3</option>
<option value=service4>Service 4</option>
</select><span class="error">* <?php echo $serviceErr;?></span></td>
</tr>
</table>
<input type="submit" name="submit" value="Submit" style="height: 40px; width: 140px; border-radius: 5px; margin-left: 140px;margin-top: 20px;">
</form>
'
这是验证脚本
'
<?php
// define variables and set to empty values
$nameErr = $contactErr = $cityErr = $serviceErr = "";
$name = $contact = $city = $service = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["contact"])) {
$contactErr = "Contact is required";
} else {
$contact = test_input($_POST["contact"]);
// check if contact number is well-formed
if (!preg_match("/^[0-9+]*$/",$contact)) {
$contactErr = "Phone number should contain only numbers";
}
}
if (empty($_POST["city"])) {
$cityErr = "City is required";
} else {
$city = test_input($_POST["city"]);
// check if city is valid
if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
$cityErr = "Only letters and white space allowed";
}
}
if (empty($_POST["service"])) {
$serviceErr = "Service is required";
} else {
$service = test_input($_POST["service"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
'
这很好地验证了表单,但我面临的问题是在验证后将表单提交到数据库。
这是我的upload_file. php代码。
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="morningstar"; // Mysql password
$db_name="infoline"; // Database name
$tbl_name="user_profile"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$name=$_POST['name'];
$city=$_POST['city'];
$contact=$_POST['contact'];
$service=$_POST['service'];
// Insert data into mysql
$sql="INSERT INTO user_profile(name, city, contact, service)VALUES('$name', '$city', '$contact', '$service')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='index.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysql_close();
?>
有人能帮我解决这个问题吗?我想在正确的验证之后将表单提交到数据库中。请帮助我。
1条答案
按热度按时间8mmmxcuj1#
如果我没理解错的话,你就是不知道如何调用另一个文件,我建议你把它转换成一个函数,包括,然后调用它,就像这样:
首先,我为语法错误道歉,这是徒手画的。
其次,我对您的代码有几点建议:
mysql_*
函数,因为它们已经过时了。我喜欢PDO,所以这就是我要建议研究的。(谷歌一下就知道了,有很多)这也将允许您使用预准备语句,这是mysql_*
函数缺少的最大东西之一。希望这是你需要的。