我有一个文件与我的HTML表单和另一个PHP文件与下面的代码。单击提交按钮后,我能够成功连接。但是当我在phpMyAdmin控制面板中刷新我的数据库时。没有数据被推送到我的数据库。
HTML表格
<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';
$name = @$_POST ['fullname'];
$phone = @$_POST ['phonenumber'];
$email = @$_POST ['emailaddress'];
$msg = @$_POST ['message'];
$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));
}
?>
答案 0 :(得分:2)
首先使用那种结构:
if (isset($_POST['fullname'])) {
$name = $_POST['fullname'];
}
等等其他值。
或者更好的是: -
$name = isset($_POST['fullname']) ? $_POST['fullname'] : '';
这样,字段$name
总是被设置为某种东西,所以当你稍后在代码中使用它时你不会出错
答案 1 :(得分:1)
很明显,你没有传递列ID的值,以太改变了列AUTO_INCREMENT, 第1步:单击更改列ID 第2步:检查A_I(AUTO_INCREMENT)
第3步:点击保存
或者另外一种方法也可以传递ID的值,如下所示:
$name = @$_POST ['fullname'];
$phone = @$_POST ['phonenumber'];
$email = @$_POST ['emailaddress'];
$msg = @$_POST ['message'];
$id = 1 // Set value for ID
$sql = "INSERT INTO contact_form (ID, fullname, phonenumber, emailaddress, message) VALUES ($id, '$name', '$phone', '$email', '$msg')";
答案 2 :(得分:0)
从@
变量中删除$_POST
符号,您还需要删除空格。我想让您意识到,在插入SQL值之前,如果没有清理/验证这些值,请使用ctype
或filter_var
。最后,我建议您将排序规则从latin1
更改为utf8
,除非您对该字符集有特定原因。
在$phone
之前的插入值中有一些额外的空格可能导致它失败
VALUES ('$name', '$phone', '$email', '$msg')
如另一个答案中所述,在尝试使用变量之前,您始终需要验证变量中是否有数据。此外,如果您在`contact_form.php中重定向,那么使用您的失败消息进行退出非常重要,否则您可能永远不会看到它们。
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));
}
答案 3 :(得分:-1)
$sql = "INSERT INTO contact_form
(fullname, phonenumber, emailaddress, message)
VALUES ('$name', '$phone', '$email', '$msg')";
使用"
代替'