论坛帖子等的“编辑”功能

编程入门 行业动态 更新时间:2024-10-09 12:32:01
本文介绍了论坛帖子等的“编辑”功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在网上寻找一个脚本,演示如何让我的网站上的用户能够编辑字段等,但我找不到任何有关它的信息。所以我想知道如果有人能向我解释它是如何工作的或只是用脚本来演示?为了说清楚,我希望用户能够通过点击编辑并按下按钮来更新他们所提交的内容。

<编辑:我忘了提及已更改的内容应该更新MySQL数据库中的表。 解决方案

您需要2个PHP文件来做到这一点。您可以使用单个文件,但这种概念更容易解释。

  • 一种将数据库内容加载到字段的窗体用户可以编辑这些值,然后在完成后按下按钮提交它们。
  • 接收更改信息并更新数据库的文件。
  • 以下是第一个文件的代码示例:

    < ;?php //连接到SQL $ dbcnx = @mysql_connect(localhost,db_name,password); if(!$ dbcnx){ echo(< P>目前无法连接到数据库服务器。< / p>); exit(); } //连接数据库 $ dbcon = @mysql_select_db(db_table,$ dbcnx); if(!$ dbcon){ echo(< P>无法在此时查找数据库表。< / p>); exit(); #查询准备数据 $ id = intval($ _ GET [id]); #从数据库中选择标题和描述字段 $ sql =SELECT * FROM table_name WHERE id = $ id; $ result = mysql_query($ sql)或die(mysql_error()); #通过使用$ row ['col_name'] $ row = mysql_fetch_array($ result); ?> < h3>编辑< / h3> < form action =save_edit.phpenctype =multipart / form-datamethod =postname =myForm/> < table> < tr> < td>< b>标题< / b>< / td> < td>< input type =textsize =70maxlength =100name =titlevalue =<?php echo $ row ['title']?> >< / TD> < / tr> < tr> < td>< b>说明< / b>< / td> < td>< textarea cols =80rows =18name =description><?php echo $ row ['description']; ?>< / textarea的>< / TD> < / tr> < / table> < input type =hiddenname =idvalue =<?php echo $ id;?> /> < input name =entertype =submitvalue =Edit> < / form> <?php mysql_close($ dbcnx); ?>

    以下是第二个文件的代码示例,用于接收用户所做的更改更新数据库。

    <?php //连接到SQL $ dbcnx = @mysql_connect (localhost,db_name,password); if(!$ dbcnx){ echo(< P>目前无法连接到数据库服务器。< / p>); exit(); } //连接数据库 $ dbcon = @mysql_select_db(db_table,$ dbcnx); if(!$ dbcon){ echo(< P>无法在此时查找数据库表。< / p>); exit(); #为查询准备数据 $ id = intval($ _ POST [id]); foreach($ _POST as $ key => $ value)$ _POST [$ key] = mysql_real_escape_string($ value); $ sql =UPDATE table_name SET title ='$ _ POST [title]', description ='$ _ POST [description]', WHERE id = $编号; $! $ b if(!mysql_query($ sql,$ dbcnx)){ die('Error:'。mysql_error()); } mysql_close($ dbcnx); 标题(location:www.domain/url_to_go_to_after_update); ?>

    I was looking online for a script that demonstrates how I would go about making it possible for users on my site able to edit fields and such, but I could not find anything about it. So I was wondering if someone could explain to me how it works or just demonstrate with a script? To make it clear, I want users to be able to edit stuff that they've submitted by simply clicking 'edit' and pressing a button to update whatever it was they changed.

    Edit: I forgot to mention that what's been changed should update a table in a MySQL database.

    解决方案

    You need 2 PHP files to do this. You could use a single file but the concept is easier to explain this way.

  • A form that will load the database content into the fields where users can then edit the values and then submit them for change by pressing a button once done.
  • A file that receives the changed information and updates the database.
  • Here is a code example for the first file:

    <?php // connect to SQL $dbcnx = @mysql_connect("localhost", "db_name", "password"); if (!$dbcnx) { echo( "<P>Unable to connect to the database server at this time.</P>" ); exit(); } // connect to database $dbcon = @mysql_select_db("db_table", $dbcnx); if (!$dbcon) { echo( "<P>Unable to locate DB table at this time.</P>" ); exit(); } #data preparation for the query $id = intval($_GET["id"]); # selects title and description fields from database $sql = "SELECT * FROM table_name WHERE id=$id"; $result = mysql_query($sql) or die(mysql_error()); # retrieved by using $row['col_name'] $row = mysql_fetch_array($result); ?> <h3>Edit</h3> <form action="save_edit.php" enctype="multipart/form-data" method="post" name="myForm" /> <table> <tr> <td><b>Title</b></td> <td><input type="text" size="70" maxlength="100" name="title" value="<?php echo $row['title'] ?>"></td> </tr> <tr> <td><b>Description</b></td> <td><textarea cols="80" rows="18" name="description"><?php echo $row['description']; ?></textarea></td> </tr> </table> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <input name="enter" type="submit" value="Edit"> </form> <?php mysql_close($dbcnx); ?>

    And here is an example of code for the second file where it receives the changes made by the user and updates the database.

    <?php // connect to SQL $dbcnx = @mysql_connect("localhost", "db_name", "password"); if (!$dbcnx) { echo( "<P>Unable to connect to the database server at this time.</P>" ); exit(); } // connect to database $dbcon = @mysql_select_db("db_table", $dbcnx); if (!$dbcon) { echo( "<P>Unable to locate DB table at this time.</P>" ); exit(); } #data preparation for the query $id = intval($_POST["id"]); foreach ($_POST as $key => $value) $_POST[$key] = mysql_real_escape_string($value); $sql = "UPDATE table_name SET title='$_POST[title]', description='$_POST[description]', WHERE id=$id"; if (!mysql_query($sql,$dbcnx)) { die('Error: ' . mysql_error()); } mysql_close($dbcnx); header ("location: www.domain/url_to_go_to_after_update"); ?>

    更多推荐

    论坛帖子等的“编辑”功能

    本文发布于:2023-10-10 18:34:35,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:编辑   功能   帖子   论坛

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!