Ignore:
Timestamp:
07/21/11 11:24:15 (13 years ago)
Author:
Daniela Dorner
Message:
added plenty of new features to view and edit the configuration database
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/www/db_po/insert_po.php

    r11353 r11505  
    1111    if (empty($_GET["fIndex"]))
    1212    {
    13         echo "Please giv an index.<br>";
     13        echo "Please give an index.<br>";
    1414        return;
    1515    }
     
    1919        return;
    2020    }
    21     if (empty($_GET["fKey"]))
    22     {
    23         echo "Please select a key.<br>";
     21    if (empty($_GET["fKey1"]))
     22    {
     23        echo "Please select a key1.<br>";
     24        return;
     25    }
     26    if (empty($_GET["fKey2"]))
     27    {
     28        echo "Please select a key2.<br>";
    2429        return;
    2530    }
     
    3136    if (empty($_GET["fDescription"]))
    3237    {
    33         echo "Please insert a Description.<br>";
    34         return;
    35     }
    36 
    37 
     38        echo "Please insert a description.<br>";
     39        return;
     40    }
     41    if (empty($_GET["fType"]))
     42    {
     43        echo "Please select a type.<br>";
     44        return;
     45    }
     46    if (empty($_GET["fOriginalIndex"]))
     47        $_GET["fOriginalIndex"]="";
    3848
    3949    include("db.php");
    4050    $db_id = mysqli_connect($host, $user, $pw, $db);
    41     //if ($db_id==FALSE)
    4251    if (mysqli_connect_errno())
    4352    {
     
    4554        die("");
    4655    }
    47     //mysql_select_db($db);
     56
     57    //query old entry
     58    $query1="SELECT fValue, fProgram, fKey1, fDescription, fType, fMin, fMax, fKey2 from ProgramOption WHERE fIndex='".$_GET["fIndex"]."';";
     59    //compare new values to old values
     60    //insert only, if at least one values is new
     61    //  update counter only if value is updated
     62    if (!$result1=mysqli_query($db_id, $query1))
     63    {
     64        echo "Error sending query: ".$query1;
     65        return;
     66    }
     67    $row1 = mysqli_fetch_row($result1);
     68
     69    if ($_GET["fUpdCase"]!=4
     70        && strcmp($row1[0],$_GET["fValue"])==0
     71        && strcmp($row1[1],$_GET["fProgram"])==0
     72        && strcmp($row1[2],$_GET["fKey1"])==0
     73        && strcmp($row1[7],$_GET["fKey2"])==0
     74        && strcmp($row1[3],$_GET["fDescription"])==0
     75        && strcmp($row1[4],$_GET["fType"])==0
     76        //&& strcmp($row1[5],$_GET["fMin"])==0
     77        //&& strcmp($row1[6],$_GET["fMax"])==0
     78       )
     79    {
     80        // be careful, this output is evaluated by UpdateRow() in po.js
     81        echo "no change";
     82        mysqli_free_result($result1);
     83        return;
     84    }
     85    else
     86        mysqli_free_result($result1);
     87
     88    //check if key alread exists
     89    $query2="SELECT Count(*) from ProgramOption WHERE fKey1='".$_GET["fKey1"]."' AND fKey2='.".$_GET["fKey2"]."'";
     90    $query2.=" AND fCounter=(Select Max(fCounter) from History)";
     91    if (!$result2=mysqli_query($db_id, $query2))
     92    {
     93        echo "Error sending query: ".$query2;
     94        return;
     95    }
     96    $row2 = mysqli_fetch_row($result2);
     97    if (($_GET["fUpdCase"]==1 || $_GET["fUpdCase"]==5) && $row2[0]>0)
     98    {
     99        // be careful, this output is evaluated by UpdateRow() in po.js
     100        echo "key exists";
     101        mysqli_free_result($result2);
     102        return;
     103    }
     104    else
     105        mysqli_free_result($result2);
     106
     107    //check if program alread exists
     108    $query3="SELECT Count(*) from ProgramOption WHERE fProgram='".$_GET["fProgram"]."'";
     109    $query3.=" AND fCounter=(Select Max(fCounter) from History)";
     110    if (!$result3=mysqli_query($db_id, $query3))
     111    {
     112        echo "Error sending query: ".$query3;
     113        return;
     114    }
     115    $row3 = mysqli_fetch_row($result3);
     116    if ($_GET["fUpdCase"]==5 && $row3[0]>0)
     117    {
     118        // be careful, this output is evaluated by UpdateRow() in po.js
     119        echo "prog exists";
     120        mysqli_free_result($result3);
     121        return;
     122    }
     123    else
     124        mysqli_free_result($result3);
     125
     126    //note for fType:
     127    //SHOW COLUMNS FROM ProgramOption LIKE 'fType';
    48128
    49129    //new entry in table ProgramOption
    50     $query0="INSERT ProgramOption SET ";
    51     $query0.="fProgram='".$_GET["fProgram"]."'";
    52     $query0.=", fKey='".$_GET["fKey"]."'";
    53     if ($_GET["fUpdCase"]==2 || $_GET["fUpdCase"]==3)
    54         $query0.=", fValue='".$_GET["fValue"]."'";
    55     if ($_GET["fUpdCase"]==1 || $_GET["fUpdCase"]==4)
    56         $query0.=", fValue=NULL";
    57     $query0.=", fDescription='".$_GET["fDescription"]."'";
    58     $query0.=", fValidFrom=Now() ";
    59     if ($_GET["fUpdCase"]==1)
    60         $query0.=", fCounter= (Select Max(fCounter) from History) ;";
    61     else
    62         $query0.=", fCounter= (Select Max(fCounter) from History)+1 ;";
    63 
    64     //update counter for other valid entries in ProgramOption
    65     $query0.=" UPDATE ProgramOption SET ";
    66     $query0.=" fCounter= (Select Max(fCounter) from History)+1 ";
    67     $query0.=" WHERE fCounter= (Select Max(fCounter) from History) ";
    68     if ($_GET["fUpdCase"]==3 || $_GET["fUpdCase"]==4)
    69         $query0.=" AND NOT fIndex=".$_GET["fIndex"];
    70     $query0.=" AND NOT ISNULL(fValue); ";
    71 
    72     //insert new counter to history
    73     if ($_GET["fUpdCase"]!=1)
    74     {
     130    //update only in case 3 if value is not changed
     131    if ($_GET["fUpdCase"]==6)
     132        $query0="DELETE FROM ProgramOption ";
     133    else
     134    {
     135        if (strcmp($row1[0],$_GET["fValue"])==0 && $_GET["fUpdCase"]==3)
     136            $query0="UPDATE ";
     137        else
     138            $query0="INSERT ";
     139        $query0.=" ProgramOption SET ";
     140        // set program, key, description, type
     141        $query0.="fProgram='".$_GET["fProgram"]."'";
     142        if (!empty($_GET["fOriginalIndex"]))
     143            $query0.=", fOriginalIndex='".$_GET["fOriginalIndex"]."'";
     144        $query0.=", fKey1='".$_GET["fKey1"]."'";
     145        $query0.=", fKey2='.".$_GET["fKey2"]."'";
     146        $query0.=", fDescription='".$_GET["fDescription"]."'";
     147        $query0.=", fType='".$_GET["fType"]."'";
     148        // set value
     149        // change value in case is was
     150        if ($_GET["fUpdCase"]==2 //inserted
     151            || ($_GET["fUpdCase"]==3 && strcmp($row1[0],$_GET["fValue"])!=0))// changed
     152            $query0.=", fValue='".$_GET["fValue"]."'";
     153        // set to NULL in case it was
     154        if ($_GET["fUpdCase"]==1 || $_GET["fUpdCase"]==5 //new insert
     155            || $_GET["fUpdCase"]==4) //delete
     156            $query0.=", fValue=NULL";
     157        //increase counter in case value was
     158        if ($_GET["fUpdCase"]==2//created
     159            || ($_GET["fUpdCase"]==3 && strcmp($row1[0],$_GET["fValue"])!=0)//changed
     160            || $_GET["fUpdCase"]==4)//deleted
     161        {
     162            $query0.=", fValidFrom=Now() ";
     163            $query0.=", fCounter= (Select Max(fCounter) from History)+1 ";
     164        }
     165        else//else set current counter
     166            $query0.=", fCounter= (Select Max(fCounter) from History) ";
     167    }
     168    //do update only for current row
     169    if ((strcmp($row1[0],$_GET["fValue"])==0 && $_GET["fUpdCase"]==3) || $_GET["fUpdCase"]==6)
     170        $query0.=" WHERE fIndex=".$_GET["fIndex"];
     171    if ($_GET["fUpdCase"]==6)
     172        $query0.=" AND fValidFrom='0000-00-00 00:00:00'";
     173    $query0.=";";
     174
     175    //update counter in case value was
     176    if ($_GET["fUpdCase"]==2 //created
     177        || ($_GET["fUpdCase"]==3 && strcmp($row1[0],$_GET["fValue"])!=0)//changed
     178       || $_GET["fUpdCase"]==4)//deleted
     179    {
     180        //update counter for other valid entries in ProgramOption
     181        $query0.=" UPDATE ProgramOption SET ";
     182        $query0.=" fCounter= (Select Max(fCounter) from History)+1 ";
     183        $query0.=" WHERE fCounter= (Select Max(fCounter) from History) ";
     184        if ($_GET["fUpdCase"]==3 || $_GET["fUpdCase"]==4)
     185            $query0.=" AND NOT fIndex=".$_GET["fIndex"];
     186        $query0.=" AND ((NOT ISNULL(fValue) AND NOT fValidFrom='0000-00-00 00:00:00') ";//normal entries, but not deleted ones
     187        $query0.=" OR (ISNULL(fValue) AND fValidFrom='0000-00-00 00:00:00')); ";//new entries
     188
     189        //insert new counter to history
    75190        $query0.=" INSERT History SET ";
    76191        $query0.=" fCounter= (Select Max(fCounter) from ProgramOption) ";
     
    78193    }
    79194
    80 
    81     /*
    82     if ($_GET["fUpdCase"]==1 || $_GET["fUpdCase"]==2)
    83         $query0 ="INSERT ProgramOption SET ";//insert key or insert value
    84     else
    85         $query0 ="UPDATE ProgramOption SET ";//update value (includes 'delete')
    86 
    87     if ($_GET["fUpdCase"]==4)//i.e. delete
    88         $query0.=" fValue=NULL";
    89     else
    90     {
    91         $query0.="fProgram='".$_GET["fProgram"]."'";
    92         $query0.=", fKey='".$_GET["fKey"]."'";
    93         $query0.=", fValue='".$_GET["fValue"]."'";
    94         $query0.=", fDescription='".$_GET["fDescription"]."'";
    95         $query0.=", fValidFrom=Now() ";
    96     }
    97 
    98     if ($_GET["fUpdCase"]==1 || $_GET["fUpdCase"]==2) //only in case of insert
    99         $query0.=", fValidFrom=Now() ";
    100 
    101     if ($_GET["fUpdCase"]==3 || $_GET["fUpdCase"]==4)// only in case of update
    102         $query0.="WHERE fIndex=".$_GET["fIndex"]."; ";
    103 
    104     if ($_GET["fUpdCase"]==2 || $_GET["fUpdCase"]==3)// only when value is changed (includes insert)
    105     {
    106         $query0.="UPDATE ProgramOption SET fCounter=(Select Max(fCounter) from History)+1 WHERE NOT ISNULL(fValue); ";
    107         $query0.="INSERT History SET fCounter=(Select Max(fCounter) from ProgramOption), fValidFrom=Now(); ";
    108     }
    109     */
    110 
    111     //echo $query0;
    112195    $result0=mysqli_multi_query($db_id, $query0);
    113196    if ($result0)
Note: See TracChangeset for help on using the changeset viewer.