为什么没有将日期值添加到DB?

时间:2016-06-03 09:19:06

标签: javascript php

以下是完整的表格:

                                                       $(document).ready(function(){             $(" #datepicker")。datepicker();         });         

    <div class="labelContainer">
        <label for="Date">Date</label>
    </div>
              <div class="labelContainer">
         <label for="Category">Category</label>
         </div>
    <div class="inputContainer">
        <select name="category" id="category" value="category" class="form-control ddplaceholder" style="width:220px;font-size:18px;font-family:Roboto;">
             <option value="" disabled selected>Select Category</option>
        <?php
        $sth = $conn->prepare('Select name From category');
        $sth->execute();
        $data = $sth->fetchAll();   
        foreach ($data as $row ){
            if($row['name']!="")
         echo ' <option id=\"CategoryName\" nameCategoryNameVendorName\" value="' .$row['name']. '">'.$row['name'].'</option>';
        }
        ?> 
        </select></div><br>


    <br>


            <div class="labelContainer">
         <label for="Item">Item</label>
         </div>
            <div class="inputContainer">
        <select name="item_name" id="item_name" value="item_name" class="form-control ddplaceholder" style="width:220px;font-size:18px;font-family:Roboto;">
         <option value="" disabled selected>Select Item</option>
        <?php
        $sth = $conn->prepare('Select item_name From item');
        $sth->execute();
        $data = $sth->fetchAll();   
        foreach ($data as $row ){
        if($row['item_name']!="")
        echo " <option id=\"ItemName\" name=\"ItemName\" value=".$row['item_name'].">".$row['item_name']."</option>";
        }
        ?> 
        </select></div><br>



         <div class="labelContainer">
         <label for="Vendor">Vendor</label>
         </div>
        <div class="inputContainer">
        <select name="vendor_name" id="vendor_name" value="vendor_name" class="form-control ddplaceholder" style="width:220px;font-size:18px;font-family:Roboto;">
        <option value="" disabled selected>Select Vendor</option>
        <?php
        $sth = $conn->prepare('Select name From vendor');
        $sth->execute();
        $data = $sth->fetchAll();   
        foreach ($data as $row ){
        if($row['name']!="")
        echo " <option id=\"VendorName\" name=\"VendorName\" value=".$row['name'].">".$row['name']."</option>";
        }
        ?> 
        </select></div><br>


    <div class="labelContainer">
   <label for="Unit">Unit</label>
     </div>
    <div class="inputContainer">
    <input type="text" placeholder="Unit" id="Unit" name="Unit" ><br>
    </div>
    <div class="labelContainer">
   <label for="Price_per_Unit">Price_per_Unit</label>
     </div>
    <div class="inputContainer">
    <input type="text" placeholder="Price per Unit" name="Price_per_Unit" id="Price_per_Unit"><br>
    </div>
    <div class="labelContainer">
   <label for="Quantity">Quantity</label>
     </div>
    <div class="inputContainer">
    <input type="text" placeholder="Quantity" name="Quantity" id="Quantity"><br>
    </div>

    <div class="labelContainer">
   <label for="VAT">VAT</label>
     </div>
    <div class="inputContainer">
    <input type="text" placeholder="VAT" name="VAT" id="VAT"><br>
    </div>
    <div class="labelContainer">
   <label for="Freight_charges">Freight_charges</label>
     </div>
    <div class="inputContainer">
   <input type="text" placeholder="Freight charges" id="Freight_charges" name="Freight_charges"><br>
    </div>

    <div class="labelContainer">
   <label for="Other_Charges">Other_Charges</label>
     </div>
    <div class="inputContainer">
   <input type="text" placeholder="Other Charges" id="Other_Charges" name="Other_Charges"><br>
    </div>
    <div class="labelContainer">
   <label for="Total">Total</label>
     </div>
    <div class="inputContainer">
    <input type="text" placeholder="Total" id="Total" name="Total"><br>
    </div>

        <input type="submit" name="submit" value="Update" style="width:150px;padding:10px;">
        <?php 
        $date="";
        $category_value="";
        $item_value="";
        $vendor_value="";
        $unit_value="";
        $price_rate_value=0;
        $quantity_value=0;
        $vat_value=0;
        $freight_value=0;
        $other_charges_value=0;
        $total_value=0;  
        if(!empty($_POST['category']))
        {
        $category_value=$_POST['category'];
        }
        if(!empty($_POST['item_name']))
        {
        $item_value=$_POST['item_name'];
        }
        if(!empty($_POST['vendor_name']))
        {
        $vendor_value=$_POST['vendor_name'];
        }
        if(!empty($_POST['Unit']))
        {
        $unit_value=$_POST['Unit'];
        }
        if(!empty($_POST['Price_per_Unit']))
        {
        $price_rate_value=$_POST['Price_per_Unit'];
        }
        if(!empty($_POST['Quantity']))
        {
        $quantity_value=$_POST['Quantity'];
        }
        if(!empty($_POST['Freight_charges']))
        {
        $freight_value=$_POST['Freight_charges'];
        }
        if(!empty($_POST['Other_charges']))
        {
        $other_charges_value=$_POST['Other_charges'];
        }
       if(!empty($_POST['VAT']))
        {
        $vat_value=$_POST['VAT'];
        } 
        $total_value= ($price_rate_value * $quantity_value)+$freight_value+$other_charges_value+$vat_value;

        $sql='INSERT INTO bill(date, category, item, vendor, unit, price_per_unit, quantity, vat, freight_charges, other_charges, total) '
    . 'values (:date,:category, :item,:vendor, :unit, :price_per_unit, :quantity, :vat, :freight_charges, :other_charges, :total)';
             $sth=$conn->prepare($sql); 
            $sth->execute(array(':date'=>$date,':category'=>$category_value,':item'=>$item_value, ':vendor'=>$vendor_value,
                ':unit'=>$unit_value,':price_per_unit'=>$price_rate_value,':quantity'=>$quantity_value,':vat'=>$vat_value,
                ':freight_charges'=>$freight_value,':other_charges'=>$other_charges_value,':total'=>$total_value));              

        ?>
 </form>

如何添加日期?已经尝试过目前所说的答案。是因为没有与日期相关的提交吗?可能是因为那个吗?

4 个答案:

答案 0 :(得分:1)

MySQL在其dateformat语法中接受该值。

MySQL中的日期格式为 YYYY-MM-DD

试试这个

if(!empty($_POST['datepicker']))
{
  $date = date("Y-m-d",strtotime($_POST['datepicker'])); //mysql date format
}

答案 1 :(得分:1)

重写您的脚本代码并尝试:

<script>
$(document).ready(function() {
    $( "#datepicker" ).datepicker();
});
$("#datepicker").datepicker('setDate', new Date());
</script>

答案 2 :(得分:0)

在将日期保存到数据库之前,将其日期格式更改为mysql数据库字段的日期格式:

if(!empty($_POST['datepicker']))
{
  $date = date("Y-m-d",strtotime($_POST['datepicker'])); // change date format like this
}
$sth->execute(array(':date'=>$date, ':category'=>$category, ....));

现在将此日期保存到数据库。因为您的日期选择器日期格式可能不同,如d-m-y,d / m / y等...所以您需要将此日期格式更改为数据库日期格式,然后才能将其成功保存到数据库。

答案 3 :(得分:-1)

好的,它现在正在运作。问题是我在表单之外编写了日历代码。把它放在里面,现在它可以工作。