PHP combined with JS implementation Click Hyperlink to perform delete confirmation operation

Source: Internet
Author: User
Tags dsn getmessage html page prepare

First, link the database and query the database data:

The

code is 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 '; Database used by


$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); Initializing a PDO object is the creation of a database connection object $pdo


$query = "SELECT * from Tb_pdo_mysql"; Define SQL statement


$result = $pdo->prepare ($query); Prepare query Statements


$result->execute (); Executes the query statement and returns the result set


while ($res = $result->fetch (PDO::FETCH_ASSOC)) {//while loops out 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/>");


}


?>


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

The

code is 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:

The

code is 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 '; Database used by


$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); Initializing a PDO object 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 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 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.