I want to insert a row in my db-table with inputs from users. The form the users fill out is made with a foreach-loop:

foreach ($results as $row) {
    echo '<div class="vm_kampe"><input type="hidden" name="kid['.$row->id.'" value="'.$row->id.'">'.$row->hhold.' - '.$row->uhold.'</div>
          <div class="vm_result"><input type="text" class="vm_resultat" name="resu['.$row->id.'" placeholder="?-?" value="'.$row->resu.'"></div>
          <div class="vm_1x2"><input type="text" class="vm_1x2" name="bet['.$row->id.'" placeholder="1X2" value="'.$row->bet.'"></div>';

On the receiving-end, I tried this, but doesn't work:

$jinput = JFactory::getApplication()->input;
$id = $jinput->get('kid');
$resu = $jinput->getString('resu');
$bet = $jinput->getString('bet');
$uid = $user->id;
$liga = 'bs';

foreach ($id as $key => $k) {
    $columns = array('id', 'uid', 'liga', 'resu', 'bet');
    $values = array($db->quote($k), $db->quote($uid), $db->quote($liga), $db->quote($resu[$key]), $db->quote($bet[$key]));
    $query->insert($db->quoteName('tip3_kuponer'))->columns($columns)->values(implode(',', $values));

It just say something about:

invalid argument in foreach....

  • $jinput->get('kid'); will no doubt be returning a single string, not an array. You cannot loop through a string.
    – Lodder
    Jul 3, 2018 at 11:26
  • So what should I do? Jul 3, 2018 at 11:34
  • Where is your array of data from the form?
    – Lodder
    Jul 3, 2018 at 11:54
  • I guess I don't have one. How should I write that? Jul 3, 2018 at 11:56
  • $data = JFactory::getApplication()->input->post->getArray(); Then loop through the $data array
    – Lodder
    Jul 3, 2018 at 11:59

1 Answer 1


First, you need to repair your damaged form html

// see the missing closing square bracket on each of your name attr ---↓-------------------↓-↓
                                                                       ↓                   ↓ ↓
echo '<div class="vm_kampe"><input type="hidden" name="kid['.$row->id.'←                   ↓ ↓
      <div class="vm_result"><input type="text" class="vm_resultat" name="resu['.$row->id.'← ↓
      <div class="vm_1x2"><input type="text" class="vm_1x2" name="bet['.$row->id.'←-----------

Because it is producing a one-dimensional $_GET (SUPERGLOBAL) array like:

$_GET = array(
    'kid[1' => '1',
    'resu[1' => 'somethingA',
    'bet[1' => 'somethingB',
    'kid[2' => '2',
    'resu[2' => 'somethingC',
    'bet[2' => 'somethingD',

but really, you want:

$_GET = array(
    'kid' => array('1' => '1', '2' => '2', etc.),
    'resu' => array('1' => 'somethingA', '2' => 'somethingC', etc.),
    'bet' => array('1' => 'somethingB', '2' => 'somethingD', etc.)

So I'll recommend this loop code block (using more lines to improve readability of my answer):

foreach ($results as $row) {
    echo '<div class="vm_kampe">';
        echo '<input type="hidden" name="kid[' , $row->id , ']" value="' , $row->id , '">';
        echo $row->hhold , ' - ' , $row->uhold;
    echo '</div>';
    echo '<div class="vm_result">';
        echo '<input type="text" class="vm_resultat" name="resu[' , $row->id , ']" placeholder="?-?" value="' , $row->resu , '">';
    echo '</div>';
    echo '<div class="vm_1x2">';
        echo '<input type="text" class="vm_1x2" name="bet[' , $row->id , ']" placeholder="1X2" value="' , $row->bet , '">';
    echo '</div>';
}  // remove the trailing semi-colon here

Second, change your submission method

Because you are delivering user-supplied data which will be saved to the database, POST is best/common practice versus GET.

Third, collect and process the multi-dimensional $_POST data

(I didn't test this advice)

$kids = JFactory::getApplication()->input->post->get('kid', [], 'array');
$resus = JFactory::getApplication()->input->post->get('resu', [], 'array');
$bets = JFactory::getApplication()->input->post->get('bet', [], 'array');

// just a general example of accessing the sets of data...
foreach ($kids as $id => $kid) {
    echo "For $id, kid is $kid, resu is {$resu[$id]}, bet is {$bet[$id]}<br>";

You should be able to integrate your query with the above.

When Joomla 4.0 rolls out, your INSERT query will be a perfect candidate for a single prepared statement with bound parameters, and iterated executions.

  • @Daniel I have answered both of your remaining two questions which do not have an accepted solution. If they solve your issues I would appreciate the awarding of the green tick. If either answer isn't quite right, or you would like some more explanation, please ask me. Please always endeavor to progress all questions to a system-recognised resolution. Jul 22, 2018 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.