php combines with js to implement click on the hyperlink to perform the deletion confirmation operation, js hyperlink
As the title says, this time we want to execute the js code by clicking on the hyperlink and confirm whether to delete the database data, using php.
First connect to the database and query the database data:
Copy code The code is as follows:
$dbms='mysql'; //Database type, for developers, if they use different databases, they only need to change this, and there is no need to remember so many functions
$host='localhost'; //Database host name
$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); //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:
Copy code 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;
}<br>
}<br>
</script>
Delete database record code:
Copy 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);
$ Result-& gt; bindparam (': id', $ _ get ['conn_id']); // Binded 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.
Haha, this is simple,
Delete
Click OK to jump directly to the delete page.
Click Cancel and leave it unchanged.
php gets url parameters
$id = $_GET[id];
If you still don’t understand anything, you can leave me a message through Baidu Hi.
Delete
When foreach traverses the data, write the id value, and then perform the delete operation on the delete.php page.
Either you include the page connecting to the database into delete.php (not recommended)
Or write them all on one page, and then switch case to determine whether the operation accepts the id and then execute
Delete It is recommended to
delete.php page
connect the database switch($act=$_GET['act']){ case 'del' : .........;break;case....}
http://www.bkjia.com/PHPjc/904015.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/904015.htmlTechArticlephp combines with js to implement click hyperlink to perform delete confirmation operation. The js hyperlink is as titled. This time it is to achieve click hyperlink The link implements executing the js code and confirms whether to delete the database data, using...