通常我看到的原因是混合了mysql和mysqli或者列和字段不匹配的人,但这不是这里的情况。 按下提交按钮时没有错误。 错误报告显示没有错误。 我已经尝试了一个提议的解决方案,它说要改变数据格式,但没有做任何修复它。
http://imgur.com/vh7OAvD& http://imgur.com/vTtEnSN应该澄清一下
指数:
<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Custom Version</title>
</head>
<body>
<form action="add.php" method="post">
<p>
<label for="project_number">Project Number:</label>
<input type="number" name="project_number" id="project_number">
</p>
<p>
<label for="project_name">Project Name:</label>
<input type="text" name="project_name" id="project_name">
</p>
<p>
<label for="project_desc">Project Description:</label>
<input type="text" name="project_desc" id="project_desc">
</p>
<p>
<label for="project_start">Project Start Date:</label>
<input type="date" name="project_start" id="project_start">
</p>
<p>
<label for="project_end">Project End Date:</label>
<input type="date" name="project_end" id="project_end">
</p>
<input type="submit" value="Submit">
</form>
</body>
</html>
Add.php
<?php
session_start();
include_once("header.php");
?>
<html>
<body>
<?php
mysqli_select_db($con,$DATABASE);
$sql="INSERT INTO project (project_number,project_name,project_desc,project_start,project_end)VALUES('$_POST[project_number]','$_POST[project_name]','$_POST[project_desc]','$_POST[project_start]','$_POST[project_end]')";
echo $sql;
if($con = new mysqli($SERVER, $USER, $PASS, $DATABASE)){ echo "success"; }else{ echo "error: " . mysqli_error($con); }
mysqli_close($con);
?>
</body>
</html>
的header.php
<?php
$SERVER = 'localhost';
$USER = 'user';
$PASS = 'pass';
$DATABASE = 'database';
error_reporting(E_ALL); ini_set('display_errors', 1);
// Create connection
$con = new mysqli($SERVER, $USER, $PASS);
// Check connection
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
echo "Connected successfully";
?>
更新:解决方案是通过添加:
来执行SQLif ($con->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
答案 0 :(得分:1)
问题在于这条线,
$sql="INSERT INTO project (project_number,project_name,project_desc,project_start,project_end)
VALUES('$_POST'[project_number]','$_POST[project_name],'$_POST[project_desc],'$_POST[project_start]','$_POST[project_end]')";
^ see the single quote here ^ missing quote here ^ missing quote here
所以你的查询应该是这样的:
$sql="INSERT INTO project (project_number,project_name,project_desc,project_start,project_end)VALUES('$_POST[project_number]','$_POST[project_name]','$_POST[project_desc]','$_POST[project_start]','$_POST[project_end]')";
旁注:您的查询容易受到SQL注入的影响。始终准备,绑定并执行您的查询以防止任何类型的SQL注入。 And this is how you can prevent SQL injection in PHP
<强>更新强>
我如何修复日期格式?
这里是将日期格式更改为MySQL DATE
类型的功能。
// 03/22/2000 ==> 2000-03-22
function mysql_date_format($date){
$unixdatetime = DateTime::createFromFormat('m/d/Y', $date)->getTimestamp();
$formatted_date = strftime("%Y-%m-%d",$unixdatetime);
return $formatted_date;
}
现在使用 project_start 和 project_end 日期调用此函数,如下所示:
$project_start = mysql_date_format($_POST['project_start']);
$project_end = mysql_date_format($_POST['project_end']);
并在INSERT
查询中使用这些格式化的值,如下所示:
$sql="INSERT INTO project (project_number,project_name,project_desc,project_start,project_end)VALUES('$_POST[project_number]','$_POST[project_name]','$_POST[project_desc]','$project_start','$project_end')";
答案 1 :(得分:0)
试试这个,你错过了$_POST value $_POST['project_number']
中的mysql日期格式和单引号,而不是$_POST[project_number]
<?php
session_start();
include_once("header.php");
?>
<html>
<body>
<?php
$start_date = date('Y-m-d',strtotime($_POST['project_start']));
$end_date = date('Y-m-d',strtotime($_POST['project_end']));
mysqli_select_db($con,$DATABASE);
$sql="INSERT INTO project (project_number,project_name,project_desc,project_start,project_end)VALUES('$_POST['project_number']','$_POST['project_name']','$_POST['project_desc']','$start_date','$end_date')";
mysqli_query($con,$sql);
mysqli_close($con);
?>