Php uses JavaScript to perform deletion confirmation by clicking a hyperlink

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.