You've already seen how PHP can be used to interface with Java components and JavaBeans. But here's something you didn't know - PHP can (shock shock! horror horror!) even be used to interface with Microsoft COM objects on the Windows platform. Will this be a happy marriage? Read on to find out.
If the user chooses to add a new record, the value of $nextopid is set to "add_process". And when the PHP script is called to process the form data, this is the code block that gets executed.
<?php
if($nextopid == "add_process")
{
// open connection
$DB_Conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$DB_Conn->Open("phpcom");
// run query
$Query = "INSERT INTO coins(name, country, weight, year, remarks)
VALUES('".trim($name)."','".trim($country)."',".trim($weight).",".trim($
year
).",'".trim($remarks)."')";
$RS_Record = $DB_Conn->Execute($Query);
// redirect browser to list
header("Location:".$PHP_SELF."?nextopid=list&previousopid=".$nextopid);
// end of "add" processor
}
?>
Similarly, when the script is called to edit a record, this
code block will get executed.
<?php
if($nextopid == "edit_process")
{
// open connection
$DB_Conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$DB_Conn->Open("phpcom");
// execute query
$Query = "UPDATE coins SET name = '".trim($name)."',country =
'".trim($country)."',weight = ".trim($weight).",year =
".trim($year).",remarks = '".trim($remarks)."' WHERE id = $id";
$RS_Record = $DB_Conn->Execute($Query);
// redirect browser back to list
header("Location:".$PHP_SELF."?nextopid=list&previousopid=".$nextopid);
// end of "edit" processor
}
?>
Fairly simple and self-explanatory, this - depending on the
type of operation, an appropriate SQL query is generated and executed.
Finally, record deletion. This is activated by calling the script with the value of $nextopid set to "delete", and an additional record ID to identify which record is to be deleted. Take a look:
<?php
if($nextopid == "delete")
{
// delete selected records
$DB_Conn = new COM("ADODB.Connection") or die("Cannot start ADO");
$DB_Conn->Open("phpcom"); $Query = "DELETE FROM coins WHERE id = $id";
$RS_Record = $DB_Conn->Execute($Query);
header("Location:".$PHP_SELF."?nextopid=list&previousopid=".$nextopid);
// end of "delete" condition
}
?>
Short and sweet. And when you try the entire thing out,
you'll see that you can add, edit and delete records to the Access database, via the COM object. Ain't it cool?