|
|
|
รบกวนขอความช่วยเหลือเกี่ยวกับ การ INSERT ลง MSSQL หน่อยครับ |
|
|
|
|
|
|
|
ตอนนี้มีปัญหาเรื่องการเพิ่มข้อมูล โดนใช้โค้ด
Code (PHP)
if($EndDate == ""){
$EndDate = 'NULL';
}
$sql = "INSERT INTO ANNOUNCEMENT (Subject, AnnounceType, EffectiveDate, Detail, Image, FileLink,Contact,
AnnounceBy, CreateBy, CreateDate, Hidden, TypeUpload, InEffectiveDate, Sequence, Highlight, ClusterCode, Area, EndDate)
VALUES('?', '?', '?', '?', '?', '?', '?', '?', '?', '?' , '?', '?', '?', '?', '?', '?', '?', '?')";
$params = array('$SubjectICONV', '$AnnounceType', '".$EffectiveDate."', '$DetailICONV', '$urlImage', '$urlFileLink', '$ContactICONV',
'$AnnounceByICONV', '$username_sess', '".$CreateDate."' , 0, 0, '$EndDate', 1, 0, '$Cluster', '$RO_ID', '".$EndDate."');
$stmt = $connPDO->prepare($sql);
$stmt->execute($params);
if( $stmt->rowCount() ) {
echo "Record add successfully <br>";
}else{
echo "<br> ERROR!!<br>";
print_r($stmt->errorInfo());
}
echo "CreateDate= ".$CreateDate."<br> EndDate= ".$EndDate;
ข้อความ ERROR
ERROR!!
Array ( [0] => 22007 [1] => 241 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Conversion failed when converting date and/or time from character string. )
CreateDate= 2019-03-29 10:32:57
EndDate= NULL
ขอบคุณครับ
Tag : PHP, MySQL, Ms SQL Server 2012, Ms SQL Server 2014, Ms SQL Server 2016
|
ประวัติการแก้ไข 2019-03-29 11:20:21
|
|
|
|
|
Date :
2019-03-29 11:07:20 |
By :
nextgen007 |
View :
602 |
Reply :
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
แปลงเป็น Mar ก็ยังไม่ได้ครับ
Code (PHP)
function convertTimeShort($date_time){///function แปลงเวลา
$date_time_s = split(" ", $date_time);
$date_s = split("-", $date_time_s[0]);
$time_s = split(":", $date_time_s[1]);
$monthCon = array("-- เดือน --" , "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec");
$date_s_m = "";
for($i=1; $i < 13; $i++){
if($date_s[1] == $i){
$date_s_m = $monthCon[$i];
}
}//Ex. 2019-Mar-29 10:32:57
return $date_s[0]."-".$date_s_m."-".$date_s[2]." ".$time_s[0].":".$time_s[1].":".$time_s[2];
}
$CreateDate =convertTimeShort($CreateDate);//แปลงเวลา
if($EndDate == ""){
$EndDate = 'NULL';
}
$sql = "INSERT INTO ANNOUNCEMENT (Subject, AnnounceType, EffectiveDate, Detail, Image, FileLink,Contact,
AnnounceBy, CreateBy, CreateDate, Hidden, TypeUpload, InEffectiveDate, Sequence, Highlight, ClusterCode, Area, EndDate)
VALUES('?', '?', '?', '?', '?', '?', '?', '?', '?', '?' , '?', '?', '?', '?', '?', '?', '?', '?')";
$params = array('$SubjectICONV', '$AnnounceType', '".$EffectiveDate."', '$DetailICONV', '$urlImage', '$urlFileLink', '$ContactICONV',
'$AnnounceByICONV', '$username_sess', '".$CreateDate."' , 0, 0, '$EndDate', 1, 0, '$Cluster', '$RO_ID', '".$EndDate."');
$stmt = $connPDO->prepare($sql);
$stmt->execute($params);
if( $stmt->rowCount() ) {
echo "Record add successfully <br>";
}else{
echo "<br> ERROR!!<br>";
print_r($stmt->errorInfo());
}
echo "<br> CreateDate= ".$CreateDate."<br> EndDate= ".$EndDate;
error code
ERROR!!
Array ( [0] => 22007 [1] => 241 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Conversion failed when converting date and/or time from character string. )
CreateDate= 2019-Mar-29 10:32:57
EndDate= NULL
|
ประวัติการแก้ไข 2019-03-29 11:38:32
|
|
|
|
Date :
2019-03-29 11:37:32 |
By :
nextgen007 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอบความคิดเห็นที่ : 4 เขียนโดย : mr.win เมื่อวันที่ 2019-03-29 11:43:33
รายละเอียดของการตอบ ::
ขอโทษด้วยครับ ใส่สลับกัน
แต่ ก็ยังไม่ได้อยู่ดี
Code (PHP)
function convertTimeShort($date_time){///function แปลงเวลา
$date_time_s = split(" ", $date_time);
$date_s = split("-", $date_time_s[0]);
$time_s = split(":", $date_time_s[1]);
$monthCon = array("-- เดือน --" , "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec");
$date_s_m = "";
for($i=1; $i < 13; $i++){
if($date_s[1] == $i){
$date_s_m = $monthCon[$i];
}
}//Ex. 2019-Mar-29 10:32:57
//return $date_s[0]."-".$date_s_m."-".$date_s[2]." ".$time_s[0].":".$time_s[1].":".$time_s[2];
return $date_s[2]."-".$date_s_m."-".$date_s[0]." ".$time_s[0].":".$time_s[1].":".$time_s[2];
}
$CreateDate =convertTimeShort($CreateDate);//แปลงเวลา
$EffectiveDate =convertTimeShort($EffectiveDate);//แปลงเวลา
echo "CreateDate= ".$CreateDate."<br> EffectiveDate= ".$EffectiveDate;
if($EndDate == ""){
$EndDate = 'NULL';
}
$sql = "INSERT INTO ANNOUNCEMENT (Subject, AnnounceType, EffectiveDate, Detail, Image, FileLink,Contact,
AnnounceBy, CreateBy, CreateDate, Hidden, TypeUpload, InEffectiveDate, Sequence, Highlight, ClusterCode, Area, EndDate)
VALUES('?', '?', '?', '?', '?', '?', '?', '?', '?', '?' , '?', '?', '?', '?', '?', '?', '?', '?')";
$params = array('$SubjectICONV', '$AnnounceType', '".$EffectiveDate."', '$DetailICONV', '$urlImage', '$urlFileLink', '$ContactICONV',
'$AnnounceByICONV', '$username_sess', '".$CreateDate."' , 0, 0, '$EndDate', 1, 0, '$Cluster', '$RO_ID', '".$EndDate."');
$stmt = $connPDO->prepare($sql);
$stmt->execute($params);
if( $stmt->rowCount() ) {
echo "Record add successfully <br>";
}else{
echo "<br> ERROR!!<br>";
print_r($stmt->errorInfo());
}
//echo "<br> CreateDate= ".$CreateDate."<br> EndDate= ".$EndDate;
echo "<br> EndDate= ".$EndDate;
CreateDate= 29-Mar-2019 10:32:57
EffectiveDate= 01-Apr-2019 00:00:00
ERROR!!
Array ( [0] => 22007 [1] => 241 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Conversion failed when converting date and/or time from character string. )
EndDate= NULL
|
|
|
|
|
Date :
2019-03-29 11:53:50 |
By :
nextgen007 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เอา single quote รอบเครื่องหมายคำถามออกให้หมด
value( ? , ?, ? ) แบบนี้ ตัว prepare มันตรวจสอบ และใส่ให้เอง อัตตโนมัต
ปล. quote ที่ $param ด้วย อ่าน document มันบ้างหรือเปล่านี่ อย่ามั่วเอาเอง หัดอ่านซะบ้าง
อย่าทำตัวเป็น genius มันจะกลายเป็น idiot ไป
|
ประวัติการแก้ไข 2019-03-29 12:32:12
|
|
|
|
Date :
2019-03-29 12:27:53 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
$val = array($val1,$val2,$val3,$val4,...);
|
|
|
|
|
Date :
2019-03-29 13:23:36 |
By :
arm8957 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|