SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Updating Multiple Rows

    I'm developing something which in many ways is along the same lines as the Thread Subscription system in vBulletin 3. A table is shown with each row corresponding to a row in the database. I want the user to be able to adjust their subscription settings, and then click "Update" to update all the subscriptions.

    When the user submits the form, each subscription is cycled though, and updated accordingly. However this system is somewhat ineffecient - say there are 30 subscriptions being updated, that results in 30 update (or delete) queries. Is there anyway I can adjust this system to make it more effecient (i.e. reduce the number of database queries)?

    PHP Code:
    if ($_POST["action"] == "update") {
      while (list(
    $key,$val)=each($_POST["tracktype"])) {
        
    $key intval($key);
     if (
    $key!=0) {
       if (
    $_POST["deletebox"][$key]==1) {
         
    $delete = @mysql_query("DELETE FROM producttracker WHERE trackid = $key AND userid = ".$_SESSION["user"]["userid"]);
        
    qcheck($delete);
       } else {
         
    $val intval($val);
         
    $update = @mysql_query("UPDATE producttracker SET tracktype = $val WHERE trackid = $key AND userid = ".$_SESSION["user"]["userid"]);
           
    qcheck($update);
       }
     }
      }
      
      eval(
    "\$redirectmessage = \"".template('redirect_tracksupdated')."\";");
      
    redirect($redirectmessage,"producttracker.php");
      

    The code below is an example of the HTML for one row. Note that each field is named so that when the form is submitted, an array is created. The number (element) is the ID number of the row.

    Code:
    <tr> 
    	  <td class="smalltext" bgcolor="#E5E5E5"><a href="../product.php?id=5">Final Fantasy VIII</a></td>
    	  <td bgcolor="#E5E5E5" width="100" align="center" class="smalltext">PSone</td>
    	  <td bgcolor="#E5E5E5" width="125" align="center"><select name="tracktype[2]">
    		  <option value="1">All Updates</option>
    				  <option value="2" selected>Articles Only</option>
    				  <option value="3">News Only</option>
    		</select></td>
    	  <td bgcolor="#E5E5E5" width="50" align="center">
    <input type="checkbox" name="deletebox[2]" value="delete"></td>
    	  <td bgcolor="#E5E5E5" width="75" align="center">
    <input type="submit" name="delete[2]" value="Remove" class="button"></td>
    	</tr>
    Many thanks for any advice
    Regards, Ant.

  2. #2
    SitePoint Evangelist
    Join Date
    Nov 2001
    Location
    UK
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No-one got any ideas?
    Regards, Ant.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •