Now we will produce two different kinds of updates:
One done with the Update function, the second one with the
MkUpdate function.
Then both updates will be output for control.
We will simulate form requests by creating the array $FAKE_REQUEST and $MORE_FAKE.
Use your fantasy to think of it as real form data.
Program File
# 1: <?php
# 2: require_once("../fastsql.inc");
# 3: define("G_HOST", "localhost");
# 4: define("G_DBUSER", "MyUsername");
# 5: define("G_DBPWD", "MyPassword");
# 6: define("G_NORMDB", "MyDB");
# 7:
# 8: $FAKE_REQUEST = array("ID"=>4,
# 9: "Name"=>"George",
#10: "Phone"=>"096654 33"
#11: );
#12: $MORE_FAKE = array("ID"=>4,
#13: "Name"=>"Harry",
#14: "Phone"=>"8743 5323"
#15: );
#16:
#17: $conn = new FastSQL;
#18: $update_statement = "update ADDRESSES set Name='" . $FAKE_REQUEST['Name']."'
#19: where ID=" . intval($FAKE_REQUEST['ID']);
#20:
#21: $do_update = $conn->update($update_statement, G_NORMDB);
#22:
#23: $select1 = "select * from ADDRESSES where ID=" . intval($FAKE_REQUEST['ID']);
#24: $res1 = $conn->single($select1, G_NORMDB);
#25: print "<pre>";
#26: print_r($res1);
#27: print "</pre>";
#28:
#29: $conn->MkUpdate("ADDRESSES", G_NORMDB, $MORE_FAKE, "ID");
#30:
#31: $select2 = "select * from ADDRESSES where ID=" . intval($MORE_FAKE['ID']);
#32: $res2 = $conn->single($select2, G_NORMDB);
#33: print "<pre>";
#34: print_r($res2);
#35: print "</pre>";
#36: ?>
The result will look like this
# 0: <?
# 1: Array
# 2: (
# 3: [ID] => 4
# 4: [Name] => George
# 5: [Phone] => 666 777 443
# 6: [MODIFIED] => 20050814191831
# 7: [CREATED] => 20050814185937
# 8: )
#10: Array
#11: (
#12: [ID] => 4
#13: [Name] => Harry
#14: [Phone] => 8743 5323
#15: [MODIFIED] => 20050814191831
#16: [CREATED] => 20050814185937
#17: )
So, what happened?
While in the first step we built a "classic" SQL statement in teh second step we used the MakeUpdate function:
#0: <?
#1: $conn->MkUpdate("ADDRESSES", G_NORMDB, $MORE_FAKE, "ID");
This caused FastSQL to update "ADDRESSES" in the database G_NORMDB (i.e. "MyDB").
It updated every field whose key existed in $MORE_FAKE, which was "Name" and "Phone" - but with an exception:
"ID" is in the array $MORE_FAKE, but it is also the comparison field, so it will not be updated.
|