Home > Backend Development > PHP Tutorial > PHP combined with js to implement click on hyperlink to perform delete confirmation operation_PHP tutorial

PHP combined with js to implement click on hyperlink to perform delete confirmation operation_PHP tutorial

WBOY
Release: 2016-07-13 10:15:28
Original
1085 people have browsed it

php combined with js to implement click on hyperlink to perform deletion confirmation operation

First connect to the database and query the database data:

The code is as follows:


$dbms='mysql'; //Database type. For developers, if they use different databases, they only need to change this and don’t need to remember so many functions
$host='localhost'; //Database host name
$dbName='db_database19'; //Database used
$user='root'; //Database connection username
$pass='root'; //Corresponding password
$dsn="$dbms:host=$host;dbname=$dbName";
try {
$pdo = new PDO($dsn, $user, $pass); //Initializing a PDO object means creating a database connection object $pdo
$query="select * from tb_pdo_mysql"; //Define SQL statement
$result=$pdo->prepare($query); //Prepare 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 to the associated index
?>





delete

}
} catch (PDOException $e) {
die ("Error!: " . $e->getMessage() . "
");
}
?>


Call a javascript method at the hyperlink deletion point and pass the record id. The js method is:

The code is as follows:


<script><br> function del(_id) {<br> if (confirm("Confirm deletion"))<br> {<br> window.location.href="index.php?conn_id="+_id; //Refresh this page<br> }<br> }<br> </script>

Delete database record code:

The code is as follows:


if(@$_GET['conn_id']!=""){
$dbms='mysql'; //Database type. For developers, if they use different databases, they only need to change this and don’t need to remember so many functions
$host='localhost'; //Database host name
$dbName='db_database19'; //Database used
$user='root'; //Database connection username
$pass='root'; //Corresponding password
$dsn="$dbms:host=$host;dbname=$dbName";
try {
$pdo = new PDO($dsn, $user, $pass); //Initializing a PDO object means creating 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); //Prepared statement
$result->bindParam(':id',$_GET['conn_id']); //Bind updated data
$result->execute();
} catch (PDOException $e) {
echo 'PDO Exception Caught.';
echo 'Error with the database:
';
echo 'SQL Query: '.$query;
echo '
';<br>
echo "Error: " . $e->getMessage(). "<br/>";<br>
echo "Code: " . $e->getCode(). "<br/>";<br>
echo "File: " . $e->getFile(). "<br/>";<br>
echo "Line: " . $e->getLine(). "<br/>";<br>
echo "Trace: " . $e->getTraceAsString(). "<br/>";<br>
echo '
';
}
}
?>

This code should be placed at the beginning of the body part of the html page, or at worst, before the query record code.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/904792.htmlTechArticlephp combines with js to implement click on the hyperlink to perform the delete confirmation operation. First, connect to the database and query the database data: The code is as follows: ?php $dbms='mysql'; //Database type, for developers,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template