2015-10-20 56 views
1

我正在嘗試使用之前添加的php將某些值更新到數據庫。我嘗試添加的其中一個數據是日期值。雖然第一次添加該值時,所有內容都會正確添加。當我嘗試更新日期值時,數據更改爲0000-00-00始終。所以我有以下問題:更新到null的日期

1)我犯的錯誤是什麼,日期未正確更新?

2)如何將日期的格式更改爲DD/MM/YYYY?

這是更新的代碼(我知道我的風險SQL注入,但是這不是我在這個時候關注):

<?php 
//DISPLAY A LIST OF ALL Files 
$sql = "select * from news"; 
$result = mysqli_query($mydb, $sql) or die(mysqli_error($mydb)); 

if(mysqli_num_rows($result)>0){ 
?> 

<table class="table table-striped" > 
<thead> 
<td><b>NewsID</b></td> 
<td><b>Headline</b></td> 
<td><b>Story</b></td> 
<td><b>Date</b></td> 
<td><b>Actions</b></td> 
</thead> 
<tbody> 
<?php while($rows=mysqli_fetch_array($result)){ 
    $NewsID = $rows['NewsID']; 
    $Headline = $rows['Headline']; 
    $Story = $rows['Story']; 
    $Date= $rows['Date']; 
?> 
    <tr> 
    <form method="post" id="action" action="<?php $_SERVER['PHP_SELF']?>"> 
    <?php //if update button was pressed change all plain text to textfields to enable inline editing 
    if (isset($_POST['preUpdate']) && $_POST['NewsID']==$NewsID) { ?> 
     <td><?php echo $NewsID ?></td> 
     <td><input type="text" name="Headline" value="<?php echo $Headline?>"></td> 
     <td><input type="text"name="Story" value="<?php echo $Story?>"></td> 
     <td><input type="date" name="Date" value="<?php echo $Date?>"></td> 
     <td><input type="submit" name="update" value="Save"/></td> 
     <?php } 
else { //These file will be displayed in plain text ?> 
     <td><?php echo $NewsID; ?> </a></td> 
     <td><?php echo $Headline; ?></td> 
     <td><?php echo $Story; ?></td> 
     <td><?php echo $Date; ?></td> 
     <td><input type="submit" name="preUpdate" value="Update"/> 
      <input type="submit" name="delete" value="delete"/></td> 
     <?php } // end of if update button was pressed ?> 
     <input type="hidden" name="NewsID" value="<?php echo $NewsID?>"> 

     </form> 
</tr> 
    <?php } //end of while loop that displays courses ?> 
     </tbody> 
    </table> 
<?php } 
+0

你是什麼日期列的類型? – Akshay

+2

請發佈更新表格並設置日期的代碼。我所看到的只是一個「選擇」而沒有「插入」或「更新」。 –

+0

人們可能會認爲在更新單詞的12次出現中,有1次會顯示 – Drew

回答

1

考慮到你的表日期的數據類型爲date

mysql中date的標準格式是YYYY-MM-DD,所以你應該以相同的格式存儲。您試圖以不同的格式插入,因此日期被存儲爲0000-00-00。您可以使用strtotime()將其更改爲所需的格式;

雖然存儲

$date = date("Y-m-d",strtotime($_POST['date'])); 

雖然顯示

echo date("required format",strtotime($date)); 

對於格式,請here

+0

感謝您的答案,我現在有我的日期正確的值,但由於某種原因,我得到每一行數據的相同日期,所以我會尋找一個解決方案 – Aldaron47

+0

很高興幫助你。:)如果答案有幫助,請考慮接受並提升它。 –

+0

同時插入或同時獲取? –