eatsoup Posted November 12, 2012 Share Posted November 12, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/270588-edit-multiple-at-same-time/ Share on other sites More sharing options...
AyKay47 Posted November 12, 2012 Share Posted November 12, 2012 Please post the relevant code that you have so far. Quote Link to comment https://forums.phpfreaks.com/topic/270588-edit-multiple-at-same-time/#findComment-1391811 Share on other sites More sharing options...
Barand Posted November 12, 2012 Share Posted November 12, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/270588-edit-multiple-at-same-time/#findComment-1391828 Share on other sites More sharing options...
eatsoup Posted November 12, 2012 Author Share Posted November 12, 2012 Wow thanks for that example, I will try it tonight and post updates. Quote Link to comment https://forums.phpfreaks.com/topic/270588-edit-multiple-at-same-time/#findComment-1391840 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.