我修改了表单和php脚本,但我还没有结果。现在当我填写表单然后按下按钮,我只是在Apache Web Server中看到一个白色的屏幕,这里是新的代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert</title>
</head>
<body>
<form action="insertform3.php" method="post">
Name: <input type="text" name="name"><br>
Phone: <input type="text" name="phonenumber"><br>
Birhtday: <input type="date" name="birthday"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
这是PHP表单:
<?php
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', 'password');
define('DBNAME', 'database');
$db = new mysqli(HOST, USER, PASS, DBNAME);
if ($db->connect_errno) {
echo "Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error;
}
if ($_POST['submit']) {
$name = $_POST['name'];
$phonenumber = $_POST['phonenumber'];
$birthday = $_POST['birthday'];
if (!empty($name) && !empty($phonenumber) && !empty($birthday)) {
$query = "INSERT INTO form_data (name,phonenumber,birthday)
VALUES(?,?,?)";
$conn = $db->prepare($query);
if ($conn->execute(array(
$name,
$phonenumber,
$birthday
))) {
header('location: http://localhost/insertform-2.html');
} else {
echo 'error';
}
$db->close();
}
}
?>
答案 0 :(得分:0)
<?php
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', 'xxxxxxxxx');
define('DBNAME', 'database');
$db = new mysqli(HOST, USER, PASS, DBNAME);
if ($db->connect_errno) {
echo "Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error;
}
if ($_POST['submit']) {
$name = $_POST['name'];
$phonenumber = $_POST['phonenumber'];
$birthday = $_POST['birthday'];
if (!empty($name) && !empty($phonenumber)) {
$query = "INSERT INTO formdatabase (name,phonenumber,bithday)
VALUES(?,?,?)";
$conn = $db->prepare($query);
if ($conn->execute(array(
$name,
$phonenumber,
$birthday
))) {
header('location: http://localhost/insertform-2.html');
} else {
echo 'error';
}
$db->close();
}
}
?>
答案 1 :(得分:-1)
您必须确保以正确格式yyy-mm-dd输入生日,并在您的数据库中将生日期设置为DATE类型。
$name = $_POST['name'];
$phonenumber = $_POST['phonenumber'];
$birthdate = $_POST['birthdate'];
if (!empty($name) && !empty($phonenumber) && !empty($birthdate)) {
$query = "INSERT INTO formdatabase (name,phonenumber)
VALUES($name,$phonenumber,$birthdate)";