• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

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?
0
marcoullis
Asked:
marcoullis
  • 13
  • 2
1 Solution
 
marcoullisAuthor Commented:
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

0
 
marcoullisAuthor Commented:
this produces some 400 check-boxes.

if i select 10 check-boxes how do I make it INSERT INTO the table 10 new rows?
0
 
InsoftserviceCommented:
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
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
marcoullisAuthor Commented:
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.
0
 
marcoullisAuthor Commented:
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

0
 
marcoullisAuthor Commented:
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

0
 
marcoullisAuthor Commented:
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.
0
 
marcoullisAuthor Commented:
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

0
 
marcoullisAuthor Commented:
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.
0
 
marcoullisAuthor Commented:
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

0
 
InsoftserviceCommented:
what is value of $val

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

 mysql_query($sql);  // put it inside
}
     
       }


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

0
 
marcoullisAuthor Commented:
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)
0
 
marcoullisAuthor Commented:
nothing got inserted into the table.
0
 
marcoullisAuthor Commented:
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

0
 
marcoullisAuthor Commented:
got it to work, thanks :-)
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 13
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now