Need help with updateing a value for more then one item at one time?!

Discussion in 'PHP' started by free-designer, Jul 12, 2010.

  1. #1
    Hey experts,
    I have a db table consists of: id, username and updates

    i fetched this list with php like the attached image. and the updates column will have a value of 1 or 0

    i added a checkbox if you check the checkbox and submit then updates it will be = 1 otherwise it will be 0, so my problem is how can i update to mysql.

    i have 3 items the ids of them is: (1), (2) and (3).
    How can i update the updates column for each one of the changed items?

    thanks so mush, if anyone needs more info just ask ??
    examble.jpg
     
    Last edited: Jul 12, 2010
    free-designer, Jul 12, 2010 IP
  2. checkwebsite

    checkwebsite Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could do it in two queries , One query which will update all rows to 1
    and another query which would update all of other rows to 0

    So you would process your input in php and then put all of the users that need to be set to 1 in one array and put all of the ones that need to be set to 0 into another array.

    Then you can run array implode on each one of those array to convert them into comma separated strings which will be used to build the query

    In the end your queries would look something like this

    
    UPDATE tableName 
    SET updates = 1
    WHERE id IN (1,4,7,9)
    
    Code (markup):
    
    UPDATE tableName 
    SET updates = 0
    WHERE id IN (2,3,5,6,8)
    
    Code (markup):
     
    checkwebsite, Jul 12, 2010 IP
  3. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Okay, here is the php code:
    -------
    
    $query = mysql_query("SELECT * FROM friends ORDER BY id DESC");
        	echo "<form method=\"POST\">";
        	echo "<ul>";
        	while($item = mysql_fetch_array($query)){
        		if($item['recive_updates'] == "1"){$checked = 'checked="checked"';}else{$checked = "";}
        		echo "<li>".$item['following']." - recive_updates <input type=\"checkbox\" name=\"recive_updates\" ".$checked."></li>";
        	}
        	echo "</ul>";
        	echo "<input type=\"submit\" name=\"update_info\" value=\"Update\">";
        	echo "</form>";
    
    PHP:
    --------
    here is the mysql code that you just will submit to your mysql:
    --------
    
    CREATE TABLE IF NOT EXISTS `friends` (
      `id` int(12) NOT NULL AUTO_INCREMENT,
      `following` varchar(120) CHARACTER SET utf8 NOT NULL,
      `recive_updates` int(11) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
    
    --
    -- Dumping data for table `friends`
    --
    
    INSERT INTO `friends` (`id`, `following`, `recive_updates`) VALUES
    (1, 'Admin', 1),
    (2, 'freedesigner', 0),
    (3, 'freedes', 1);
    
    Code (markup):
    --------

    Just do a connection to mysql.
    What i need is when i change any of these checkboxes save the right data to mysql i mean update recive_updates cloumn.
    check will = 1 and if you uncheck update the recive_updates for the right row to 0

    Thanks
     
    free-designer, Jul 12, 2010 IP