74769

'Edit' function for forum posts and such

Question:

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.

Answer1:

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

<ol><li>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.</li> <li>A file that receives the changed information and updates the database.</li> </ol>

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: http://www.domain.com/url_to_go_to_after_update"); ?>

Answer2:

If you just need an idea how to create a basic edit form in PhP, that's easy enough. When they click the edit button take them to a new form. Pull the content from the database, using whatever database accessing api you are, and then initialize the field with it. For example, where $content has the content of the field:

echo '<textarea name="content">'.htmlspecialchars($content).'</textarea>';

When they submit the form, take whats now in the field and use it to update the table. It's the same as the original insert script, except that you use update statements instead of insert.

Answer3:

I'm not sure I understood what you said. If you want a way to edit things in place, you can use this jQuery plugin: <a href="http://www.appelsiini.net/projects/jeditable" rel="nofollow">Jeditable</a> (with Ajax).

Answer4:

To extend Daniel's code a bit

<?php $filename = "file.txt"; if ($_SERVER['REQUEST_METHOD'] == 'POST']) { file_put_contents($filename, $_POST['content']); header("Location: ".$_SERVER['PHP_SELF']); exit; } $content = htmlspecialchars(file_get_contents($filename)); ?> <form method="POST"> <textarea name="content"><?php echo $content?></textarea><br> <input type="submit"> </form>

Recommend

  • Repeated calls to CreateCompatibleBitmap() eventually failure with GetLastError() == 6
  • php login session login variables
  • Are the cmdlets in a pipeline executing in parallel?
  • ASP.NET Membership ChangePassword control - Need to check for previous password
  • Autolayout collapsing space when removing intermediate views
  • Overflow for background color of text
  • XSLT function returns different results [Saxon-EE vs Saxon-HE/PE]
  • Filter by multiple patterns with filter() and str_detect()
  • Google tts api giving me blank mp3
  • How to filter children based on whether the parent is filtered in D3?
  • Automate editable PDF
  • Cycle R,G,B vales as HUE?
  • Linux could not find metis.h
  • Using VCL Styles gives a exception / crash in TOpenDialog
  • in Gwt, there are 2 different packages (or 2 options) for doing drag n Drop? Which one is better?
  • What is the diff. between default.properties and project.properties?
  • jQuery how to translate livequery to on?
  • Identifying dates in strings using NLTK
  • How can I run an Illustrator javascript on all files in a directory?
  • Visual Studio MSB3721 error when compiling a __device__ function call from another file
  • Getting/building the SQL (with parameters) from NHibernate 3.2
  • didSelectItemAtIndexPath of UICollectionView inside of a UIScrollView is not getting called
  • Firestore: Version history of documents
  • cSPADE data mining in R using arulesSequences - Error while converting to “transactions” format
  • Symfony - Setting Cookie onKernelRequest
  • Haskell program that can handle any arbitrary deterministic finite automaton
  • playing mp3 from nsbundle
  • How to redirect into different page by user type in php and mysql
  • Generate and export point cloud from Project Tango
  • Using redis as an LRU cache for postgres
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Google App Engine backend servlet not responding
  • Computing the discrete fourier transform of audio data with FFTW
  • Make checkout phone field optional for specific countries in WooCommerce
  • Excel VBA : conditional formatting of sheet1 cells from sheet2 values in excel 2007
  • How to use FirstOrDefault inside Include
  • PHP Permalinks.. how to change?
  • media foundation H264 decoder not working properly
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web