Hi there. I have a page that lists the records of a table. On this page I want to be able to edit/update two attributes of these records by using checkboxes - e.g., being able to update multiple records at once. I thought the smartest way to achieve this would be by using arrays.

Currently I can get the name attribute to update, if both the height and name checkbox are ticked. I cannot get the height attribute to update at all.

<?php
include("connection.php");
$conn = oci_connect($UName,$PWord,$DB) or die("Error");
if (empty($_POST["check1"]))
{
    $query = "SELECT * FROM HORSE ORDER BY HORSE_NAME";
    $stmt = oci_parse($conn,$query);
    oci_execute($stmt);
	$Horses = oci_fetch_array ($stmt);
?>
<?php
while ($Horses = oci_fetch_array ($stmt))
{
?>
        <tr>
          <td><?php echo $Horses["HORSE_ID"]; ?></td>
          <td><input type="checkbox" name="check1[]" value="<?php echo $Horses["HORSE_ID"]; ?>"><input type="text" size="5" name="<?php echo $Horses["HORSE_ID"]; ?>" value="<?php echo $Horses["HORSE_HEIGHT"]; ?>"></td>
          <td><input type="checkbox" name="check2[]" value="<?php echo $Horses["HORSE_ID"]; ?>"><input type="text" size="20" name="<?php echo $Horses["HORSE_ID"]; ?>" value="<?php echo $Horses["HORSE_NAME"]; ?>"></td>
        </tr>
<?php
}
?>
      </table><p />
        <h4><input type="submit" value="Update Selected Horses"></h4>
    </form>
<?php
oci_free_statement($stmt);
}
else
{
	foreach($_POST['check1'] as $horse_id)
	{
	$query = "UPDATE HORSE SET HORSE_HEIGHT = ".$_POST[$horse_id]." WHERE HORSE_ID ='".$horse_id."'";
	$stmt = oci_parse($conn,$query);
	oci_execute($stmt);
	}
	
	foreach($_POST['check2'] as $horse_name)
	{
		$query = "UPDATE HORSE SET HORSE_NAME = '$_POST[$horse_name]' WHERE HORSE_ID =''.$horse_name.''";
		$stmt = oci_parse($conn,$query);
		oci_execute($stmt);
	}
}
?>

The Text boxes for check1 and check2 have the same name... The horse ID. If you view the resolved HTML of the web page you will see that they have the same name and form field names need to be unique. Since the text fields are not unique, they are stepping on each other. You should prefix the text box names with the names of the database fields, like this:

<td><input type="checkbox" name="check1[]" value="<?php echo $Horses["HORSE_ID"]; ?>">

<!-- Creating Unique Field names for text fields -->
<input type="text" size="5" name="<?php echo 'Height_' . $Horses["HORSE_ID"]; ?>" value="<?php echo $Horses["HORSE_HEIGHT"]; ?>"></td>

This way every form field would have a unique name.

To address a possible future error, though I have no idea how you have oci set up but line 9 seems a little redundant as you're doing that on line 12 anyway and depending on how you have oci setup, this could cause the fetch_array to move the pointer ahead one, skipping the first record. Unless of course it's designed to do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.