使用MVC结构我在TExt区域输入日期时出错 在那种形式YYYY-MM-DD Ex:2014-09-17
它插入Mysql LIke这个0000-00-00
这是视图
<html>
<head>
</head>
<body>
<div class="register">
<form action="InterventionController.php" method="POST">
<h1> Ajouter Une Date Pour L'intervention: </h1>
<input required="required" name="Date" class="input-group-addon" type="text" placeholder="Date Sous Forme : yy-mm-dd ">
<input required="required" class="btn-primary btn-lg" type="submit" name="submit" value="Ajouter L'intervention">
</form>
</div>
</body>
</html>
这是控制器
<?php
if($_POST){
if(isset($_POST['submit']) AND $_POST['submit'] == "Ajouter L'intervention" ){
$data['Date'] = $_POST['Date'];
try {
include 'models\AddInterventionModel.php';
new AddInterventionModel($data);
} catch (Exception $exc) {
echo $exc->getMessage();
}
header("location:index.php?page=Demande_Inter");
}
}
else {
include 'Login.php';
}
?>
这就是模块
<?php
class AddInterventionModel {
private $Date;
private $cnx;
function __construct($data) {
if(is_array($data)){
$this->setData($data);
}
else
{
throw new Exception("Error Data Should Be An Array");
}
// Connect To Database
$this->connectToDb();
//Insert User Data
$this->AddInterventionModelF();
}
private function setData($data)
{
$this->Date = $data['$Date'];
}
private function connectToDb() {
include 'models/Database.php';
$vars = "include/vars.php";
$this->cnx = new Database($vars);
}
function AddInterventionModelF ()
{
$query = "INSERT INTO `intervention` (`ID_I`, `DATE_I`) VALUES('','$this->Date')";
$sql = mysql_query($query);
if($sql){
echo 'Added Sussecfully';
}
else
{
throw new Exception("Error : not Added");
}
}
function close(){
$this->cnx->close();
}
}
答案 0 :(得分:2)
首先,您将input
&#34; Mname&#34;命名为,但您在控制器中呼叫$_POST['date']
(因此您必须更改&#39; Mname&#39; in& #39;日期&#39;
其次,在模块中,您要验证日期是否为数组,但日期应为字符串。
最后,考虑使用此
$dateString = $_POST['date'];
if (!empty($dateString)) {
$date = new \DateTime($dateString);
$date->setTime(0, 0, 0);
}
然后在mysql查询中:
$query = "INSERT INTO `intervention` (`ID_I`, `DATE_I`) VALUES('','".$date->format('Y-m-d H:i')."')";