作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 2 个 php 文件...一个带有 html 表单,另一个名为 contact_form.php。每次点击提交按钮时,我都会收到此错误:
解析错误:语法错误,/contact_form.php 第 24 行出现意外的“}”
...并且没有数据保存到我的数据库中...任何人都可以帮助我让此代码正常工作...谢谢
表格
<form class="well" id="contactForm" name="sendMsg" novalidate="" action="contact_form.php" method="post">
<div class="control-group">
<div class="controls">
<input class="form-control" name="fullname" id="name" type="text" placeholder="Full Name" required="" data-validation-required-message="Please enter your name" />
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="form-control" name="phonenumber" id="phone" type="text" placeholder="Phone Number" required="" data-validation-required-message="Please enter your phone number" />
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="form-control" name="emailaddress" id="email-address" type="email" placeholder="Email Address" required="" data-validation-required-message="Please enter your email" />
</div>
</div>
<div class="control-group">
<div class="controls">
<textarea rows="6" class="form-control" name="message" id="msg" type="msg" placeholder="Enter detailed question/concern, and we will get back to you." required="" data-validation-required-message="Please enter your question/concern"></textarea>
</div>
</div>
<div class="control-group">
<div class="controls submit-btn">
<button class="btn btn-primary" type="submit" value="Submit">Submit</button>
</div>
</div>
</form>
CONTACT_FORM.PHP
<?php
define('DB_NAME', 'xxx');
define('DB_USER', 'xxx');
define('DB_PASSWORD', 'xxx');
define('DB_HOST', 'xxx');
$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$connection){
die('Database connection failed: ' . mysqli_connect_error());
}
$db_selected = mysqli_select_db($connection, DB_NAME);
if(!$db_selected){
die('Can\'t use ' .DB_NAME . ' : ' . mysqli_connect_error());
}
echo 'Connected successfully';
if($_POST['fullname']){
$name = $_POST['fullname']
}else{
echo "name not received";
exit;
}
if($_POST['phonenumber']){
$phone = $_POST['phonenumber']
}else{
echo "phone not received";
exit;
}
if($_POST['emailaddress']){
$email = $_POST['emailaddress']
}else{
echo "email not received";
exit;
}
if($_POST['message']){
$msg = $_POST['message']
}else{
echo "message not received";
exit;
}
$sql = "INSERT INTO contact_form (fullname, phonenumber, emailaddress, message)
VALUES ('$name', '$phone', '$email', '$msg')";
if (!mysqli_query($connection, $sql)){
die('Error: ' . mysqli_connect_error($connection));
}
?>
数据库屏幕截图
最佳答案
您有很多类似的代码:
if($_POST['fullname']){
$name = $_POST['fullname']
}else{
echo "name not received";
exit;
}
$name是一个php变量的赋值,在变量中存储值后需要加上分号,例如:
$name = $_POST['fullname'];
对所有变量执行此操作并重试。
关于php - 如何将 HTML 表单数据提交到 MySql 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26891559/
我是一名优秀的程序员,十分优秀!