Page 1 of 2

Update More Than One Record

Posted: Wed Jan 11, 2006 1:04 pm
by icesolid
I have a script where a manager logs on and checks off which cases to assign to an inspector. Next to the check box is a 3 digit field that the manager fills out that is the number of the inspector that is going to be assigned to that case.

At the bottom of the page I have a submit button that the manager clicks in order to assign these cases to the inspectors. I want to know how after the manager hits submit how can I have the program update the inspector_code in each case record that was selected. If the case was selected I want the inspector field in the database for that record to be updated for each record.

Database Fields:
id
inspector_code

PLEASE HELP!!!

Posted: Wed Jan 11, 2006 3:24 pm
by timvw
I presume you show a list of selectboxes (give them as name the case_id).
Probably want to save them in $_SESSION (so when the form is posted back, you know which you have displayed and are expecting)

All those that were actually selected will appear in $_POST (those that were not checked simply don't appear).
Now loop over those variables, update your database, and you're set :)

More Help!

Posted: Wed Jan 11, 2006 3:30 pm
by icesolid
Thats's not what im trying to do really I do not think.

The loop is what I need help with really, some code example for this would help.

Posted: Wed Jan 11, 2006 4:27 pm
by Ambush Commander
Judging from your specifications, the checkbox would be superfluous. Simply leave boxes that you don't want to be assigned false. Your HTML form will look like:

Code: Select all

<tr><td>Name of assignment</td><td><input type="text" name="assign[assignment_id]" /></td></tr>
...
Which will look like

Code: Select all

$_POST['assign'] = array(
  '4' => 'inspector_number'
  '67' => 'inspector_number'
  //...
);
Then, the loop would look like

Code: Select all

foreach ($_POST['assign'] as $id => $inspector_code) {
  $mysql_id = (int) $id;
  $mysql_inspector_code = (int) $inspector_code;
  mysql_query("INSERT INTO `tablename` (`id`, `inspector_code`) VALUES ('$mysql_id','$mysql_inspector_code')");
}
Totally untested though.

Well...

Posted: Wed Jan 11, 2006 4:49 pm
by icesolid
The inspector_codes are dynamic, there are no certain codes. The codes are assigned my the executive accounts. So naming them in a array as if knowing what they are going to be wouldent work. I still need HELP!!!!!!!!!!!!

Posted: Wed Jan 11, 2006 6:23 pm
by John Cartwright
He only named them in the array to show you an example

So lets break it down a little more for you. As you generate the form, you would generate a bunch of input fields where assigment_id would be the your `case id`.

Code: Select all

<form action="" method="POST">
	<input type="text" name="assign[5]" /> <br />
	<input type="text" name="assign[12]" /> <br />
	<input type="text" name="assign[18]" /> <br />
	<input type="text" name="assign[19]" /> <br />
	<input type="submit">
</form>
So in this instance, we have 4 different cases (which can all be generated dynamically). Lets say I typed in a 3 digit code of 001 for each of those

Code: Select all

Array
(
    [assign] => Array
        (
            [5] => 001
            [12] => 001
            [18] => 001
            [19] => 001
        )

)
As you can see, you don't need to hardcode anything.. your end result should look something like

Code: Select all

if (!empty($_POST['assign'])) {
   foreach ($_POST['assign'] as $caseId => $inspectorId) {
      if (!empty($_POST['assign'][$caseId])) {
         //run update
      }
   }
}

Heres My Code

Posted: Sat Jan 14, 2006 10:08 am
by icesolid
I still need help with this one.

Here is my HTML code:

Code: Select all

<!-- These below are printed out dynamically from the database -->
Assign:                                                                                                                 Inspector Code:

<input type="checkbox" name="id" value="<?php echo $row["id"]; ?>">  <input type="text" name="inspector_code">
<input type="checkbox" name="id" value="<?php echo $row["id"]; ?>">  <input type="text" name="inspector_code">
<input type="checkbox" name="id" value="<?php echo $row["id"]; ?>">  <input type="text" name="inspector_code">
<input type="checkbox" name="id" value="<?php echo $row["id"]; ?>">  <input type="text" name="inspector_code">
Now for each one that is checked off and a value is entered I want the inspector_code to be entered into the database for the id in the database. There could bew only one updated, or there could be more. I need to know how to do this.

I Still Need Help!!!

Posted: Mon Jan 16, 2006 1:22 pm
by icesolid
I still need help to my question, someone please!

Posted: Mon Jan 16, 2006 2:19 pm
by John Cartwright
Something along the lines of

Code: Select all

?>
<form method="post" action="">

<?php

	$ids = array(10,11,12,13,14);
	
	foreach ($ids as $id) {
			echo '<input type="checkbox" name="assign['.$id.']" value="assigned"> <input type="text" name="inspector['.$id.']"> <br />';
	}
	
	if (!empty($_POST['submit'])) {			
		foreach ($_POST['assign'] as $assignID => $check) {
			if (in_array($assign, $_POST['inspector']))	{
				echo 'Run update on assignID: '. $assignID .' with inspector '. $_POST['inspector'][$assignID].'<br>';
			}
		}
	}
	
	
	
?>

	<input type="submit" name="submit" value="submitted">
</form>
Note that I use a hardcoded array of $ids, this should be generated from a mysql_query() of all cases.

should output something along the lines of
Run update on assignID: 10 with inspector 111
Run update on assignID: 11 with inspector 222
Run update on assignID: 12 with inspector 333

hmhmh...

Posted: Mon Jan 16, 2006 2:31 pm
by icesolid
I cant use a hard code of arrays tho, thats my problem.

The inspector_codes are dynamic.

Every part of this is dynamic, I do not know the id of the case, the inspector_code cause they change, and what the user is going to enter obviously, there could be 1000 cases or 500 cases, there could be inspector codes 100-500.

Posted: Mon Jan 16, 2006 2:35 pm
by John Cartwright
I've already said this any twice now, we know that the array is going to be dynamic.. what I'm saying is just plug in your dynamically generated list of cases from the database...

Code: Select all

$result = mysql_query('SELECT `id` FROM `tablename`') or die(mysql_error()); 

$ids = array();
while ($row = mysql_fetch_assoc($result)) {
   array_push($ids, $row['id']);
}

Code Not Working

Posted: Mon Jan 16, 2006 3:49 pm
by icesolid
There is no error on the code. Nothing happens to the database when submit is hit.

Heres my code:

Code: Select all

<form method="post" action="test.php">

<?php
include("connect.php");

$result = mysql_query('SELECT `id` FROM `cases`') or die(mysql_error());

$ids = array();

while($row = mysql_fetch_assoc($result)) {
    array_push($ids, $row['id']);
}

foreach($ids as $id) {
    echo '<input type="checkbox" name="assign['.$id.']" value="assigned"> <input type="text" name="inspector['.$id.']"> <br />';
}
     
if(!empty($_POST['submit'])) {
    foreach ($_POST['assign'] as $assignID => $check) {
        if(in_array($assign, $_POST['inspector']))    {
            mysql_query("UPDATE cases SET inspector_code='" . $_POST['inspector'][$assignID] . "' WHERE id='" . $assignID . "'");
            echo 'Run update on assignID: '. $assignID .' with inspector '. $_POST['inspector'][$assignID].'<br>';
        }
    }
}
?>
<input type="submit" name="submit" value="Assign">
</form>

Posted: Mon Jan 16, 2006 4:01 pm
by John Cartwright
oops, change this line

Code: Select all

if(in_array($assign, $_POST['inspector']))    {
to

Code: Select all

if(in_array($assignID, $_POST['inspector']))    {

Still No Go

Posted: Mon Jan 16, 2006 4:10 pm
by icesolid
Its still not updating, its weird cause there is no errors from the code.

Here is the updated code:

Code: Select all

<form method="post" action="test.php">

<?php
include("connect.php");

$result = mysql_query('SELECT `id` FROM `cases`') or die(mysql_error());

$ids = array();

while($row = mysql_fetch_assoc($result)) {
    array_push($ids, $row['id']);
}

foreach($ids as $id) {
    echo '<input type="checkbox" name="assign['.$id.']" value="assigned"> <input type="text" name="inspector['.$id.']"> <br />';
}
     
if(!empty($_POST['submit'])) {
    foreach ($_POST['assign'] as $assignID => $check) {
        if(in_array($assignID, $_POST['inspector']))    {
            mysql_query("UPDATE cases SET inspector_code='" . $_POST['inspector'][$assignID] . "' WHERE id='" . $assignID . "'");
            echo 'Run update on assignID: '. $assignID .' with inspector '. $_POST['inspector'][$assignID].'<br>';
        }
    }
}
?>
<input type="submit" name="submit" value="Assign">
</form>

Posted: Mon Jan 16, 2006 4:18 pm
by John Cartwright

Code: Select all

mysql_query("UPDATE cases SET inspector_code='" . $_POST['inspector'][$assignID] . "' WHERE id='" . $assignID . "'");
to

Code: Select all

mysql_query("UPDATE cases SET inspector_code='" . $_POST['inspector'][$assignID] . "' WHERE id='" . $assignID . "'") or die(mysql_error());
You should always have error reporting on :wink:

BTW, is any of the

Code: Select all

echo 'Run update on assignID: '. $assignID .' with inspector '. $_POST['inspector'][$assignID].'<br>';
being outputted?