+ Reply to Thread
Results 1 to 3 of 3

Thread: Update Multiple (checked) records

  1. #1
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Update Multiple (checked) records

    I have a recordset, based on a form submit which then echo's to a table - simple.

    What I am trying to do is to have a checkbox to the left of each row (again simple)

    At the base of the table, I would have a sperate blank row with fields and a link or button for "Edit all checked".

    This would then update all records that have been checked with the values entered into the blank line.

    The initial recordset is fine and displaying is fine. Even the checkbox is OK, but I'm having problems with the "value/ checked".

    In addition, I already have a submit button for the initial recordset.

    Current code below

    HELP!!!!!

    head stuff:

    (. connection and db selection has already been made in an include)

    PHP Code:
    <?php
    //set form variable "search for what"
    $crit1_recordlookup "gobbledegook";
    if (isset( 
    $_POST['scriteria1'])) {
      
    $crit1_recordlookup = (get_magic_quotes_gpc()) ?  $_POST['scriteria1'] : addslashes$_POST['scriteria1']);
    }
    //set form variable "search in which column"
    $col1_recordlookup "COMPANY";
    if (isset( 
    $_POST['scolumn1'])) {
      
    $col1_recordlookup = (get_magic_quotes_gpc()) ?  $_POST['scolumn1'] : addslashes$_POST['scolumn1']);
    }
    //set form variable "order by"
    $ord1_recordlookup "COMPANY";
    if (isset( 
    $_POST['sorder'])) {
      
    $ord1_recordlookup = (get_magic_quotes_gpc()) ?  $_POST['sorder'] : addslashes$_POST['sorder']);
    }

    $query_recordlookup sprintf("SELECT * FROM CONTACTS WHERE %s LIKE '%%%s%%' ORDER BY %s"$col1_recordlookup,$crit1_recordlookup,$ord1_recordlookup);
    $recordlookup mysql_query($query_recordlookup ) or die(mysql_error());
    $row_recordlookup mysql_fetch_assoc($recordlookup);
    $totalRows_recordlookup mysql_num_rows($recordlookup);
    ?>
    body results etc...

    PHP Code:
    <h1>Detailed Search</h1>

    <fieldset>
    <form name="form1" method="post" action="">
    Find
      <input name="scriteria1" type="text" id="scriteria1" value="<?php echo $_POST['scriteria1']?>" />
    &nbsp;in
    <select name="scolumn1" id="select3">
      <option value="Some Option">Some Option</option>
      <option value="Some Option">Some Option</option>
    </select>
    &nbsp;order by:&nbsp;
    <select name="sorder" id="select5">
      <option value="Some Option">Some Option</option>
     <option value="Some Option">Some Option</option>
    </select>

    <input type="submit" name="Submit" value="Submit" />

    </fieldset>


    <p>&nbsp;<?php echo $totalRows_recordlookup ?> Records found  </p>
    <?php if ($totalRows_recordlookup 0) { // Show if recordset not empty ?>
      <table class="datatable" width="auto">
        
        <tr class="trheader">
          <td class="td1">&nbsp;</td>
          <td class="td1">Whatever1</td>
          <td class="td1">Whatever2</td>    
        </tr>

        <?php do { ?>
        
        <tr>
          <td class="td1">
            <input name="checkbox" type="checkbox" id="checkbox" value="<?php echo $row_recordlookup['ID']; ?>" />
        </td>
          <td class="td1"><?php echo $row_recordlookup['Whatever1']; ?></td>
          <td class="td1"><?php echo $row_recordlookup['Whatever2']; ?></td>
        </tr>

        <?php } while ($row_recordlookup mysql_fetch_assoc($recordlookup)); ?>

        <tr class="trheader">
          <td class="td1">
    <input name="update" type="submit" id="update" value="Edit checked with" />
          </td>
          <td class="td1">
              <input name="whatever1" type="text" id="whatever" />
          </td>
          <td class="td1">
              <input name="whatever2" type="text" id="whatever2" />
          </td>
          </tr>
      </table>
      
        <?php // Show if recordset not empty ?>
        </form>

    <?php
    mysql_free_result
    ($recordlookup);
    ?>
    Last edited by freecrm; 02-05-2009 at 03:22 AM.

  2. #2
    nightscream is offline x10 Lieutenant nightscream is an unknown quantity at this point
    Join Date
    Feb 2006
    Location
    Hallaar, Belgium
    Posts
    474

    Re: Update Multiple (checked) records

    if i'm not mistaking and you just give every checkbox a name like whatever[] it will become an array and give the value the primary key and you will be able to update them easely. I think you get what I mean.
    ------------------------------------------------------------------------------------------
    If you have any troubles with a website or a script, just send me a pm.

    I also code websites in xHTML/css, can code javascript and php too if needed

  3. #3
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: Update Multiple (checked) records

    Thanks Nightscream

    I have amened this line slightly (about half way down the 2nd part of attached script (do-while loop) to the following...

    PHP Code:
    <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row_recordlookup['ID']; ?>" />
    ..and now added my update fields with a for{} loop and update sql but it aint working and I can't see where I'm going wrong.

    After the last for{} row, I've added another <tr> with the fields to update and another submit button

    PHP Code:
    <tr class="trheader">
          <
    td class="td1"><input name="edit" type="submit" id="edit" value="Update checked records"></td>
          <
    td class="td1"><input name="fsource" type="text" id="fsource" /></td>
    <
    td class="td1"><input name="fsource" type="text" id="fsource" /></td>
        </
    tr

    Then after the table, I've added the update

    PHP Code:
    // Check if edit button active, start this 
            
    if($edit)
            {
            echo 
    "if ($edit) working";
            for(
    $i=0;$i<$count;$i++)
                {
                echo 
    $i;
                echo 
    $checkbox[$i];
                
    $edit_id $checkbox[$i];
                echo 
    $edit_id;
                
    $update_sql sprintf("UPDATE CONTACTS SET WHATEVER1=%s, WHATEVER2=%s WHERE CONTACTID='$edit_id'",
                    
    GetSQLValueString($_POST['whatever1'], "text"),
                    
    GetSQLValueString$_POST['whatever2'], "text")
                    );
                    
                
    $update_result mysql_query($update_sql) or die(mysql_error('could not execute query'));
                
                }
            
            
    // if successful REFRESH
            
    if($update_result)
                {
                echo 
    "<meta http-equiv=\"refresh\" content=\"0;URL=lookupany.php\">";
                }
            }
            
            
    ?> 
    Heeeelp
    Edit:
    OK - cracked it.

    Stupid mistake in the for{} loop using the wrong parameter variable name! Duh..
    Last edited by freecrm; 02-05-2009 at 06:41 AM. Reason: Automerged Doublepost

+ Reply to Thread

Similar Threads

  1. Zoning records are back
    By like2program in forum Free Hosting
    Replies: 2
    Last Post: 05-13-2008, 01:37 PM
  2. New PSP UPGADE
    By IamShipon1988 in forum Gamer's Lounge
    Replies: 4
    Last Post: 09-04-2005, 08:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers