หน้าเว็บ

วันเสาร์ที่ 23 กรกฎาคม พ.ศ. 2559

PHP : SQL set null value to database.

PHP : SQL set null value to database.
Example :
   $query = "INSERT INTO uradmonitor (db_value1, db_value2) VALUES (".
        (($val1=='')?"NULL":("'".$val1."'")) . ", ".
        (($val2=='')?"NULL":("'".$val2."'")) .
        ")";
    $result = mysql_query($query);
 
 
$SQL     = "INSERT INTO sci_rp.RPHeader(RPID, RequestDate, CreateDate, Creator, PREmployee, RPSupervisor, RPManager, PRSupervisor, PRManager, Director, Status, RequestLevel, EndDate) ";
$SQL    .= " VALUES('".$NewID."','".$RequestDate."','".$CreateDate."','".$CreatorEmpId."',".(($PREmployee=='')?"NULL":("'".$PREmployee."'")).",".(($RPSupervisor=='')?"NULL":("'".$RPSupervisor."'")).",".(($RPManager=='')?"NULL":("'".$RPManager."'")).",".(($PRSupervisor=='')?"NULL":("'".$PRSupervisor."'")).",".(($PRManager=='')?"NULL":("'".$PRManager."'")).",".(($Director=='')?"NULL":("'".$Director."'")).",'".$Status."','".$HeaderData["RequestLevel"]."',".(($EndDate=='')?"NULL":("'".$EndDate."'")).")";
$myDB->Update($SQL);
 
Or use this method.
- Create tmp table in database. By copy table to new table by structure only.
- User SQL INSERT(SELECT * FROM ...)
Code Example :
 
$SQL     = "INSERT INTO sci_rp.RPHeader_tmp (SELECT * FROM sci_rp.RPHeader WHERE RPID='".$RPID."')";
$myDB->Update($SQL);
 
$SQL     = "UPDATE sci_rp.RPHeader_tmp SET RPID='".$NewID."' WHERE RPID='".$RPID."'";
$myDB->Update($SQL);
 
$SQL     = "INSERT INTO sci_rp.RPHeader(SELECT * FROM sci_rp.RPHeader_tmp WHERE RPID='".$NewID."')";
$myDB->Update($SQL);
$myDB->Update("DELETE FROM sci_rp.RPHeader_tmp WHERE RPID='".$NewID."'");
 
 

ไม่มีความคิดเห็น:

แสดงความคิดเห็น