views:

359

answers:

3

[Status: Learner]

I have a Table A with thousands of records. On each displayed page, only 20 rows are showing, with the following fields:

check
last_name 
first_name 
gr 
eth 
sex 
id_num (a unique value)
reason 
start_date

The "check" field indicates a checkbox that the user can tick. At the bottom of each page of 20 records is the select button. I want to take the unique "id_num" from the rows that are selected by the user from Table A and insert them into Table B. (Method is post.)

Checkbox input code:

$html = <input type='checkbox' name='checkbox[]'
                      id = '' value='<?PHP echo $aRecords [ \"id_num\" ]; ?>' />" ;

The SQL after the rows on a page are selected by the user:

include_once "db_login.php" ;
if ( isset ( $_POST [ checkbox] ) ) 
   { 
      foreach ( $_POST [ 'checkbox' ] as $checkbox )
         {
            $sql = "INSERT INTO sap_id_select ( select_id )
                    VALUES ( '<???>' ) " ;
            mysql_query ( $sql ) or ( "Error " . mysql_error () ) ;
         }
   }

Also tried, in addition to many other things:

foreach ( $_POST [ 'checkbox' ] as $checkbox )
         {
            $id_list = implode(',',$_POST['checkbox']);
            $sql = "INSERT INTO sap_id_select ( select_id )
                    VALUES ( '{$id_list}' ) " ;
         }

How can I get the id_num field into the $_POST array and then into Table B? Thanks for your help.

A: 

It looks like you have things set up OK. try

  $sql = "INSERT INTO sap_id_select ( select_id )
                VALUES ( '.$checkbox.' ) " ;

Maybe you can say if there are specific errors that you are getting.

Vincent Ramdhanie
Thanks, Vincent. Still no success. Interestingly, I'm getting no errors.
dave
Vincent, the id_num field is type CHAR(9). This is what is inserted:.<?PHP ec
dave
Look at the html source of your page in the browser, look at the checkbox code, what does the value look like? It should look like <input type='checkbox' name='checkbox[]' id = '' value='1' /> with relevant id values. If not then your php output is wrong.
Vincent Ramdhanie
A: 

Vincent -- I changed the html checkbox input code to

$html .= "<input type='checkbox' name='checkbox[]' id = ''
            value='$aRecords ['id_num']' />" ;

Assuming that this is correct (?), what would be my SQL INSERT statement to INSERT the unique record number (id_num) into Table B?? The following SQL, of course, doesn't work.

include_once "db_login.php" ;
if ( isset ( $_POST [ checkbox] ) ) 
   {  
      foreach ( $_POST [ 'checkbox' ] as $checkbox )
         {  
            $sql = "INSERT INTO sap_id_select ( selected_id )
                    VALUES ( '{ $_POST [ checkbox ]}'
                           ) " ;
            mysql_query ( $sql ) or ( "Error " . mysql_error () ) ;
         }
   }
dave
+1  A: 
 $html = <input type='checkbox' name='checkbox[]'
                  id = '' value='<?PHP echo $aRecords [ \"id_num\" ]; ?>' />" ;

You're missing the open quote aren't you? So the above doesn't give you an error that means your opening quote is above this line in other words, it's all messed up. Use an editor that colors your syntax.

Assuming you wanted to write

 $html = "<input type='checkbox' name='checkbox[]'
                  id = '' value='<?PHP echo $aRecords [ \"id_num\" ]; ?>' />" ;

then php will interpret the <?PHP bit litterally, which would explain what you're seeing. You want simply:

 $html = "<input type='checkbox' name='checkbox[]'
                  id = '' value='".$aRecords["id_num"]."' />" ;

Or is what you wrote is not in the php excuted part of your code then all I can suggest is to put <?php instead of <?PHP but I doubt very much it's what's causing problems. My bet is on the above.

Martin
Yes, Martin, you are correct. I sorta gave up on this, but now going back and correcting this, it is working. Thank you! I had assumed that it was an impenetrable SQL query problem.
dave