Jump to content

[SOLVED] insert array data only if check


TheJuan

Recommended Posts

hi

i have here a form which a result query from database. i would like to execute a script which insert to another table, those data with a check can only be inserted. my problem is that when i check the first 2, skip two data, and check the last data, i get the first 3 data. still the same when i check the 1st, skip 2nd,3rd,4th, check the last, i still get the first 2 data which is wrong. hope i make it clear.

 

here is the form

<form method='POST' action='add.php'>
<table border='0' width='100%' class='kylin'>
<tr>
    <th width='6%'>Add</td>
  </tr>
  <tr>
      <td>  <input type='text' readonly value='LSO13' name='xsocode[]'></td>
      <td width='3%'><input type='checkbox' name='check_block[]' value='check_block' checked></td>
      </tr>
  <tr>
      <td>  <input type='text' readonly value='LSO15' name='xsocode[]'></td>      
      <td width='3%'><input type='checkbox' name='check_block[]' value='check_block' checked></td>
      </tr>
  <tr>
      <td>  <input type='text' readonly value='LSO17' name='xsocode[]'></td>  
      <td width='3%'><input type='checkbox' name='check_block[]' value='check_block' checked></td>
      </tr>
  <tr>
      <td>  <input type='text' readonly value='LSO71' name='xsocode[]'></td>
      <td width='3%'><input type='checkbox' name='check_block[]' value='check_block' checked></td>
      </tr>
  <tr>
      <td>  <input type='text' readonly value='LSO16' name='xsocode[]'></td>
      <td width='3%'><input type='checkbox' name='check_block[]' value='check_block' checked></td>
      </tr>
  </table>
<input type='submit' value='Submit' name='submit'>
<input type='hidden' value='set'name='pasalog' />
<input type='hidden' value='04-05607' name='xstudentno'>
<input type='hidden' value='vincent' name='user'>
</p>
</form>

 

here is my code for add.php

$vsocode = $_POST['xsocode'];
$vstudentno = $_POST['xstudentno'];
$vuser = $_POST['user'];
$vdate = date('m-d-Y');
$vtime = date('h:i:s A');
$kylin = $_POST['check_block'];

if (isset($_POST['pasalog'])) {
      foreach ($kylin as $key => $value) {
            $query = "
            INSERT INTO subjects_enrolled_tbl SET studentno = '$vstudentno', subjectenrolled = '$vsocode[$key]', encoder = '$vuser', dateentered = '$vdate', timeentered = '$vtime';
            ";
            print $key . "-" . $query . "<br/>";
            }
      }

Link to comment
Share on other sites

Add a counter variable and specify the keys in the form names, so you get

 

<tr>
    <th width='6%'>Add</td>
  </tr>
  <tr>
      <td>  <input type='text' readonly value='LSO13' name='xsocode[1]'></td>
      <td width='3%'><input type='checkbox' name='check_block[1]' value='check_block' checked></td>
      </tr>
  <tr>
      <td>  <input type='text' readonly value='LSO15' name='xsocode[2]'></td>      
      <td width='3%'><input type='checkbox' name='check_block[2]' value='check_block' checked></td>
      </tr>

 

Only checked boxes get posted, so if 2 are unchecked the key values get out of synch

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.