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

  • 2021-07-22 09:21:17
  • OfStack

For example, this time, we want to click on hyperlinks to implement js code, and confirm whether to delete database data and adopt php.

First, link the database and query the database data:


<?php
$dbms='mysql';                         // Database type , For developers to use different databases, just change this and don't have to remember so many functions
$host='localhost';                     // Database Hostname
$dbName='db_database19';            // Database used
$user='root';                          // Database connection user name
$pass='root';                          // Corresponding password
$dsn="$dbms:host=$host;dbname=$dbName";
try {
$pdo = new PDO($dsn, $user, $pass);     // Initialization 1 A PDO Object, that is, the database connection object is created $pdo
$query="select * from tb_pdo_mysql";    // Definition 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 out the query result set and set the result set to the associated index
?>     
<tr>
<td height="22" 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/>");
}
?>

The 1javascript method is called at the hyperlink deletion, passing the record id, and the js method is:


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

Delete database record code:


<?php
    if(@$_GET['conn_id']!=""){
    $dbms='mysql';                         // Database type , For developers to use different databases, just change this and don't have to remember so many functions
    $host='localhost';                     // Database Hostname
    $dbName='db_database19';            // Database used
    $user='root';                          // Database connection user name
    $pass='root';                          // Corresponding password
    $dsn="$dbms:host=$host;dbname=$dbName";
    try {
        $pdo = new PDO($dsn, $user, $pass);     // Initialization 1 A PDO Object, that is, the database connection object is created $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:<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 section of the html page, and at worst before the query record code.


Related articles: