-1

Okay I hope my title is not confusing. I have two checkboxes names Delete[] and Update[] followed by a textbox named Name[]. When I tick checkbox Delete[] and click Submit, I ran a query to delete the data based on that Checkbox's value (which I have set to be the primary key of the record) and it worked fine.
My problem started when I tried to do the same thing with my Update[] checkbox. After looking thru some forums I found the explanation I've been looking for:

"Remember that in the POST data all of the textboxes will always be present, but only the checkboxes that are actually checked will be there, meaning that the two arrays will be different sizes unless all the checkboxes are checked. So by doing it with arrays you can never be sure which checkbox relates to which textbox."

He also posted a solution, which I am sorry to admit that I don't quite understand:

"Another way to do it would be to create a hidden input for each checkbox, and in the onsubmit event for the form loop through the checkboxes and set each of the hidden fields.

That way the array of hidden fields representing the checkboxes would be the same size as the array of textboxes."

I will be posting my codes below. What I wanted is for the user to tick the checkbox if they want to update the record, and when it's done my program will show the result of how many records are updated. Currently it does not matter if they tick the checkbox or not, any changes in the textbox will update the database on Submit.

<?php
$q=$_GET["q"];

include($_SERVER['DOCUMENT_ROOT'] . '/clientconf/conf/config.php');
include($_SERVER['DOCUMENT_ROOT'] . '/clientconf/conf/opendb.php');

$sql="SELECT * FROM company WHERE country_id = '".$q."'";

$result = mysql_query($sql);

echo "<table>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<input type=\"hidden\" id=\"ID[]\" name=\"ID[]\" value=\"" . $row['company_id'] . "\">";
  echo "<td width=\"30\"><input type=\"checkbox\" id=\"Delete[]\" 
  name=\"Delete[]\" value=\"" . $row['company_id'] . "\"></td>";
  echo "<td width=\"30\"><input type=\"checkbox\" id=\"Update[]\" 
  name=\"Update[]\" value=\"" . $row['company_id'] . "\"></td>";
  echo "<td width=\"1\"></td>";
  echo "<td><input type=\"text\" id=\"Name[]\" name=\"Name[]\" size=\"35\" value=\"" . $row['name'] . "\"></td>";
  echo "</tr>";
  }
echo "</table>";

include($_SERVER['DOCUMENT_ROOT'] . '/clientconf/conf/closedb.php');

?>

Please note that I have 7 other similar files which has more textbox fields. I am using this example as it is the most simple with only one field which is Name[]. Others can have up to 7 fields.
Below is the main function that I use:

function Add_Record() 
{
 $iRowCt=0;
 $uRowCt=0;
 $dRowCt=0;
 $ctrid=$_SESSION['ctrid'];

 foreach($_POST['Name'] as $row=>$val)
 {
  $ID=($_POST['ID'][$row]);
  $Delete=($_POST['Delete'][$row]);
  $Update=($_POST['Update'][$row]);

  $Name=mysql_real_escape_string($val);
  $Name=trim($Name);

//this delete thing is working fine.
  if (isset($Delete)) 
  {
   $sql=mysql_query("DELETE FROM company
                     WHERE company_id='$Delete'") 
	             or exit (mysql_error());
   $sql=mysql_query("ALTER TABLE company AUTO_INCREMENT = 0") 
		     or exit (mysql_error()); 
   $dRowCt++;
  } 
  
//this is where I found out that i can't use the isset condition for what I wanted to achieve since like what is said earlier, the size of the array will be different.
  if (isset($Update))
  {
   $uRowCt++;
  }

//there are also blank fields on the HTML form if the user wants to insert a new data. 
  if (empty($Name) && !empty($ID)) 
  {
   $_SESSION['err'] = "Records with missing mandatory fields can not be recorded!";
  }
  else if (!empty($Name))
  {
   if (empty($ID))
   {
    $sql=mysql_query("SELECT name FROM company WHERE name='$Name' AND country_id='$ctrid'");
    $row=mysql_fetch_array($sql);

    if ($row < 1)
    {
     $sql=mysql_query("INSERT INTO company
		       VALUES (NULL, '$Name', '$ctrid')") 
		       or exit (mysql_error());
     $iRowCt++;
    }
    else
    {
     $_SESSION['exist'] = "Duplicates of existing company not allowed!";    
    }
   }
//putting the following sql command here causes it to update the record whether or not the user tick the Update[] checkbox. I do this only because I do not know how to achieve what I actually wanted.
   else if (!empty($ID))
   {
    $sql=mysql_query("UPDATE company
	 	     SET name='$Name'
                     WHERE company_id='$ID'") 
		     or exit (mysql_error());
   }
  }
 }
 $iRowCt.=" records added"; 
 $uRowCt.=" records updated"; 
 $dRowCt.=" records deleted"; 
 $_SESSION['iRowCt']=$iRowCt;
 $_SESSION['uRowCt']=$uRowCt;
 $_SESSION['dRowCt']=$dRowCt;

}
3
Contributors
4
Replies
13
Views
7 Years
Discussion Span
Last Post by laljaseh
1

Hey.

You can specify the array index for <input> name arrays, and PHP will respect those in the receiving code.

So you could do:

while($row = mysql_fetch_assoc($query_resault)) {
    echo "<input type='checkbox' name='Update[{$row['id']}]' value='Box #{$row['id']}'>";
    echo "<input type='text' name='Value[{$row['id']}]' value='{$row['value']}'><br />";
}

And, lets say that generates a list of check boxes with IDs 1 through 5, and you only check 2 and 5, PHP would receive:

Array (
  ['Update'] = (
      [2] = Box #2,
      [5] = Box #5
  ),
  ['Value'] = (
      [1] = <the value>,
      [2] = <the value>,
      [3] = <the value>,
      [4] = <the value>,
      [5] = <the value>,
  )
)

Allowing you to do something like:

foreach($_POST['Update'] as $_id => $_label) {
  $newValue = $_POST['Value'][$_id];
  $sql = "UPDATE tbl SET `value` = '{$newValue}' WHERE `id` = {$_id}";
}

See what I mean?

-1

Damn it worked... but now it messed up the other parts of the codes... I'll try to fix it and get back to you. Thanks alot though.. =)
Appreciate the help.

0

After going through hundreds of pages related to this problem, didnt solve my problem, This code really save my day Thanks alot..

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.