Solved

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

Posted on 2012-04-12
15
382 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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
 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Building a glossary into your website 9 57
w3c parsing errors 4 36
Save data in two Database, Asp 2 68
Securing source code... theoretical question 10 47
This article discusses four methods for overlaying images in a container on a web page
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

739 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