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>
<TD height= "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:
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.