我的代码如下。请帮我纠正代码。我对语法感到困惑。另外,我不知道如果存在任何数据库问题,到底应该在哪里放置错误消息警报以及如何关闭数据库。请帮忙
<form id="ContactForm" name="form1" method="post" onsubmit="submitdata()">
<div>
<div class="formwrap"><span>Your Name:</span><input name="fname" type="text" class="input" ></div>
<div class="formwrap"><span>Your E-mail:</span><input type="text" name="femail" class="input" ></div>
<div class="formwrap"><span>Your Mobile:</span><input type="text" name="fmobile" class="input" ></div>
<div class="formwrap"><span>Your City:</span><input type="text" name="fcity" class="input" ></div>
<div class="textarea_box"><span>Your Message:</span><textarea name="fmessage" cols="1" rows="1"></textarea></div>
<a class="button" onClick="document.getElementById('ContactForm').submit()"><span class="shadow"></span>Send</a>
<a class="button" onClick="document.getElementById('ContactForm').reset()"><span class="shadow"></span>Clear</a>
</div>
</form>
<?php
function submitdata(){
include "config/connection.php";
$fdname = $_POST['fname']);
$fdemail = $_POST['femail']);
$fdmobile = $_POST['fmobile']);
$fdcity = $_POST['fcity']);
$fdmessage = $_POST['fmessage']);
$sql="INSERT INTO users (name, email, mobile, city, message)
VALUES ('$fdname', '$fdemail', '$fdmobile', '$fdcity', '$fdmessage')";
}
?>
谢谢
最佳答案
我更改了您的 html 表单上的操作,并创建了 php 脚本,该脚本将处理您的数据并将其存储到数据库中,请查看并进一步阅读准备好的语句。
<form id="ContactForm" name="form1" action="insert.php" method="post">
<div>
<div class="formwrap"><span>Your Name:</span><input name="fname" type="text" class="input" ></div>
<div class="formwrap"><span>Your E-mail:</span><input type="text" name="femail" class="input" ></div>
<div class="formwrap"><span>Your Mobile:</span><input type="text" name="fmobile" class="input" ></div>
<div class="formwrap"><span>Your City:</span><input type="text" name="fcity" class="input" ></div>
<div class="textarea_box"><span>Your Message:</span><textarea name="fmessage" cols="1" rows="1"></textarea></div>
<a class="button" onClick="document.getElementById('ContactForm').submit()"><span class="shadow"></span>Send</a>
<a class="button" onClick="document.getElementById('ContactForm').reset()"><span class="shadow"></span>Clear</a>
</div>
</form>
插入.php
<?php
if($_POST){
$db = new mysqli("address","id","password","DBname");
if (mysqli_connect_errno())
{
echo "Connection Failed: " . mysqli_connect_errno();
exit();
}
$stmt = $db->prepare("INSERT INTO users (name, email, mobile, city, message) VALUES (?, ?, ?, ?, ?)");
$stmt -> bind_param('sssss', $fname, $femail, $fmobile, $fcity, $fmessage);
$fname = $_POST['fname'];
$femail = $_POST['femail'];
$fmobile = $_POST['fmobile'];
$fcity = $_POST['fcity'];
$fmessage = $_POST['fmessage'];
$stmt -> execute();
$stmt -> close();
$db -> close();
}
?>
关于php - 对 name、id、onclick、onsubmit 感到困惑 - 我在数据库中提交数据时做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25962168/