PHP combined with JS implementation Click Hyperlink Perform delete confirmation operation _php Instance

Source: Internet
Author: User
Tags dsn getmessage prepare

As the title, this is to achieve click Hyperlink Implementation JS Code, and confirm whether to delete database data, using PHP.

First, link the database and query the database data:

Copy Code code as follows:

<?php
$dbms = ' MySQL '; Database type, for developers, use a different database, just change this, do not have to remember so many functions
$host = ' localhost '; Database host Name
$dbName = ' db_database19 '; The database used
$user = ' root '; Database connection user Name
$pass = ' root '; The corresponding password
$DSN = "$dbms: host= $host;d bname= $dbName";
try {
$pdo = new PDO ($DSN, $user, $pass); Initializes a PDO object, which is the creation of a database connection object $pdo
$query = "SELECT * from Tb_pdo_mysql"; Defining SQL statements
$result = $pdo->prepare ($query); Preparing query statements
$result->execute (); Executes the query statement and returns the result set
while ($res = $result->fetch (PDO::FETCH_ASSOC)) {//while loops output the query result set and sets the result set as an associated index
?>
<tr>
&LT;TD height= "align=" "Center" valign= "Middle" ><?php echo $res [' id '];? ></td>
&LT;TD align= "center" valign= "Middle" ><?php echo $res [' Pdo_type '];? ></td>
&LT;TD align= "center" valign= "Middle" ><?php echo $res [' database_name '];? ></td>
&LT;TD align= "center" valign= "Middle" ><?php echo $res [' dates '];? ></td>
&LT;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/>");
}
?>

Invoke a JavaScript method at the hyperlink deletion, passing the record Id,js method as:

Copy Code code as follows:

<script>
Function del (_id) {
if (Confirm ("Confirm deletion"))
{
window.location.href= "index.php?conn_id=" +_ID; This page refreshes
}
}
</script>

To delete a database record code:

Copy Code code as follows:

<?php
if (@$_get[' conn_id ']!= "") {
$dbms = ' MySQL '; Database type, for developers, use a different database, just change this, do not have to remember so many functions
$host = ' localhost '; Database host Name
$dbName = ' db_database19 '; The database used
$user = ' root '; Database connection user Name
$pass = ' root '; The corresponding password
$DSN = "$dbms: host= $host;d bname= $dbName";
try {
$pdo = new PDO ($DSN, $user, $pass); Initializes a PDO object, which is the creation of 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 statements
$result->bindparam (': Id ', $_get[' conn_id ')); Binding 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 very beginning of the body section of the HTML page and, at worst, before the query logging 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.