我也看到过类似的主题,但是在尝试了上面提到的一些解决方案之后,我仍然没有找到正确的答案。我是mysql和php的新手,我很确定在某个地方有一个我看不到的小错误,但是我已经为这个问题挣扎了几天了,所以我决定问一下。
问题是,当我创建一个html表单和方法将表单中的值插入数据库时,我得到了“成功”的插入,但是添加到数据库中的所有行仍然是空的。
html表单:
<div class="panel panel-default">
<div class="panel-body">
<form action="InsertMessage.php" method="post">
<div class="form-group">
<label for="comment">First name:</label>
<textarea class="form-control" id="comment" name=first_name rows="1" required></textarea>
</div>
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" name="email" id="email" required>
</div>
<div class="form-group">
<label for="date">Date:</label>
<input type="date" class="form-control" name="desired_date" id="desired_date" required>
</div>
<div class="form-group">
<label for="time">Time:</label>
<input type="time" class="form-control" name="desired_time" id="desired_time" required>
</div>
<div class="form-group">
<label for="comment">Comment</label>
<textarea class="form-control" id="comment" name="info" rows="3" placeholder="placeholder" required></textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
php代码:
$host = "localhost";
$username = "root";
$password = "";
$database = "name";
//create a connection
$conn = new mysqli($host, $username, $password, $database);
$first_name = filter_input(INPUT_POST, 'first_name');
$email = filter_input(INPUT_POST, 'email');
$desired_date = filter_input(INPUT_POST, 'desired_date');
$desired_time = filter_input(INPUT_POST, 'desired_time');
$info = filter_input(INPUT_POST, 'info');
if(mysqli_connect_error()){
die('Connection error (' . mysqli_connect_errno() . ')' .
mysqli_connect_error());
}else{
$sql = "INSERT INTO messages (first_name, email, desired_date,
desired_time, info) VALUES
('$first_name','$email','$desired_date','$desired_time','$info')";
if ($conn->query($sql)){
echo "New record is inserted successfully!";
}
else{
echo "Error:" . $sql . $conn->error;
}
$conn->close();
}
phpmyadmin截图:
1条答案
按热度按时间rqdpfwrv1#
试试这个
//创建连接