Link to home
Start Free TrialLog in
Avatar of marcoullis
marcoullis

asked on

PHP / MySQL using check-boxes in a form to make multiple INSERT INTO for a MySQL database

I need to download all the members of parliament (stored in a MySQL database) into a HTML form using a PHP loop so that I can attach members names to legislation using check-boxes.

When the form is submitted, all the names with check-boxes selected shall have a row inserted in the table of the specific legislation.

Table structure:

- Legislation: Legislation_ID, name, date, attachment etc...
- Legislation_Support: Legislation_ID, Member_ID, Member_Name (INSERT_INTO)
- Parliament_Member: Member_ID, Member_Name (SELECT)

So the form needs to pull the data from Parliament_Member (some 400 parliamentarians), and present the parliamentarians with their Member_Name as check-boxes. Then upon submit Legislation_Support for the specified Legislation_ID, gets one row inserted for each check-box selected.

Any ideas?
Avatar of marcoullis
marcoullis

ASKER

my check-box script is attached

    echo "<table>";
    echo "<tr>";
    echo "<td><br><br></td>";
    echo "</tr>";
    
	 do { 
         
         echo "<tr>";   
         echo "<td><div align='center'><input type=checkbox name=\"".$row_result['SUBJECT']."\"></div></td>";
         echo "<td> <b>".$row_result['SUBJECT']." </b></option> \n";
         echo "<br>(".$row_result['STATE']." - District ".$row_result['DISTRICT'].")";   
	 echo "<br><br></td>";
	 echo "</tr>";
            
             } while ($row_result = mysql_fetch_assoc($result)); 
             
    echo "</table>";

Open in new window

this produces some 400 check-boxes.

if i select 10 check-boxes how do I make it INSERT INTO the table 10 new rows?
hi,

if i am not wrong you want the data from the multiple selected checkboxes to be submitted in the db .
please confirm.

just do two things

<input type=checkbox name="name_of_member[]" >
// added square bracket [] in name

and in php where u would be taking the post value u will get an array of members selected

$name_of_member = $_POST['name_of_member'];
echo '<pre>';print_r($name_of_member);

foreach($name_of_member as $key =>$val)
{
   insert query with $val as name of the member selected.
}

Please paste the o/p data for rechecking the script
yes i am looking to insert one row for each check value.

trying to apply your solution now, but it would help if your example was clearer.
In the form I have this

    echo "<table>";

    
	 do { 
         
         echo "<tr>";   
         echo "<td><div><input type=checkbox name='body[]' value=\"".$row_result['PARENT_ID']."\"></div></td>";
         echo "<td> <b>".$row_result['SUBJECT']." </b>(".$row_result['PARENT_ID'].")</option> \n";
         echo "<br>(".$row_result['STATE']." - District ".$row_result['DISTRICT'].")";   
	 echo "<br><br></td>";
	 echo "</tr>";
	 
	 $inArr = array();

	 if (isset($_POST["body"])){
	 $inArr = $_POST["body"];
         }
            
             } while ($row_result = mysql_fetch_assoc($result)); 
             
    echo "</table>";

Open in new window

here is the insert script, where I don't know what to do...

 function insertLink( $subject = null, $body = null, $url = null ) {
    if ( $this->parentMode && $this->parentSelected || !$this->parentMode ) {

      if ( $this->parentMode ) {
        $parentID = $this->parentID;
      } else {
        $parentID = 0;
      }

      $sql = "INSERT INTO ISSUES
              (
                PARENT_REF,
                SUBJECT,
                BODY,
                URL
              )
              VALUES
              (
                '".$parentID."',
                '".$subject."',
                '".$body."',
                '".$url."'
              )";

      mysql_query($sql);

      if ( mysql_errno() ) {
        $this->linkSelected       = false;
        $this->error              = LINK_INSERT_FAILED . mysql_error();
        return false;

      } else {
        $this->linkID             = mysql_insert_id();
        $this->linkSubject        = $subject;
        $this->linkBody           = $body;
        $this->linkURL            = $url;
        $this->linkSelected       = true;
        $this->error              = null;
        return true;
      }

    } else {
      $this->error                = PARENT_NOT_SELECTED;
      return null;
    }
  }

Open in new window

On first attempt to submit the form i get:

Array
(
    [0] => 12
    [1] => 54
)
Insert successful.

But in the database it just says "Array", with one row, instead of the expected 2.
here is the php script that handled the processing of the check-box data that produced the incorrect results i mentioned above.

  function insertLink( $subject = null, $body = null, $url = null ) {
    if ( $this->parentMode && $this->parentSelected || !$this->parentMode ) {

      if ( $this->parentMode ) {
        $parentID = $this->parentID;
      } else {
        $parentID = 0;
      }
      
      $body = $_POST['body'];
      echo '<pre>';print_r($body);

      foreach($body as $key =>$val)
{
         $sql = "INSERT INTO ISSUES
              (
                PARENT_REF,
                SUBJECT,
                BODY,
                URL
              )
              VALUES
              (
                '".$parentID."',
                '".$subject."',
                '".$body."',
                '".$url."'
              )";
}



      mysql_query($sql);

      if ( mysql_errno() ) {
        $this->linkSelected       = false;
        $this->error              = LINK_INSERT_FAILED . mysql_error();
        return false;

      } else {
        $this->linkID             = mysql_insert_id();
        $this->linkSubject        = $subject;
        $this->linkBody           = $body;
        $this->linkURL            = $url;
        $this->linkSelected       = true;
        $this->error              = null;
        return true;
      }

    } else {
      $this->error                = PARENT_NOT_SELECTED;
      return null;
    }
  }

Open in new window

Array
(
    [0] => 10
    [1] => 1
)
Insert successful.

OK i tried something a little different using $var and the above result is what i got, but in the database only one row was added, with the value 1. So 10 was missing.
i can confirm it is just adding the last check-box selection. here is the php script. it is not adding any of the previous ones.

function insertLink( $subject = null, $body = null, $url = null ) {
    if ( $this->parentMode && $this->parentSelected || !$this->parentMode ) {

      if ( $this->parentMode ) {
        $parentID = $this->parentID;
      } else {
        $parentID = 0;
      }
      
      $body = $_POST['body'];
      echo '<pre>';print_r($body);

      foreach($body as $key =>$val)
{
         $sql = "INSERT INTO ISSUES
              (
                PARENT_REF,
                SUBJECT,
                BODY,
                URL
              )
              VALUES
              (
                '".$parentID."',
                '".$subject."',
                '".$val."',
                '".$url."'
              )";
}



      mysql_query($sql);

      if ( mysql_errno() ) {
        $this->linkSelected       = false;
        $this->error              = LINK_INSERT_FAILED . mysql_error();
        return false;

      } else {
        $this->linkID             = mysql_insert_id();
        $this->linkSubject        = $subject;
        $this->linkBody           = $body;
        $this->linkURL            = $url;
        $this->linkSelected       = true;
        $this->error              = null;
        return true;
      }

    } else {
      $this->error                = PARENT_NOT_SELECTED;
      return null;
    }
  }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Insoftservice inso
Insoftservice inso
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Array
(
    [0] => 10
    [1] => 12
    [2] => 54
)


Warning:  Invalid argument supplied for foreach() in /home/pseka/public_html/modules/issues/issues.class.php on line 869

Insert successful.

line 869 is:

foreach($inArr as $key=>$val)
nothing got inserted into the table.
Full solution attached... thanks for your patience! :-)

    mysql_select_db( $database );
    $sql = "SELECT PARENT_ID, SUBJECT, STATE, DISTRICT FROM CONGRESS_PARENT ORDER BY SUBJECT ASC";
    $result = mysql_query($sql);
    $row_result = mysql_fetch_assoc($result);
    $totalRows_result = mysql_num_rows($result);
    
    
    echo "<h2>Members in Support</h2> \n";

    
    echo "<table>";

    
	 do { 
         
         echo "<tr>";   
         echo "<td><div><input type=checkbox name=\"body[]\" value=\"".$row_result['PARENT_ID']."\"></div></td>";
         echo "<td> <b>".$row_result['SUBJECT']." </b>(".$row_result['PARENT_ID'].")</option> \n";
         echo "<br>(".$row_result['STATE']." - District ".$row_result['DISTRICT'].")";   
	 echo "<br><br></td>";
	 echo "</tr>";
	 

            
             } while ($row_result = mysql_fetch_assoc($result));

Open in new window

function insertLink( $subject = null, $body = null, $url = null ) {
    if ( $this->parentMode && $this->parentSelected || !$this->parentMode ) {

      if ( $this->parentMode ) {
        $parentID = $this->parentID;
      } else {
        $parentID = 0;
      }
      
      $body = $_POST['body'];
      echo '<pre>';print_r($body);


      foreach($body as $key =>$val)
{
         $sql = "INSERT INTO ISSUES
              (
                PARENT_REF,
                SUBJECT,
                BODY,
                URL
              )
              VALUES
              (
                '".$parentID."',
                '".$subject."',
                '".$val."',
                '".$url."'
              )";
                    mysql_query($sql);
}





      if ( mysql_errno() ) {
        $this->linkSelected       = false;
        $this->error              = LINK_INSERT_FAILED . mysql_error();
        return false;

      } else {
        $this->linkID             = mysql_insert_id();
        $this->linkSubject        = $subject;
        $this->linkBody           = $body;
        $this->linkURL            = $url;
        $this->linkSelected       = true;
        $this->error              = null;
        return true;
      }

    } else {
      $this->error                = PARENT_NOT_SELECTED;
      return null;
    }
  }

Open in new window

got it to work, thanks :-)