duanqian6295 2015-02-28 06:26
浏览 91
已采纳

Php无法通过HTML表单更新MySQL表中的日期

I cannot solve this seemingly simply problem. I have an HTML form posting to a php file which works perfectly well. However, when I post a date value from HTML to php the MySQL database updates all the required fields except the date which still shows as 0000-00-00. When I change the php variable to CURDATE() it works OK however, I do not require the current date but the date I post from my HTML file. This is my HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Credit Note Generator v.2</title>
<meta name="generator" content="">
</head>
<body>
<div id="space"><br></div>
<div id="container">
<div id="wb_Form1" style="position:absolute;left:210px;top:110px;width:530px;height:381px;z-index:4;">
<form name="cnote" method="post" action="cnote.php" id="cnote">
<input type="number" id="dupid" style="position:absolute;left:240px;top:31px;width:207px;height:20px;line-height:20px;z-index:0;" name="dupid" value="" required="required">
<input type="date" id="cndate" style="position:absolute;left:240px;top:81px;width:207px;height:20px;line-height:20px;z-index:1;" name="cndate" value="" required="required" >
<textarea name="cnreason" id="cnreason" style="position:absolute;left:240px;top:141px;width:202px;height:127px;z-index:2;" rows="6" cols="24" required="required"></textarea>
<input type="submit" id="Button1" name="" value="Generate" style="position:absolute;left:220px;top:310px;width:132px;height:47px;z-index:3;">

The input date I am passing from this HTML to php is:

<input type="date" id="cndate" style="position:absolute;left:240px;top:81px;width:207px;height:20px;line-height:20px;z-index:1;" name="cndate" value="" required="required" >

My php file (I am only including the relevant part) reads as follows:

<?php
var_dump($_POST);


$con = mysql_connect("192.168.10.223","user","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("BMS", $con);

$pass = $_POST["dupid"]; 
$cnotes = $_POST["cnreason"];
$cndatepost = $_POST["cndate"];
$cndate2 = strtotime($cndatepost);
$cndate = date('Y-m-d',$cndate2); 





//get highest
$sqlgethighest = mysql_query("SELECT MAX(CN_No) FROM Main_Data_Table");
$row = mysql_fetch_array($sqlgethighest);
$cnoteno=($row['MAX(CN_No)'])+1;


$sql="INSERT INTO Main_Data_Table (ID, Name, Surname, TitleID, NationalityID, 
Age, AgentID, `Course Start`, `Course End`, CourseTypeID, `Rate per unit`, 
`Arrival Date`, `Departure Date`, `Arrival Time`, `Departure Time`, `Arrival Flight`, 
`Departure Flight`, AccommodationID, HostFamilyID, `Rate per night`, TransferId, 
`Material Fee`, `Total Units Booked`, `Reg Fee`, BoardBasisID, `Ref Fee Acc`, 
Notes, Notes2, DateofCanx, ReasonforCanx, HFXId, old_Invoice_number, LevelID, 
Hide, Alloc_CodeAMID, Alloc_CodePMID, Lunch, Binder, `Audio CD`, `CD-ROM/Web`, 
Tours, PromoMat, TrfFee, Settle_Bill, Commission, Pay_Local, Settle_Bill_rcpt, 
Pay_on_AC, Pay_on_AC_amt, Pay_on_AC_rcpt, Deposit, SageExportNo, Invoice_number,
TrfRateCode, `Extra Commission`, F56, F57, HOT_CONF, Room_type_ID, Invoice_Date,
CN_Date, CN_No, Passport_No, DOB, CN_Presence, Corresp_CN) 
SELECT NULL, Name, Surname, TitleID, NationalityID, Age, AgentID, `Course Start`, 
`Course End`, CourseTypeID, `Rate per unit`, `Arrival Date`, `Departure Date`, 
`Arrival Time`, `Departure Time`, `Arrival Flight`, `Departure Flight`, 
AccommodationID, HostFamilyID, `Rate per night`, TransferId, `Material Fee`, 
`Total Units Booked`, `Reg Fee`, BoardBasisID, `Ref Fee Acc`, CONCAT('Ref Invoice number: ', Invoice_number, ' ', '$cnotes'), Notes2, 
DateofCanx, ReasonforCanx, HFXId, old_Invoice_number, LevelID, Hide, 
Alloc_CodeAMID, Alloc_CodePMID, Lunch, Binder, `Audio CD`, `CD-ROM/Web`, 
Tours, PromoMat, TrfFee, 0, Commission, 0, 0, 0, 0, 0, 0, 0, 0, TrfRateCode, 
`Extra Commission`, F56, F57, HOT_CONF, Room_type_ID, NULL, $cndate, $cnoteno, NULL, NULL, 0, NULL 
from Main_Data_Table WHERE ID='$pass'"; 

My var_dump correctly displays: string(10) "2015-02-26" for the string $cndate which is the value I am trying to pass but for some reason it is still updating as 0000-00-00.

Thanks

  • 写回答

2条回答 默认 最新

  • dongxu8533486 2015-02-28 07:06
    关注

    While inserting put $cndate in single quote '$cndate'.

    Also do check data type of cndate field

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起