HOME > PHP > PHP Forum > Fatal error: Uncaught exception com_exception with message <b>Source:</b> Microsoft OLE DB Provider for ODBC Drivers<br/><b>Description:</b> [Microsoft][ODBC Microsoft Access Driver] Could not delete from specified tables.
Fatal error: Uncaught exception com_exception with message <b>Source:</b> Microsoft OLE DB Provider for ODBC Drivers<br/><b>Description:</b> [Microsoft][ODBC Microsoft Access Driver] Could not delete from specified tables.
<?php
//index.php
include ('mdb.php');
$mdb = new mdb('mydb.mdb'); // your own mdb filename required
$ac=filter_input(INPUT_GET, 'action');
switch($ac){
case 'delete':
$sql='delete from test where id='.filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
$mdb->execute($sql);
header('location: index.php'); break;
case 'insert':
$un=filter_input(INPUT_POST, 'user');
$pw=filter_input(INPUT_POST, 'pswd');
$cr=filter_input(INPUT_POST, 'credit');
$sql = "insert into test ".
"select top 1 (id + 1) as x, '$un' as un, '$pw' as pw, '$cr' as cr , '0' as bl from test order by id desc ";
$mdb->execute($sql); // your own table in the mdb file
header('location: index.php'); break;
default:
//
}
$mdb->execute('select * from test'); // your own table in the mdb file
while(!$mdb->eof()) {
echo ($id=$mdb->fieldvalue('id')) . ' = ' . $mdb->fieldvalue('username');
echo ' <a href="?action=delete&id='.$id.'">x</a><br>';
$mdb->movenext();
}
echo '<br><hr><br>';
$mdb->movefirst();
$id = $mdb->RS->Fields(0);
$un = $mdb->RS->Fields(1);
$pw = $mdb->RS->Fields(2);
while(!$mdb->eof()){
echo $id->value . ' = ' . $un->value . ' : ' . $pw->value;
echo ' <a href="?action=delete&id='.$id->value.'">x</a><br>';
$mdb->movenext();
}
$mdb->close();
?>
<form method="post" action="?action=insert">
<label>username</label><input type="text" name="user"><br>
<label>password</label><input type="password" name="pswd"><br>
<label>credit</label><input type="text" name="credit"><br>
<button>save</button>
</form>
Code (PHP)
<?php
//mdb.php
class mdb
{
var $RS = 0;
var $con= 0;
var $RecordsAffected;
var $strProvider = 'DRIVER={Microsoft Access Driver (*.mdb)}';
var $strDataSource = '';
var $strConn = '';
var $strRealPath = '';
var $recordcount = 0;
var $ok = false;
/**
* Constructor needs path to .mdb file
*
* @param string $dsn = path to *.mdb file
* @return boolean success
*/
function mdb( $dsn='Please enter DataSource!' ){
$this->strRealPath = realpath( $dsn );
if( strlen( $this->strRealPath ) > 0 ){
$this->strDataSource = 'DBQ='.$this->strRealPath;
$result = true;
}else {
echo "<br>mdb::mdb() File not found $dsn<br>";
$result = false;
}
$this->RecordsAffected = new VARIANT();
$this->open();
} // eof constructor mdb()
function open(){
if( strlen( $this->strRealPath ) > 0 ){
$this->strConn = $this->strProvider.';'.$this->strDataSource.';';
$this->con= new COM( 'ADODB.Connection' );
if( $this->con){
$this->con->open( $this->strConn );
$result = true;
}else{
echo '<br>mdb::open() ERROR with ADODB.Connection<br>'.$this->strConn;
$result = false;
}
}
$this->ok = $result;
return $result;
} // eof open()
function execute( $strSQL, $getrecordcount = false ){
$this->RS = $this->con->execute( $strSQL, $this->RecordsAffected );
if( $getrecordcount == true ){
$this->RS->MoveFirst();
$this->recordcount = 0;
# brute force loop
while( $this->RS->EOF == false ){
$this->recordcount++;
$this->RS->MoveNext();
}
$this->RS->MoveFirst();
}
} // eof execute()
function eof(){
return $this->RS->EOF;
} // eof eof()
function movenext(){
$this->RS->MoveNext();
} // eof movenext()
function movefirst(){
$this->RS->MoveFirst();
} // eof movefirst()
function close(){
$this->RS->Close();
$this->RS=null;
$this->con->Close();
$this->con=null;
} // eof close()
function fieldvalue( $fieldname ){
return $this->RS->Fields[$fieldname]->value;
} // eof fieldvalue()
function fieldname( $fieldnumber ){
return $this->RS->Fields[$fieldnumber]->name;
} // eof fieldname()
function fieldcount( ){
return $this->RS->Fields->Count;
} // eof fieldcount()
} // eoc mdb