简体   繁体   中英

SQL + PHP Update query

I've been trying to update my data according to the user session (UserLogin) but it kept saying: Data type mismatch in criteria expression. The print_r is just for testing purposes.

Thanks in advance,

Z

function Employee1_BeforeShow(& $sender)
{
    $Employee1_BeforeShow = true;
    $Component = & $sender;
    $Container = & CCGetParentContainer($sender);
    global $Employee1; //Compatibility


$Page = CCGetParentPage($sender);

$db = $Page->Connections["PettyCashMDB"];

$sql1 = "UPDATE Employee SET Employee.LastActive = Date() WHERE Employee.[EmpID] = ". $_SESSION['UserLogin'];
$db->query($sql1);

print_r($_SESSION['UserLogin']);



$db->close();
Employee1_BeforeShow @67-67106FAD
return $Employee1_BeforeShow;
}

EDIT: I've tried @NanaPartykar 's method and by accident I've noticed that it does get the value from $_SESSION['UserLogin'] , just that somehow the datatype is different.

EDIT: It displays the error Data type mismatch but both of them are string and returns string.

代替Employee.[EmpID] ,使用Employee.EmpID

您需要一些引号:

$sql1 = "UPDATE Employee SET Employee.LastActive = Date() WHERE Employee.[EmpID] = \'". $_SESSION['UserLogin'] . "\'";

Z - There are a bunch of built-in Codecharge functions to assist with getting values from querystring, sessions and controls.

eg: CCGetSession("UserLogin", "default");

http://docs.codecharge.com/studio50/html/index.html?http://docs.codecharge.com/studio50/html/Components/Functions/PHP/Overview.html

and executing SQL with some validating (from 'Execute Custom SQL' help topic):

$db = new clsDBConnection1();
$SQL = "INSERT INTO report (report_task_id,report_creator) ". 
     "VALUES (". $db->ToSQL(CCGetFromGet("task_id",0),ccsInteger) .",". $db->ToSQL(CCGetUserID(),ccsInteger) .")";

$db->query($SQL);
$db->close(); 

The $db->ToSQL (and CCToSQL) functions convert and add quotes for relevant data types (ccsText, ccsDate).

There are many examples in the Manual under 'Examples' and 'Programming Reference' for PHP (and ASP, .NET, etc)

http://support.codecharge.com/tutorials.asp

I strongly suggest looking at some of the examples, as Codecharge will handle a lot of the 'plumbing' and adding a lot of custom code will causing problems with the generation of code. In your example, you should add a 'Custom Code' action to the Record's 'Before Show' Event and add your code there. If you add code just anywhere, the entire section of code (eg: Before Show) will change colour and no longer be updated if you change something.

For example, if you manually edited the 'Update' function to change a default value, then no changes through the IDE/Properties will change the 'Update' function (such as adding a new field to the Record).

Finally got it to work, this is the code $sql1 = "UPDATE Employee SET LastActive = Date() WHERE EmpID = '$_SESSION[UserLogin]' "; Thanks to everyone that helped out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM