Jump to content

Edit Multiple At Same Time


eatsoup

Recommended Posts

Hello all,

 

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

 

Mysql design:

 

Table_users:

ID,USERNAME

1,Name1

2,Name2

 

 

Table_comments:

ID,USERNAME_ID,COMMENT

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?

|Add

 

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.

Luuk

Link to comment
https://forums.phpfreaks.com/topic/270588-edit-multiple-at-same-time/
Share on other sites

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

 

Here's an example

 

<?php
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'>

</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.