我似乎無法從網站的表單中將數據獲取到使用XAMPP在本地運行的mysql。無法將表單中的數據從表單發佈到mysql
我得到「無法POST /ph/contactFormHandler.php」錯誤。
這裏是表單的HTML:
<div class="contact-grid mdl-grid">
<div class="contact-title mdl-typography--headline">
Contact the team
</div>
<form action="../ph/contactFormHandler.php" method="post">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" size="" type="text" name="name">
<label class="mdl-textfield__label" for="name">Name</label>
</div>
</form>
<form action="../ph/contactFormHandler.php" method="post">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" size="" type="email" name="email">
<label class="mdl-textfield__label" for="email">Email</label>
</div>
</form>
<form action="../ph/contactFormHandler.php" method="post">
<div class="mdl-textfield mdl-js-textfield">
<textarea class="mdl-textfield__input" type="text" rows="6" name="message"></textarea>
<label class="mdl-textfield__label" for="message">Message</label>
</div>
</form>
<form action="../ph/contactFormHandler.php" method="post">
<input class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent" type="submit" name="send">
</form>
</div>
我一直在使用的mysqli也試過,我收到了同樣的錯誤。
的PHP:
<?php
$sql_connection = mysql_connect("localhost","root","root");
$db = mysql_select_db("mdlDemoDB", $sql_connection);
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
if(isset($_POST["send"])){
$query = mysql_query("insert into messages(ContactID, ContactName, ContactEmail, ContactMessage) values ('$name', '$email', '$message') ");
}
mysql_close($sql_connection);
?>
您正在試圖插入3個值('$ name','$ email','$ message')到4場('ContactID','ContactName', 'ContactEmail','ContactMessage')。您插入的每組值都必須與您指定的字段數匹配。目前,您現在將'ContactID'設置爲'$ name',並且沒有'ContactMessage' –
爲什麼你有一堆'form'-s? – FirstOne
[您的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) –