Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts

    Inserting selected records using checkbox Getting ERROR MSG?

    Im Looking for a simple method of Inserting selected records
    into database using checkbox

    This is not a working example, but I hope it explains what I am trying to do.
    Not sure how to define If (checked = true) then do post all selected to db

    Code:
    <form name="PostSelected" method="post">
    
    Row1: <input type="checkbox" name="id[0]" value="" >
          <input type="text"     name="id[0]['FirstName']" value="Bob"  >
          <input type="text"     name="id[0]['LastName']"  value="Jones"><br> 
    
    Row2: <input type="checkbox" name="id[1]" value="" >
          <input type="text"     name="id[1]['FirstName']" value="John"  >
          <input type="text"     name="id[1]['LastName']"  value="Doe"><br> 
    
    Row3: <input type="checkbox" name="id[2]" value="" >
          <input type="text"     name="id[2]['FirstName']" value="Sandy"  >
          <input type="text"     name="id[2]['LastName']"  value="Beach"><br> 
    
    <input name="submit" type="submit"><br>
    </form>
    
    <?php
    // connect to db
    require_once("includes/connect.php");
    
    // find selected rows
     if($_POST[checkbox] == 'true') {
    
    // loop through selected rows
       foreach($_POST[id] as $post_key ){ 
    
    // post selected rows to db
         mysql_query("INSERT INTO businesses (FirstName, LastName) VALUES ('$post_key[FirstName]','$post_key[LastName]')") 
    
             or die("No Records Selected");
    }
    }  
    ?>
    Last edited by DataTalk; 12-14-2010 at 11:08 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,525
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    Are you generating your form dynamically using PHP?
    You show 3 names (or rows), but could there be any number of them?

    The names you are giving your text and checkboxes is very odd.
    I wonder if you can elaborate on what it is supposed to do (the purpose of your script).

    Sort of have a hunch you're approaching this from the wrong direction.

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Maybe this will better define my task

    1. I HAVE A LIST OF RECORDS WITH A CHECKBOX NEXT TO EACH RECORD TO SELECT FROM
    2. I WANT TO SELECT SOME OF THOSE RECORDS USING THE CHECKBOX
    3. AND INSERT THE SELECTED RECORDS INTO A NEW TABLE CALLED "TEST"

    Please follow my // notes through the script to see where I need help

    Code:
    <div align='left'>
    <form name"PostRecords" method="post">
    <table border='1' bordercolor='#COCOCO' cellpadding='0'>
      <tr>
        <td width="200"><p style='margin: 1'>SELECT A RECORD</td>
        <td width="50" ><p style='margin: 1'>id</td>
        <td width="200"><p style='margin: 1'>CostCode</td>
        <td width="300"><p style='margin: 1'>CostItem</td>
    </tr>
    <?php
    require_once("includes/connect.php");
    
    // First Lets List The Records for our selection
    
    $sql= "SELECT * FROM costitems ORDER BY CostCode";
      $result = mysql_query($sql);
        while($row = mysql_fetch_assoc($result)) {
          echo "<tr>
            <td><p style='margin: 1'><input type='checkbox' name='CB1' value=''>Select</td>
            <td><p style='margin: 1'>$row[id]</td>
            <td><p style='margin: 1'>$row[CostCode]</td>
            <td><p style='margin: 1'>$row[CostItem]</td>
         </tr>";
    }
    ?>
    </form>
    </table>
    </div>
    <!-- 1. ABOVE - WE NOW HAVE A LIST OF RECORDS TO SELECT FROM -->
    <!-- 2. NOW LETS SELECT SOME RECORDS FROM THE LIST ABOVE USING THE CHECKBOX -->
    <!-- 3. NOW LETS INSERT THOSE RECORDS INTO A NEW TABLE CALLED "TEST" -->
    
    <?php
    // This is where I need Help
    // IDENIFY THE ABOVE SELECTED RECORDS WITH IF STATEMENT
    
      if($_POST[checkbox] == 'true') { // need the correct code here
    
    // define the proper query to insert each record the database as a new record
       foreach($_POST[id] as $post_key ){ 
         mysql_query("INSERT INTO TEST (CostCode, CostItem) VALUES ('$post_key[CostCode]','$post_key[CostItem]')") 
    
             or die("No Records Selected");
    }
    echo "All the selected records have been inserted in TEST";
    } 
    ?>
    Last edited by DataTalk; 12-14-2010 at 06:48 PM.

  • #4
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by mlseim View Post
    Are you generating your form dynamically using PHP?
    You show 3 names (or rows), but could there be any number of them?

    The names you are giving your text and checkboxes is very odd.
    I wonder if you can elaborate on what it is supposed to do (the purpose of your script).

    Sort of have a hunch you're approaching this from the wrong direction.
    I was trying to layout a simple apptoach, I revised my code to reflect exactly what I am trying to do

    1. I have a list of records from db table with a checkbox next to each record
    2. I want to select some of those records and insert into a new table
    3. Im not sure how to identify each record that was selected for the insert query loop??

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,525
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    Checkbox arrays are somewhat different than text boxes.

    With a checkbox array, you'll only see the checkboxes that were "checked".
    Example, if you have 100 lines in your form, and the user checks 5 of them,
    the checkbox array will only have 5 elements in it, numbered 0,1,2,3,4.

    Yet, with text boxes and hidden tags, you'll see every one of them in the array.

    So, we need to give the checkbox a value of it's position in the array.
    That position will point to the correct location for the text box or hidden tag value.

    My test script ... but this is untested. I'm not sure what will happen ...
    PHP Code:
    <div align='left'>
    <form name"PostRecords" method="post">
    <table border='1' bordercolor='#COCOCO' cellpadding='0'>
      <tr>
        <td width="200"><p style='margin: 1'>SELECT A RECORD</td>
        <td width="50" ><p style='margin: 1'>id</td>
        <td width="200"><p style='margin: 1'>CostCode</td>
        <td width="300"><p style='margin: 1'>CostItem</td>
    </tr>
    <?php
    require_once("includes/connect.php");

    // First Lets List The Records for our selection

    $line=0;
    $sql"SELECT * FROM costitems ORDER BY CostCode";
      
    $result mysql_query($sql);
        while(
    $row mysql_fetch_assoc($result)) {
          echo 
    "<tr>
            <td><p style='margin: 1'><input type='checkbox' name='cb[]' value='$line'>Select</td>
            <input type='hidden' name='rid[]' value='$row[id]'>
            <input type='hidden' name='cc[]' value='$row[CostCode]'>
            <input type='hidden' name='ci[]' value='$row[CostItem]'>
            <td><p style='margin: 1'>$row[id]</td>
            <td><p style='margin: 1'>$row[CostCode]</td>
            <td><p style='margin: 1'>$row[CostItem]</td>
         </tr>"
    ;
         
    $line++;
    }
    ?>
    </form>
    </table>
    </div>
    <!-- 1. ABOVE - WE NOW HAVE A LIST OF RECORDS TO SELECT FROM -->
    <!-- 2. NOW LETS SELECT SOME RECORDS FROM THE LIST ABOVE USING THE CHECKBOX -->
    <!-- 3. NOW LETS INSERT THOSE RECORDS INTO A NEW TABLE CALLED "TEST" -->

    <?php
    // by looping through the checkboxes, you will only see the ones that were checked.
    // the value of those checkboxes will be associated with the proper line from your form.
    // i'm not sure what you want to do with $row[id]?  Perhaps you don't need it, your new table will assign new ID?

    // define the proper query to insert each record the database as a new record
       
    foreach($_POST['cb'] as $line_no ){ 
         
    mysql_query("INSERT INTO TEST (CostCode, CostItem) VALUES ('$_POST[cc][$line_no]','$_POST[ci][$line_no]')"

             or die(
    "No Records Selected");
    }

    echo 
    "All the selected records have been inserted in TEST";
    ?>

  • #6
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    REVISED CODE!

    Were close, here is what hit the database selecting 5 random records
    0 Array[0] Array[0] 0 NULL NULL NULL
    0 Array[1] Array[1] 0 NULL NULL NULL
    0 Array[2] Array[2] 0 NULL NULL NULL
    0 Array[3] Array[3] 0 NULL NULL NULL
    0 Array[4] Array[4] 0 NULL NULL NULL

    Code:
    <div align='left'>
    <form method="post">
    <p><input type="submit" value="Submit"></p>
    <table border='1' bordercolor='#COCOCO' cellpadding='0'>
      <tr>
        <td width="200"><p style='margin: 1'>SELECT A RECORD</td>
        <td width="50" ><p style='margin: 1'>id</td>
        <td width="200"><p style='margin: 1'>CostCode</td>
        <td width="300"><p style='margin: 1'>CostItem</td>
    </tr>
    <?php
    require_once("includes/connect.php");
    
    $line=0;
    $sql= "SELECT * FROM costitems ORDER BY CostCode";
      $result = mysql_query($sql);
        while($row = mysql_fetch_assoc($result)) {
          echo "<tr>
            <td><input type='checkbox' name='F1[]' value='$line'></td>
            <td><input type='text'     name='F2[]' value='$row[id]'></td>
            <td><input type='text'     name='F3[]' value='$row[CostCode]'></td>
            <td><input type='text'     name='F4[]' value='$row[CostItem]'></td>
         </tr>";
         $line++;
    }
    ?>
    </form>
    </table>
    </div>
    
    <?php
       foreach($_POST['F1'] as $line_no ){ 
         mysql_query("INSERT INTO TEST (CostCode, CostItem) VALUES ('$_POST[F3][$line_no]','$_POST[F4][$line_no]')") ;
    }
    ?>

  • #7
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by DataTalk View Post
    REVISED CODE!

    Were close, here is what hit the database selecting 5 random records
    0 Array[0] Array[0] 0 NULL NULL NULL
    0 Array[1] Array[1] 0 NULL NULL NULL
    0 Array[2] Array[2] 0 NULL NULL NULL
    0 Array[3] Array[3] 0 NULL NULL NULL
    0 Array[4] Array[4] 0 NULL NULL NULL

    Code:
    <div align='left'>
    <form method="post">
    <p><input type="submit" value="Submit"></p>
    <table border='1' bordercolor='#COCOCO' cellpadding='0'>
      <tr>
        <td width="200"><p style='margin: 1'>SELECT A RECORD</td>
        <td width="50" ><p style='margin: 1'>id</td>
        <td width="200"><p style='margin: 1'>CostCode</td>
        <td width="300"><p style='margin: 1'>CostItem</td>
    </tr>
    <?php
    require_once("includes/connect.php");
    
    $line=0;
    $sql= "SELECT * FROM costitems ORDER BY CostCode";
      $result = mysql_query($sql);
        while($row = mysql_fetch_assoc($result)) {
          echo "<tr>
            <td><input type='checkbox' name='F1[]' value='$line'></td>
            <td><input type='text'     name='F2[]' value='$row[id]'></td>
            <td><input type='text'     name='F3[]' value='$row[CostCode]'></td>
            <td><input type='text'     name='F4[]' value='$row[CostItem]'></td>
         </tr>";
         $line++;
    }
    ?>
    </form>
    </table>
    </div>
    
    <?php
       foreach($_POST['F1'] as $line_no ){ 
         mysql_query("INSERT INTO TEST (CostCode, CostItem) VALUES ('$_POST[F3][$line_no]','$_POST[F4][$line_no]')") ;
    }
    ?>
    PS: The list has about 500 records, loads very slow....

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,525
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    The slow loading is because you are using <table>.
    I'll bet you're using IE too.

    IE won't display anything until the <table> is complete.

    Eliminate <table> and use CSS instead, or try it on FireFox .... it will load quicker.


    ===============

    My bad! I'm referencing these wrong ... $_POST[F3][$line_no]

    I think we need to make arrays out of them first ...

    My revised code:
    PHP Code:
    <div align='left'>
    <form method="post">
    <p><input type="submit" value="Submit"></p>
    <table border='1' bordercolor='#COCOCO' cellpadding='0'>
      <tr>
        <td width="200"><p style='margin: 1'>SELECT A RECORD</td>
        <td width="50" ><p style='margin: 1'>id</td>
        <td width="200"><p style='margin: 1'>CostCode</td>
        <td width="300"><p style='margin: 1'>CostItem</td>
    </tr>
    <?php
    require_once("includes/connect.php");

    $line=0;
    $sql"SELECT * FROM costitems ORDER BY CostCode";
      
    $result mysql_query($sql);
        while(
    $row mysql_fetch_assoc($result)) {
          echo 
    "<tr>
            <td><input type='checkbox' name='F1[]' value='$line'></td>
            <td><input type='text'     name='F2[]' value='$row[id]'></td>
            <td><input type='text'     name='F3[]' value='$row[CostCode]'></td>
            <td><input type='text'     name='F4[]' value='$row[CostItem]'></td>
         </tr>"
    ;
         
    $line++;
    }
    ?>
    </form>
    </table>
    </div>

    <?php
    $ccode
    =$_POST['F3'];
    $citem=$_POST['F4'];
       foreach(
    $_POST['F1'] as $line_no ){ 
         
    mysql_query("INSERT INTO TEST (CostCode, CostItem) VALUES ('$ccode[$line_no]','$citem[$line_no]')") ;
    }
    Last edited by mlseim; 12-14-2010 at 08:57 PM.

  • #9
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Thank you very much for your help! here is an update..

    I'm getting an error msg;
    Warning: Invalid argument supplied for foreach()

    I added a quick echo "results" to see what all is posting
    The script is working properly, the correct data is posted clean to both..
    Database and echo out. Can't find the bug causing the error


    Could it be $_POST['F3']; vs $_POST[F3] i get the error either way
    something in: foreach($_POST['F1'] as $line_no ){


    PS: Also, I fixed the speed issue by using hidden fields and not displaying
    the data in the textbox in the selection list - see the revised table format.

    Code:
    <div align='left'>
    <form method="post" target='_self'>
    <p><input type="submit" value="Submit"></p>
    <table border='1' bordercolor='#COCOCO' cellpadding='0'>
      <tr>
        <td width="200"><p style='margin: 1'>SELECT A RECORD</td>
        <td width="50" ><p style='margin: 1'>id</td>
        <td width="200"><p style='margin: 1'>CostCode</td>
        <td width="300"><p style='margin: 1'>CostItem</td>
    </tr>
    <?php
    require_once("includes/connect.php");
    
    $line=0;
    $sql= "SELECT * FROM costitems WHERE CostId = '10' ORDER BY CostCode";
      $result = mysql_query($sql);
        while($row = mysql_fetch_assoc($result)) {
          echo "<tr>
            <td>
                <input type='checkbox' name='F1[]' value='$line'>
                <input type='hidden'   name='F2[]' value='$row[id]'>
                <input type='hidden'   name='F3[]' value='$row[CostCode]'>
                <input type='hidden'   name='F4[]' value='$row[CostItem]'>  
            </td>
                <td>$row[id]      </td>
                <td>$row[CostCode]</td>
                <td>$row[CostItem]</td>
            </tr>";
         $line++;
    }
    ?>
    </form>
    </table>
    </div>
    <?php
    $username = "test";
    $costcode = $_POST['F3'];
    $costitem = $_POST['F4'];
      foreach($_POST['F1'] as $line_no ){ 
        mysql_query("INSERT INTO test (CostCode, CostItem, UserName) VALUES ('$costcode[$line_no]','$costitem[$line_no]','$username')") ;
    }
    
    if ($_POST) {
    $user = $_POST[user];
    $sql2= "SELECT * FROM test WHERE UserName = 'test' ORDER BY CostCode";
      $result2 = mysql_query($sql2);
        while($row2 = mysql_fetch_assoc($result2)) {
          echo "TEST DATAENTRY RESULTS: $row2[CostCode], $row2[CostItem]<br>";
    }} 
    ?>
    Last edited by DataTalk; 12-14-2010 at 11:05 PM.

  • #10
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Need help resolving the error mesage in the above example

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,525
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    hmmm .... try this:
    PHP Code:

    <?php
    $username 
    "test";
    $costcode $_POST['F3'];
    $costitem $_POST['F4'];
    $chkbox $_POST['F1'];
      foreach(
    $chkbox as $line_no ){ 
        
    mysql_query("INSERT INTO test (CostCode, CostItem, UserName) VALUES ('$costcode[$line_no]','$costitem[$line_no]','$username')") ;
    }

    if (
    $_POST) {
    $user $_POST[user];
    $sql2"SELECT * FROM test WHERE UserName = 'test' ORDER BY CostCode";
      
    $result2 mysql_query($sql2);
        while(
    $row2 mysql_fetch_assoc($result2)) {
          echo 
    "TEST DATAENTRY RESULTS: $row2[CostCode], $row2[CostItem]<br>";
    }} 
    ?>

  • #12
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Sorry! no-go-bro;

    Still get the error??

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,525
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    I'm stumped.

  • #14
    Regular Coder
    Join Date
    Nov 2010
    Location
    Oregon
    Posts
    243
    Thanks
    23
    Thanked 10 Times in 10 Posts
    I posted a working example of this script

    http://quickweboffice.com/weblink.php?id=22

    The script works great! how do we solve the for each error msg?
    worst case - how could we write a custom error msg for the foreach
    function and leave it blank

  • #15
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,525
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    doh!

    That's a warning, not a fatal failure ... why didn't I see that before?

    change this:

    foreach($chkbox as $line_no ){

    to this:

    @foreach($chkbox as $line_no ){

    See if that stops the warning.
    Otherwise, put this at the top of your script:

    <?php
    error_reporting (E_ALL ^ E_NOTICE);


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •