我正在尝试提交提交表单。如果字段为空但它没有将数据插入表中,并且在提交后只重新加载带有标题的页面,则会返回错误。我不知道什么是错的,因为没有错误被回应
我的验证
<?php
// define variables and initialize with empty values
$nameErr = $comErr = $catErr =$priceErr =$linkErr ="";
$name = $description = $category = $price = $link = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
print_r($_POST);
if ($_POST["name"] == "") {
$nameErr = "Name the app";
}
else {
$name= $_POST["name"];
}
if ($_POST["price"] == "") {
$priceErr = "Price the app";
}
else {
$price= $_POST["price"];
}
if ($_POST["link"] == "") {
$linkErr = "Link the app";
}
else {
$link= $_POST["link"];
}
if ($_POST["category"] == "") {
$catErr = "Missing";
}
else {
$category = $_POST["category"];
}
if (empty($_POST["description"])) {
$comErr = "Missing";
}
else {
$description= $_POST["description"];
}
if (empty($rateErr) && empty($comErr) && empty($catErr)&& empty($linkErr) && empty($priceErr)) {
try {
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "INSERT INTO apps (app_name, category, price, link, description, date_added) VALUES (:name, :category, :price, :link, :description, :date)";
$stmt = $con->prepare( $sql );
$stmt->bindValue( ":name", $name);
$stmt->bindValue( ":category", $category);
$stmt->bindValue( ":price", $price);
$stmt->bindValue( ":link", $link);
$stmt->bindValue( ":description", $description);
$stmt->bindValue( ":date", now());
$stmt->execute();
echo "Submitted successfully";
}catch( PDOException $e ) {
echo $e->getMessage();
}
}
}
?>
表格
<form method="POST"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
App name <input type="text" name="name" value="<?php echo htmlspecialchars($name);?>">
<span class="error"><?php echo $nameErr;?></span>
<br />
Price <input type="text" name="price" value="<?php echo htmlspecialchars($price);?>">
<span class="error"><?php echo $priceErr;?></span>
<br />
Download link <input type="text" name="link" value="<?php echo htmlspecialchars($link);?>">
<span class="error"><?php echo $linkErr;?></span>
<br />
<select name="category">
<option value=""></option>
<option value="maths">maths</option>
<option value="driving">driving</option>
<option value="languages">languages</option>
<option value="literature">literature</option>
<option value="science">science</option>
<option value="psychology">psychology</option>
<option value="psychology">biology</option>
<option value="IT">IT</option>
<option value="other">Other</option>
</select>
<span class="error"><?php echo $catErr;?></span>
<br />
<textarea rows="4" cols="50" name="description" value="<?php echo htmlspecialchars($description);?>">
Enter text here...</textarea>
<span class="error"><?php echo $comErr;?></span>
<input type="submit" name="submit" value="Submit">
</form>
答案 0 :(得分:1)
你没有回应任何错误。 您正在返回&#34;已成功提交&#34;但你不是在一个功能。 你应该放置echo&#34;成功提交&#34;如果你想看到这条消息。
似乎你的代码没有通过$ _SERVER [&#34; REQUEST_METHOD&#34;]。
尝试使用if(isset($ _ POST))代替并使用print_r($ _ POST)打印$ _POST, 只是为了验证您的表单是否正确发布数据。
答案 1 :(得分:0)
检查数据库连接代码
$con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
替换为下面的一个,它正在工作,值插入...可能这可能也是一个问题
$con = new PDO('mysql:host=localhost;dbname=test', $user, $pass);