This article is a super simple php with js implementation click hyperlink to execute js code, and confirm whether to delete the database data, attach all the source code, for a friend in need to refer to it as the question, click a hyperlink to execute the js code and confirm whether to delete the database data. php is used.
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
?>
<? Php echo $ res ['id'];?>
<? Php echo $ res ['pdo _ type'];?>
<? Php echo $ res ['database _ name'];?>
<? Php echo $ res ['dates'];?>
) "> Delete
<? Php
}
} Catch (PDOException $ e ){
Die ("Error! : ". $ E-> getMessage ()."
");
}
?>
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:
';
Echo 'SQL Query:'. $ query;
Echo'
';
echo "Error: " . $e->getMessage(). "
";
echo "Code: " . $e->getCode(). "
";
echo "File: " . $e->getFile(). "
";
echo "Line: " . $e->getLine(). "
";
echo "Trace: " . $e->getTraceAsString(). "
";
echo '
';
}
}
?>
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.