php结合js实现点击超链接执行删除确认操作,js超链接
如题,本次是要实现点击超链接实现执行js代码,并确认是否删除数据库数据,采用php。
首先链接数据库,查询数据库数据:
复制代码 代码如下:
$dbms='mysql'; //数据库类型 ,对于开发者来说,使用不同的数据库,只要改这个,不用记住那么多的函数
$host='localhost'; //数据库主机名
$dbName='db_database19'; //使用的数据库
$user='root'; //数据库连接用户名
$pass='root'; //对应的密码
$dsn="$dbms:host=$host;dbname=$dbName";
try {
$pdo = new PDO($dsn, $user, $pass); //初始化一个PDO对象,就是创建了数据库连接对象$pdo
$query="select * from tb_pdo_mysql"; //定义SQL语句
$result=$pdo->prepare($query); //准备查询语句
$result->execute(); //执行查询语句,并返回结果集
while($res=$result->fetch(PDO::FETCH_ASSOC)){ //while循环输出查询结果集,并且设置结果集的为关联索引
?>
}
} catch (PDOException $e) {
die ("Error!: " . $e->getMessage() . "
");
}
?>
在超链接删除处调用一javascript方法,传递记录id,js方法为:
复制代码 代码如下:
<script><br />
function del(_id) {<br />
if (confirm("确认删除"))<br />
{<br />
window.location.href="index.php?conn_id="+_id; //本页面刷新<br />
}<br />
}<br />
</script>
删除数据库记录代码:
复制代码 代码如下:
if(@$_GET['conn_id']!=""){
$dbms='mysql'; //数据库类型 ,对于开发者来说,使用不同的数据库,只要改这个,不用记住那么多的函数
$host='localhost'; //数据库主机名
$dbName='db_database19'; //使用的数据库
$user='root'; //数据库连接用户名
$pass='root'; //对应的密码
$dsn="$dbms:host=$host;dbname=$dbName";
try {
$pdo = new PDO($dsn, $user, $pass); //初始化一个PDO对象,就是创建了数据库连接对象$pdo
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$query="delete from tb_pdo_mysql where Id=:id";
$result=$pdo->prepare($query); //预准备语句
$result->bindParam(':id',$_GET['conn_id']); //绑定更新的数据
$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 '';
}
}
?>
这段代码放在html页面body部分最开头处,最坏也要放在查询记录代码之前。
呵呵,这个简单啊,
删除
删除
点确定就直接跳到删除页去了。
点取消就原样不动。
php获取url参数
$id = $_GET[id];
要是还有什么不明白的,可以真通过百度Hi给我留言。
foreach遍历数据的时候 写上id值,然后delete.php页面执行删除操作。
要么你把连数据库的页面包含到 delete.php(不推荐)
要么 都写在一个页面,然后switch case 判断操作 接受id 然后执行
删除 这样比较推荐
delete.php页面里
连好数据库 switch($act=$_GET['act']){ case 'del': .........;break;case....}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.