由 ADMIN 编辑数据

发布于 2024-10-26 17:34:55 字数 143 浏览 4 评论 0原文

我是 PHP 新手,正在处理小型本地网页和数据库,该网页和数据库获取用户信息,数据库存储相同的用户信息。如果我使用 ADMIN 登录,它会显示所有数据。我的要求是登录用户是管理员,那么他有权编辑我存储在数据库中的所有用户信息。这是使用 GET 方法来完成的。它将如何运作?

I am new to PHP and working on small local webpage and database that takes user information and database stores the same user information .If i Login with ADMIN it shows all data. My requirement is that the loggined user is an admin, then he has a right to edit all the informtion of the users that i stored in the database.And this is to be done using GET method . How it will be working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘你却要生生世世 2024-11-02 17:34:55

下面是一些示例代码,纯粹是为了演示如何使用 GET 方法表单更新表。该代码没有任何类型的错误检查,并假设您已经知道如何连接到数据库(及其 MySQL)。

假设您已到达邀请您编辑数据的页面,您正在编辑的记录由 URL 上的“id”变量引用,该变量与数据库表中的数字主键相匹配。

<?php
$SQL = "SELECT myField1,myField2 FROM myTable WHERE myKeyField = '".intval($_GET['id'])."'";
$QRY = mysql_query($SQL);

$DATA = mysql_fetch_assoc($QRY);
?>

<form method='get' action='pageThatStoresData.php'>
  <input type='hidden' name='key' value='<?php echo $_GET['id']; ?>' />
  <input type='text' name='myField1' value="<?php echo $DATA['myField1']; ?>" /> 
  <input type='text' name='myField2' value="<?php echo $DATA['myField2']; ?>" /> 
  <button type='submit'>Submit</button>
</form>

因此,这将为您提供一个页面,该页面从表中取出数据,将其显示在带有预填充值的表单中,并在提交时转到如下 URL:

http://mydomain.com/pageThatStoresData.php?key=1&myField1=someData&myField2=someMoreData

在该页面中,您可以访问变量“key”,通过 $_GET 方法获取“myField1”、“myField2”。

然后您只需要更新该页面中的表格:

$SQL = "UPDATE myTable 
        SET myField1 = '".mysql_real_escape_string($_GET['myField1'])."',
            myField2 = ".mysql_real_escape_string($_GET['myField1'])."
        WHERE key = '".intval($_GET['key'])."'
        ";

$QRY = mysql_query($SQL);

请注意:上面的代码不适合直接复制/粘贴,因为它不执行任何错误检查等,它纯粹是一个功能示例(并直接在此处键入,因此我如有错别字请见谅!)

Heres some example code purely to demonstrate how to update a table using a GET method form. The code doesn't have any kind of error checking and assumes you already know how to connect to your database (and that its MySQL).

Assuming you've landed on a page which invites you to edit data, which record you're editing is referenced by an 'id' variable on the URL which matches a numerical primary key in your database table.

<?php
$SQL = "SELECT myField1,myField2 FROM myTable WHERE myKeyField = '".intval($_GET['id'])."'";
$QRY = mysql_query($SQL);

$DATA = mysql_fetch_assoc($QRY);
?>

<form method='get' action='pageThatStoresData.php'>
  <input type='hidden' name='key' value='<?php echo $_GET['id']; ?>' />
  <input type='text' name='myField1' value="<?php echo $DATA['myField1']; ?>" /> 
  <input type='text' name='myField2' value="<?php echo $DATA['myField2']; ?>" /> 
  <button type='submit'>Submit</button>
</form>

So, this will give you a page that takes the data out of your table, displays it in a form with pre-filled values and on submit, will go to a URL like:

http://mydomain.com/pageThatStoresData.php?key=1&myField1=someData&myField2=someMoreData

In that page, you can access variables 'key', 'myField1', 'myField2' via the $_GET method.

Then you just need to update your table within that page:

$SQL = "UPDATE myTable 
        SET myField1 = '".mysql_real_escape_string($_GET['myField1'])."',
            myField2 = ".mysql_real_escape_string($_GET['myField1'])."
        WHERE key = '".intval($_GET['key'])."'
        ";

$QRY = mysql_query($SQL);

PLEASE NOTE: The code above is unsuitable for a straight copy/paste as it doesn't do any error checking etc, its purely a functional example (and typed straight in here so I apologise if there are any typos!).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文