Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 小女子求教,如何在点击php网页里table中的某记录时,删除数据库中相应记录?

小女子求教,如何在点击php网页里table中的某记录时,删除数据库中相应记录?

Jun 23, 2016 pm 01:43 PM

比如php编写的网页中table类似于:

序号        姓名       性别        删除           编辑
1            张三       男            删除           编辑
2            李四       女            删除           编辑

其中,序号、姓名、性别等都是直接从数据库读出来的。

如何做到,在点击序号为1的“删除”时,从数据库中删除序号为1的整条记录?
(删除是href格式的,链接到处理页面。)

想通过点击“删除”时获取序号,然后通过表单POST传递给处理页面,
由处理页面对数据库进行操作。 
但是不知道如何获取这个序号啊~~~~~~~~~~
谢谢大神啦


回复讨论(解决方案)

这个得用js获取序号然后赋值到表单当中一个字段里通过post进行操作。
如果就是一个删除完全可以通过Ajax来实现这样就避免提交表单时整个页面都刷新

这个得用js获取序号然后赋值到表单当中一个字段里通过post进行操作。
如果就是一个删除完全可以通过Ajax来实现这样就避免提交表单时整个页面都刷新



不单单是删除,类似的还有“编辑”,也是页面跳转,在新的页面中做处理。
请问,这个js获取序号怎么实现啊,还有赋值到表单中一个字段……
尝试了好多都不对……
比如以下的,提示找不到row
<a href = "delete.php ? id = ' ".$row['id']. " ' ">删除</a>
Copy after login
Copy after login
Copy after login

找不到row?
序号 姓名 性别 删除 编辑
1 张三 男 删除 编辑
2 李四 女 删除 编辑

那?些??你是如何?出的??出的代???看看

找不到row?
序号 姓名 性别 删除 编辑
1 张三 男 删除 编辑
2 李四 女 删除 编辑

那?些??你是如何?出的??出的代???看看


通过 $sqklink,
$sql="select * from users ",
$mysql_query()来读取的~~~~

关键是这个row它好像识别不了啊 。。。

http://blog.csdn.net/MoreWindows/article/details/7102362
这篇文章能够参考,就是通过处理页面获取Id序号来处理,

但是依旧不知道怎么获取这个id 啊。。。。


这个得用js获取序号然后赋值到表单当中一个字段里通过post进行操作。
如果就是一个删除完全可以通过Ajax来实现这样就避免提交表单时整个页面都刷新



不单单是删除,类似的还有“编辑”,也是页面跳转,在新的页面中做处理。
请问,这个js获取序号怎么实现啊,还有赋值到表单中一个字段……
尝试了好多都不对……
比如以下的,提示找不到row
<a href = "delete.php ? id = ' ".$row['id']. " ' ">删除</a>
Copy after login
Copy after login
Copy after login


你的 程序是不是php和html混写到一起的。你在循环数组的时候就可以用数组下标当序号然后删除就想你那样写
<a href = "delete.php ? id = ' ".$row['id']. " ' ">删除</a>你说找不到row什么意思啊。如果有报错把它完整的粘贴出来

<?php// 执行 SQL$sql="select * from users ";// 执行查询$result = mysql_query($sql);// 结果// 参见 mysql_result(), mysql_fetch_array(), mysql_fetch_row() 等。while ($row = mysql_fetch_assoc($result)) {    echo $row['序号'];    echo $row['姓名'];    echo $row['性别'];echo '<a href ="delete.php?id = '.$row['序号']. ' "   >删除</a>';}// 释放关联结果集的资源// 在脚本结束的时候会自动进行mysql_free_result($result);?>
Copy after login


把数据库的字段替换一下,里面的 序号 姓名 性别

?了一?例子,看看。

<?php$conn=@mysql_connect('dbhost','dbusername','dbpassword')  or die(mysql_error());mysql_select_db('dbname',$conn) or die(mysql_error());$sqlstr = "select * from users";$query = mysql_query($sqlstr) or die(mysql_error());while($thread=mysql_fetch_assoc($query)){	$result[] = $thread;}?><table><tr><td>序?</td><td>姓名</td><td>性?</td><td>?除</td><td>??</td></tr><?phpforeach($result as $row){?>	<tr>	<td><?php echo $row['id'] ?></td>	<td><?php echo $row['name'] ?></td>	<td><?php echo $row['gender'] ?></td>	<td><a href="del.php?id=<?php echo $row['id'] ?>">?除</a></td>	<td><a href="mod.php?id=<?php echo $row['id'] ?>">??</a></td></tr><?php}?></table>
Copy after login



这个得用js获取序号然后赋值到表单当中一个字段里通过post进行操作。
如果就是一个删除完全可以通过Ajax来实现这样就避免提交表单时整个页面都刷新



不单单是删除,类似的还有“编辑”,也是页面跳转,在新的页面中做处理。
请问,这个js获取序号怎么实现啊,还有赋值到表单中一个字段……
尝试了好多都不对……
比如以下的,提示找不到row
<a href = "delete.php ? id = ' ".$row['id']. " ' ">删除</a>
Copy after login
Copy after login
Copy after login


你的 程序是不是php和html混写到一起的。你在循环数组的时候就可以用数组下标当序号然后删除就想你那样写
<a href = "delete.php ? id = ' ".$row['id']. " ' ">删除</a>你说找不到row什么意思啊。如果有报错把它完整的粘贴出来



谢谢你哈,热心人。
(原谅我不能直接贴代码,因为编程的机子无法联网……我只能尽量描述,谢谢你的耐心)
是把php和html混合在一起写的。
目前有两个网页,users.php和delete.php
其中,user.php实现了用table显示各个用户的信息,包括序号,姓名等,每条用户记录后都跟一个“删除”。实现如下:
…… <a href = "delete.php ? id = ' ".$row['id']. " ' ">删除</a> ……
序号 姓名 ……
Copy after login

这样就通过POST提交到href后边的delete.php了吧?
打算通过delete.php实现用这个id检索到数据库中的相应记录,然后删除:
$sql="delete * from users where id =' ". $_POST['id']. " ' ";
Copy after login

但是,出错提示是:POST['id']无法识别这个id,说是无效检索名。
不知道哪里出错了~~·

你这个虽然有form表单在外面。但不是post提交就是URL提交(通过地址栏提交的)所以你这句话
$sql="delete * from users where id =' ". $_POST['id']. " ' ";应该这样写
$sql="delete * from users where id =' ". $_GET['id']. " ' ";

但是,出错提示是:POST['id']无法识别这个id,说是无效检索名。这句话是不是在delete.php报出的啊?
如果你的user.php页面写不好可以参考8楼的

但是,出错提示是:POST['id']无法识别这个id,说是无效检索名。这句话是不是在delete.php报出的啊?
如果你的user.php页面写不好可以参考8楼的


对,是在delete.php中出错了
提示:Undefined index id in delete.php on line 4

我先去试试你们给的建议,谢谢你~~~

<a href="delete.php?id=<?php echo $row['id'] ?>">删除</a>
Copy after login
Copy after login
Copy after login

1、去掉 url 中的多余空格和引号
2、取值用 $_GET['id'],而不是 $_POST['id']
3、如果你确实想用 post 方式。那可以使用 ajax

Copy after login


把数据库的字段替换一下,里面的 序号 姓名 性别




?了一?例子,看看。

<?php<?php}?></table>
Copy after login





你这个虽然有form表单在外面。但不是post提交就是URL提交(通过地址栏提交的)所以你这句话
$sql="delete * from users where id =' ". $_POST['id']. " ' ";应该这样写
$sql="delete * from users where id =' ". $_GET['id']. " ' ";





<a href="delete.php?id=<?php echo $row['id'] ?>">删除</a>
Copy after login
Copy after login
Copy after login

1、去掉 url 中的多余空格和引号
2、取值用 $_GET['id'],而不是 $_POST['id']
3、如果你确实想用 post 方式。那可以使用 ajax



谢谢各位的耐心回复,在你们的帮助下问题已解决,小菜鸟很感激。o(∩_∩)o
其中,7,8,12,15楼的参考价值最大
<a href="delete.php?id=<?php echo $row['id'] ?>">删除</a>
Copy after login
Copy after login
Copy after login

这段代码是对的,为了获取当前行的id,关键是在处理页中一定要用 $_GET['id'],而且id的i是小写,
我把这个id与数据库字段Id搞混了,所以写成了 $_GET['Id'],导致一直出错。
希望对有同样困扰的人有帮助。
再次谢谢耐心回答问题的人。
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

See all articles