有人会帮我解释语法吗?
它正在运作。我添加了错误消息,一些东西肯定已经坏了。
注意:连接文件正常并且数据库已连接。 connection.php没有问题。问题出在这个代码的某个地方
<!DOCTYPE html>
<html lang="en">
<head>
<title>Lara Grant MFA </title>
<meta charset="utf-8">
<meta name="author" content="pixelhint.com">
<meta name="description" content="La casa free real state fully responsive html5/css3 home page website template"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/reset.css">
<link rel="stylesheet" type="text/css" href="css/responsive.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript" src="js/js_validation.js"></script>
<?php
ini_set('display_errors', 1); error_reporting(E_ALL);
require('connection.php');
// define variables and set to empty values
$fnameErr = $lnameErr = $artnameErr = $emailErr = $phoneErr = $stateErr = $zipErr = "";
$fname = $lname = $artname = $email = $phone = $message = $stname = $cname = $state $zip = "";
$blank = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["fname"])) {
$fnameErr = "First Name is required";
} else {
$fname = test_input($_POST["fname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
$fnameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["lname"])) {
$lnameErr = "Last Name is required";
} else {
$lname = test_input($_POST["lname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
$lnameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["artname"])) {
$artnameErr = "Art Name is required";
} else {
$artname = test_input($_POST["artname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$artname)) {
$artnameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["phone"])) {
$phone = "";
} else {
$phone = test_input($_POST["phone"]);
if (!preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $_POST['phone'])) {
$phoneErr = "Invalid phone number";
}
}
if (empty($_POST["message"])) {
$message = "";
} else {
$message = test_input($_POST["message"]);
}
if (empty($_POST["stname"])) {
$stname = "";
} else {
$stname = test_input($_POST["stname"]);
}
if (empty($_POST["cname"])) {
$cname = "";
} else {
$cname = test_input($_POST["cname"]);
}
if (empty($_POST["state"])) {
$state = "";
} else {
$state = test_input($_POST["state"]);
if (!preg_match("/^[a-zA-Z]{2}$/",$state)) {
$stateErr = "Only two letters allowed";
}
}
if (empty($_POST["zip"])) {
$zip = "";
} else {
$zip = test_input($_POST["zip"]);
if (!preg_match('/^[0-9]{5}$/', $_POST['zip'])) {
$zipErr = "Invalid ZIP";
}
}
if ($fnameErr == $blank && $lnameErr == $blank && $artnameErr == $blank && $emailErr == $blank && $phoneErr == $blank && $stateErr == $blank && $zipErr == $blank) {
$que1 = "INSERT INTO paintings2 (ArtName,FirstName,LastName,Email,Phone,Message,Street,City,State,ZIP) VALUES('$artname','$fname','$lname','$email','$phone','$message','$stname','$cname','$state','$zip')";
if ($conn->query($que1) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $que1 . "<br>" . $conn->error;
}
$conn->close();
}
//VALUES('$artname','$fname','$lname','$email','$phone','$message','$stname','$cname','$state','$zip')";
//,Message,Street,City,State,ZIP
// $fnameErr = $lnameErr = $artnameErr = $emailErr = $phoneErr = ""
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<section class="hero">
<header>
<div class="wrapper"><a href="#" class="hamburger"></a>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="paintingform.php">Painting Inquiry Form</a></li>
<li><a href="allpaintings.html">All Paintings</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
<a href="tel:+14109806451" class="login_btn">Call Me!</a>
</nav>
</div>
</header><!-- end header section -->
<section class="caption">
<h2 class="caption">Lana Grant - Master of Fine Art</h2>
<h3 class="properties">Universal Fine Art and Design Art</h3>
</section>
</section><!-- end hero section --><!-- end search section -->
<section class="listings">
<div class="wrapper">
<center>
<h2 class="caption">Painting Inquiry Form</h2>
<br><br>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table width="30%" border="0" align="center" cellpadding="10" cellspacing="20">
<tr>
<td width="40%"> <span style="font-size: 22px">First Name:*</span><br><br /></td>
<td width="60%"><input type="text" name="fname" /> <br><?php echo $fnameErr;?></td>
</tr>
<tr>
<td width="20%"> <span style="font-size: 22px">Last Name:*</span><br>
<br /></td>
<td width="80%"><input type="text" name="lname" /> <br><?php echo $lnameErr;?></td>
</tr>
<tr>
<td><span style="font-size: 22px">Art Name:* <br>
<br></span></td>
<td><select name="artname">
<option value="">Select...</option>
<option value="DepthsofUnknown">Depths of Unknown</option>
<option value="IncredibleLife">Incredible Life</option>
<option value="NightoftheComet">Night of the Comet</option>
<option value="ItHappened">It Happened</option>
<option value="ApplesFromHeaven">Apples From Heaven</option>
</select>
<br><?php echo $artnameErr; ?></td>
</tr>
<tr>
<td><span style="font-size: 22px">Email:*</span> <br>
<br></td>
<td><input type="text" name="email" /><br><?php echo $emailErr; ?></td>
</tr>
<tr>
<td><span style="font-size: 22px">Phone:*</span> <br>
<br></td>
<td><input type="number" name="phone" /><br><?php echo $phoneErr; ?></td>
</tr>
<tr>
<td valign="top"><span style="font-size: 22px">Message:</span></td>
<td><input type="text" name="message" /></td>
</tr>
<tr>
<td><span style="font-size: 22px">Street:</span></td>
<td><input type="text" name="stname"/></td>
</tr>
<tr>
<td><span style="font-size: 22px">City:</span></td>
<td><input type="text" name="cname" /></td>
</tr>
<tr>
<td><span style="font-size: 22px">State:</span></td>
<td><input type="text" name="state" size="2" width="60"/><br><?php echo $stateErr; ?></td>
</tr>
<tr>
<td><span style="font-size: 22px">Zip:</span></td>
<td><input type="text" name="zip" size="5" width="100"/><br><?php echo $zipErr; ?></td>
</tr>
</table>
<br><br>
<input class="button-0" type="submit" value="Submit" />
<br>
</form>
</center>
</div>
</section> <!-- end listing section -->
<footer>
<div class="wrapper footer">
<ul>
<li class="links">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="paintingform.html">Painting Form</a></li>
<li><a href="allpaintings.html">All Paintings</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</li>
<li class="links">
<ul>
<li><a href="#">Address: </a></li>
<li><a href="#">520 W. 183 Street</a></li>
<li><a href="#">New York</a></li>
<li><a href="#">10033</a></li>
<li><a href="#"></a></li>
</ul>
</li>
<li class="links">
<ul>
<li><a href="#">Contact:</a></li>
<li><a href="tel:+14109806451">(410)980-6451</a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
</ul>
</li>
<li class="about">
<p>Vastness of mind, spirit and beauty of the everlasting interconnection between cosmos and earth.</p>
<ul>
<li><a href="http://facebook.com/lanagrant" class="facebook" target="_blank"></a></li>
<li><a href="#" class="twitter" target="_blank"></a></li>
<li><a href="#" class="google" target="_blank"></a></li>
<li><a href="#" class="skype"></a></li>
</ul>
</li>
</ul>
</div>
<div class="copyrights wrapper">
Copyright © 2015 <a href="index.html"> Lana Grant</a> <a href="http://pixelhint.com" target="_blank" class="ph_link" title="Download more free Templates">Credits: Pixelhint.com</a>. All Rights Reserved.
</div>
</footer><!-- end footer -->
</body>
</html>
答案 0 :(得分:3)
此行中有错误:
$fname = $lname = $artname = $email = $phone = $message = $stname = $cname = $state $zip = "";
在=
和$state
变量之间缺少相同的$zip
符号。
更改为:
$fname = $lname = $artname = $email = $phone = $message = $stname = $cname = $state = $zip = "";