我的網站很簡單,我想要做的就是在日期選擇器中插入一個日期到MySQL中。 (目前它插入像這樣「0000-00-00 00:00:00」)。插入日期到MySQL
我的PHP/HTML代碼是:
<form id="form_351400" class="appnitro" method="post" action="insert.php">
<div class="form_description">
<div style="width:100%;border:0px solid #000;">
<div style="float:left; width:75%;">
<h2 align="left">Samurai Digital d.o.o.</h2>
<p>
Maintenance Team
</p>
</div>
<div style="float:left; width:25%;">
<a href="index.php"><img src="samurai.png" alt="Samurai" width="150" height="150" /></a>
</div>
<div style="clear:both;"></div>
</div>
</div>
<ul >
<li id="li_0" >
<label class="description" for="element_0">Insert into DB </label>
<br/>
</li>
<li id="li_1" >
<label class="description" for="element_1" name="id" >ID </label>
<input type="text" name="id" />
</li>
<li id="li_2" >
<label class="description" for="element_3">Date </label>
<span>
<input id="element_3_1" name="element_3_1" class="element text" size="2" maxlength="2" value="" type="text">
/<label for="element_3_1">MM</label>
</span>
<span>
<input id="element_3_2" name="element_3_2" class="element text" size="2" maxlength="2" value="" type="text">
/<label for="element_3_2">DD</label>
</span>
<span>
<input id="element_3_3" name="element_3_3" class="element text" size="4" maxlength="4" value="" type="text">
<label for="element_3_3">YYYY</label>
</span>
<span id="calendar_3"> <img id="cal_img_3" class="datepicker" src="calendar.gif" alt="Pick a date."> </span>
<script type="text/javascript">
Calendar.setup({
inputField : "element_3_3",
baseField : "element_3",
displayArea : "calendar_3",
button : "cal_img_3",
ifFormat : "%B %e, %Y",
onSelect : selectDate
});
</script>
</li>
<li id="li_3" >
<label class="description" for="element_2" name="refid" >Refid </label>
<input type="text" name="refid" />
</li>
<li id="li_4" >
<label class="description" for="element_4" name="classification" >Classification </label>
<select name="classification">
<option value="SECRET">SECRET</option>
<option value="SECRET//NOFORN">SECRET//NOFORN</option>
<option value="CONFIDENTIAL">CONFIDENTIAL</option>
<option value="CONFIDENTIAL//NOFORN">CONFIDENTIAL//NOFORN</option>
<option value="UNCLASSIFIED">UNCLASSIFIED</option>
<option value="UNCLASSIFIED//FOR OFFICIAL USE ONLY">UNCLASSIFIED//FOR OFFICIAL USE ONLY</option>
</select>
</li>
<li id="li_5" >
<label class="description" for="element_5" name="origin" >Origin </label>
<input type="text" name="origin" />
</li>
<li id="li_6" >
<label class="description" for="element_6" name="destination" >Destination </label>
<input type="text" name="destination" />
</li>
<li id="li_7" >
<label class="description" for="element_7" name="header" >Header </label>
<input type="text" name="header" />
</li>
<li id="li_8" >
<label class="description" for="element_8" name="content" >Content </label> <textarea rows="15" cols="40" name="content"></textarea>
<br/>
</li>
<input type="Submit" />
</ul >
</form>
和連接到我的MySQL anohter PHP:
<?php
$id = $_POST['element_1'];
$date = $_POST['element_3_3'].'-'.$_POST['element_3_1'].'-'.$_POST['element_3_2'];
$refid = $_POST['element_2'];
$classification = $_POST['element_4'];
$origin = $_POST['element_5'];
$destination = $_POST['element_6'];
$header = $_POST['element_7'];
$content = $_POST['element_8'];
$con = mysql_connect("localhost","XXXXXX","XXXXXX");
if (!$con)
{
die('Error connecting to mysql server: ' . mysql_error());
}
mysql_select_db("XXXXXX", $con);
$sql="INSERT INTO emails (id, date, refid, classification, origin, destination, header, content)
VALUES
('$_POST[id]','$_POST[date]','$_POST[refid]','$_POST[classification]','$_POST[origin]','$_POST[destination]','$_POST[header]','$_POST[content]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>
所有其他形式的工作,我可以將它們添加,所有除日期。也許是因爲我錯誤地將DAY/MONTH/YEAR添加爲一個字符串。
日期時間的格式應該是_'YYYY-MM-DD HH:MM:SS'_你只提供_YYYY-MM-DD_變化領域_DATE_ – 2012-08-04 15:04:37
你應該閱讀有關SQL注入。您的表單非常不安全。使用預先準備好的語句,或者如果這太過分了,請使用mysql_real_escape_string – 2012-08-04 15:08:13
這是非常必要的,但不是主題,提到你絕對必須逃避你的變量!當你使用'mysql_'方法時,使用'mysql_real_escape_string($ _ POST ['variable'])'你會很好。此外,嘗試使用'mysqli_'或'PDO'方法。 – newfurniturey 2012-08-04 15:08:41