无法在php中将日期存储到数据库

时间:2015-03-09 06:02:44

标签: php mysql database mysqli

<!DOCTYPE html>  
<html>  
<head>  
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/  
jquery-ui.css" rel="stylesheet" type="text/css"/>  
<script type="text/javascript"   
src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>  
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js">  
</script>  

<script>
$(document).ready(function() {
$(".datepicker").datepicker()
});
</script>
</head>
<body>
<?php
$status = $_POST['status'];  
$driver_name= $_POST['driver_name'];  
$from=date('y-m-d',strtotime($_POST['date_from']));  
$to=date('y-m-d',strtotime($_POST['date_to']));  
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or  
    die("Database not connected" . mysqli_error());
if(isset($_POST['sub'])) {
foreach($status as $k=>$s){
    $ins = "insert into driver_status(driver_name,status,date_from,date_to) 
VALUES                   
        ('".$driver_name[$k]."','$s','".$from[$k]."','".$to[$k]."')";
        $quer=mysqli_query($conn,$ins);
        }
    if($quer){
        echo "Updated";
    }else{
        echo"NOT".mysqli_error($conn);
    }
 }

$sel = 'select Driver_name from driver_master';
$query = mysqli_query($conn, $sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){    
 echo "<tr><td>".$row['Driver_name']
       ."<input type=\"hidden\" name=\"driver_name[]\"
 value=\"".$row['Driver_name']."\"/></td>";
 $sel1='select d_status from status';
 $query1=mysqli_query($conn,$sel1);
 echo "<td><select name=\"status[]\">";
 while($row1=mysqli_fetch_assoc($query1)){
       echo "<option value=\"".$row1['d_status']."\">".$row1['d_status']."  
 </option>";
 }
 echo "</select></td>";
 echo "<td>".'<input type="text" name="date_from[]" class="datepicker">'."</td>";
 echo "<td>".'<input type="text" name="date_to[]" class="datepicker">'."</td>";
 echo "</tr>";
 }
 echo "</table>";
 echo '<input type="submit" name="sub" value="Update"/>';
 echo "</form>";
 ?>
 </body>
 </html>

我想从jquery datepicker将日期存储到数据库。上面是我的代码,错误是警告:strtotime()期望参数1是字符串,给定数组。我可以将日期存储到数据库中作为array.Plz帮助。

2 个答案:

答案 0 :(得分:1)

将输入名称更改为: -

echo "<td>".'<input type="text" name="date_from" class="datepicker">'."</td>"; //  remove []
echo "<td>".'<input type="text" name="date_to" class="datepicker">'."</td>"; //  remove []

像这样使用Dateformat

Try: date("Y-m-d") which uses the numeric equivalents.

答案 1 :(得分:1)

@vove This is code-
<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/
jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js">
</script>

<script>
$(document).ready(function() {
$(".datepicker").datepicker()
});
</script>
</head>
<body>
<?php
$status = $_POST['status'];
$driver_name= $_POST['driver_name'];
$from=date('y-m-d',strtotime($_POST['date_from']));
$to=date('y-m-d',strtotime($_POST['date_to']));
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or
    die("Database not connected" . mysqli_error());
if(isset($_POST['sub'])) {

    $ins = "insert into driver_status(driver_name,status,date_from,date_to)
  VALUES                   
        ('$driver_name','$s','$from','$to')";
        $quer=mysqli_query($conn,$ins);
        }
    if($quer){
        echo "Updated";
    }else{
        echo"NOT".mysqli_error($conn);

}
$sel = 'select Driver_name from driver_master';
$query = mysqli_query($conn, $sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){    
  echo "<tr><td>".$row['Driver_name']
       ."<input type=\"hidden\" name=\"driver_name\"   
value=\"".$row['Driver_name']."\"/></td>";
  $sel1='select d_status from status';
 $query1=mysqli_query($conn,$sel1);
 echo "<td><select name=\"status\">";
 while($row1=mysqli_fetch_assoc($query1)){
       echo "<option value=\"".$row1['d_status']."\">".$row1['d_status']." 
 </option>";
 }
 echo "</select></td>";
 echo "<td>".'<input type="text" name="date_from" class="datepicker">'."</td>";
 echo "<td>".'<input type="text" name="date_to" class="datepicker">'."</td>";
 echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>