Php uses JavaScript to perform deletion confirmation by clicking a hyperlink
First, connect to the database to query the database data:
The Code is as follows:
<? Php
$ Dbms = 'mysql'; // database type. Developers do not need to remember so many functions to use different databases.
$ Host = 'localhost'; // Database host Name
$ DbName = 'db _ database19 '; // database used
$ User = 'root'; // database connection username
$ Pass = 'root'; // Password
$ Dsn = "$ dbms: host = $ host; dbname = $ dbName ";
Try {
$ Pdo = new PDO ($ dsn, $ user, $ pass); // initialize a PDO object, that is, create a database connection object $ pdo
$ Query = "select * from tb_pdo_mysql"; // define an SQL statement
$ Result = $ pdo-> prepare ($ query); // prepare the query statement.
$ Result-> execute (); // execute the query statement and return the result set.
While ($ res = $ result-> fetch (PDO: FETCH_ASSOC) {// while LOOP outputs the query result set, and sets the result set as the associated index
?>
<Tr>
<Td align = "center" valign = "middle"> <? Php echo $ res ['id'];?> </Td>
<Td align = "center" valign = "middle"> <? Php echo $ res ['pdo _ type'];?> </Td>
<Td align = "center" valign = "middle"> <? Php echo $ res ['database _ name'];?> </Td>
<Td align = "center" valign = "middle"> <? Php echo $ res ['dates'];?> </Td>
<Td align = "center" valign = "middle"> <a href = "javascript: del (<? Php echo $ res ['id']?>) "> Delete </a> </td>
</Tr>
<? Php
}
} Catch (PDOException $ e ){
Die ("Error! : ". $ E-> getMessage ()." <br/> ");
}
?>
Call a javascript method to delete a hyperlink and pass the record id. The js method is as follows:
The Code is as follows:
<Script>
Function del (_ id ){
If (confirm ("confirm deletion "))
{
Window. location. href = "index. php? Conn_id = "+ _ id; // refresh this page
}
}
</Script>
Code for deleting database records:
The Code is as follows:
<? Php
If (@ $ _ GET ['conn _ id']! = ""){
$ Dbms = 'mysql'; // database type. Developers do not need to remember so many functions to use different databases.
$ Host = 'localhost'; // Database host Name
$ DbName = 'db _ database19 '; // database used
$ User = 'root'; // database connection username
$ Pass = 'root'; // Password
$ Dsn = "$ dbms: host = $ host; dbname = $ dbName ";
Try {
$ Pdo = new PDO ($ dsn, $ user, $ pass); // initialize a PDO object, that is, create a database connection object $ pdo
$ Pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION );
$ Query = "delete from tb_pdo_mysql where Id =: id ";
$ Result = $ pdo-> prepare ($ query); // pre-prepared statement
$ Result-> bindParam (': id', $ _ GET ['conn _ id']); // bind the updated data
$ Result-> execute ();
} Catch (PDOException $ e ){
Echo 'pdo Exception Caught .';
Echo 'error with the database: <br/> ';
Echo 'SQL Query:'. $ query;
Echo '<pre> ';
Echo "Error:". $ e-> getMessage (). "<br/> ";
Echo "Code:". $ e-> getCode (). "<br/> ";
Echo "File:". $ e-> getFile (). "<br/> ";
Echo "Line:". $ e-> getLine (). "<br/> ";
Echo "Trace:". $ e-> getTraceAsString (). "<br/> ";
Echo '</pre> ';
}
}
?>
This code is placed at the beginning of the body part of the html page. The worst part should also be placed before the query record code.