[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2012-04-12
15
Medium Priority
?
386 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

650 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