Update Multiple (checked) records

freecrm

New Member
Messages
629
Reaction score
0
Points
0
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:
<?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:
<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:

nightscream

New Member
Messages
948
Reaction score
0
Points
0
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.
 

freecrm

New Member
Messages
629
Reaction score
0
Points
0
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:
<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:
<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:
// 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:
Top