我無法將表單發送到我的數據庫。我試了一個小時來解決問題;我用「echo mysqli_error();」解決了我的代碼但沒有收到任何錯誤,只是缺少新的數據到我的數據庫(看起來好像提交表單不重新加載頁面發送信息)。該數據庫適用於我的其餘頁面使用config.php,connectDB.php和header.php,所以問題不在數據庫設置。我相信這個錯誤是在「isset($ _ POST ['submit'])」內,因爲它不會使用header()重新加載頁面,但是我提供了其餘的代碼以防萬一。 (我知道這個代碼不針對SQL注入的保護,表單驗證是下一步我解決這個問題之後):執行未將數據輸入數據庫的PHP代碼
<?php
require_once ("Includes/config.php");
require_once ("Includes/connectDB.php");
include("Includes/header.php");
if (isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$content = $_POST['content'];
$query = "INSERT INTO requests (name, email, content) VALUES (?, ?, ?)";
$statement = $databaseConnection->prepare($query);
$statement->bind_param('sss', $name, $email, $content);
header('Location: /index.php');
$statement->execute()
$statement->store_result();
setcookie("nameErr"," ", time()+3600);
setcookie("emailErr"," ", time()+3600);
setcookie("contentErr"," ", time()+3600);
setcookie("contentSucc","Announcement Request Successful", time()+3600);
header('Location: /request.php');
}
?>
<div id="main">
<ol>
<li>
<label for="name">Name:</label>
<input type="text" name="name" value="" id="name" />
<span class="error">* <?php if (isset($_COOKIE["nameErr"])){echo $_COOKIE["nameErr"];}?></span>
</li>
<li>
<label for="email">Email:</label>
<input type="text" name="email" value="" id="email" style = "position: relative; left: 3px;"/>
<span class="error" style = "position: relative; left: 3px;">* <?php if (isset($_COOKIE["emailErr"])){echo $_COOKIE["emailErr"];}?></span>
</li>
<li>
<label for="content">Requested Announcement:</label><br>
<textarea rows="18" cols="140" name="content" id="content"></textarea>
<br>
<span class="error"> <?php if (isset($_COOKIE["contentErr"])){echo $_COOKIE["contentErr"];}?></span>
<span class="error"> <?php if (isset($_COOKIE["contentSucc"])){echo $_COOKIE["contentSucc"];}?></span>
</li>
</ol>
<input type="submit" name="submit" value="Submit " style = "position: relative; left: 40px;" />
<!-- onclick="window.location='request.php';" -->
<p>
<a class="cancel" href="index.php" style = "position: relative; left: 40px;">Cancel</a>
</p>
</div>
你似乎並不有一個''
這是當你試圖用代碼會發生什麼3個小時的睡眠。對不起,我會進行修改。謝謝! – AGentleRose
我對'execute()'之前的'header()'調用持懷疑態度。你的服務器錯誤日誌說什麼? –