Solved

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

Posted on 2012-04-12
15
373 Views
Last Modified: 2012-06-21
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
Comment
Question by:marcoullis
  • 13
  • 2
15 Comments
 

Author Comment

by:marcoullis
ID: 37841118
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
 

Author Comment

by:marcoullis
ID: 37841121
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
 
LVL 15

Expert Comment

by:Insoftservice
ID: 37841194
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
 

Author Comment

by:marcoullis
ID: 37841205
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
 

Author Comment

by:marcoullis
ID: 37841208
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
 

Author Comment

by:marcoullis
ID: 37841218
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
 

Author Comment

by:marcoullis
ID: 37841227
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:marcoullis
ID: 37841230
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
 

Author Comment

by:marcoullis
ID: 37841235
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
 

Author Comment

by:marcoullis
ID: 37841244
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
 
LVL 15

Accepted Solution

by:
Insoftservice earned 500 total points
ID: 37841308
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
 

Author Comment

by:marcoullis
ID: 37841322
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
 

Author Comment

by:marcoullis
ID: 37841329
nothing got inserted into the table.
0
 

Author Comment

by:marcoullis
ID: 37841343
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
 

Author Comment

by:marcoullis
ID: 37841345
got it to work, thanks :-)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now