Jump to content

Edit Multiple At Same Time


Recommended Posts

Hello all,


I'm fairly new to php, and can't figure this thing out:


Mysql design:










1,1,"Comment 1"

2,1,"Comment 2"


I use a for loop to fill a form with <input type=”text”>

So i have the following output:

Name: Name1

Comments:(input type=text)

Comment 1|Delete?

Comment 2|Delete?



I want to be able to edit the existing comments, or add a new comment.

How can I?:

-list all the comments, and display one extra <input> form for a new comment

-make sure that if i edit both “comment 1” and “comment 2” all gets executed as a mysql command

-perhaps make it possible to edit “comment 1” and the new blank “comment 3” at the same time


If anyone can help me out, it would be great :)


Thanks in advance.


Link to comment
Share on other sites

Use an "INSERT ... ON DUPLICATE KEY UPDATE ..." query.


Here's an example


include("testDBconnect.php");  //connect to database    

if (isset($_POST['userid'])) {
   $userid = intval($_POST['userid']);
   * update comments
   if (isset($_POST['comment'])) {
    $updates = array();
    foreach ($_POST['comment'] as $id => $com) {

	    if ($id == 0 && !empty($com)) {
		    $updates[] = sprintf("(null, %d, '%s')", $userid, mysql_real_escape_string($com));
	    } else {
		    $updates[] = sprintf("(%d, %d, '%s')", intval($id), $userid, mysql_real_escape_string($com));
    * insert new comments
    * and update existing
    $sql = "INSERT INTO comments (id, userid, comment) VALUES " .
		    join (",\n", $updates) .
		    "ON DUPLICATE KEY UPDATE comment = VALUES(comment)";
    mysql_query($sql) or die(mysql_error()."<pre>$sql</pre>");
    $userid = $comments = '';

   else {

    * get comments
    $sql = "SELECT id, comment
		    FROM comments
		    WHERE userid = $userid";
    $comments = '<h3>Comments</h3>';
    $res = mysql_query($sql);
    while (list($id, $com) = mysql_fetch_row($res)) {
	    $comments .= "<textarea name='comment[$id]' cols='50' rows='5'>$com</textarea><br /><br />\n";
    $comments .= "<textarea name='comment[0]' cols='50' rows='5'></textarea>";
else {
   $userid = $comments = '';

<form method='post' action=''>

Enter UserID <input type='text' name='userid' size='5' value="<?=$userid?>"><br />

<?php if ($userid) echo $comments; ?>

<input type='submit' value='Submit' name='btnSubmit'>


Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.