1

Sorry I'm a bit of a noob when it comes to PHP but I just wondered if someone had an idea on how I could solve this PHP/SQL problem.

I have a PDO statement that gets all users from a database. With the array of users from the database I create a foreach loop to display all of the users in a table which I want to use to select a specific user, enter a number in the row of the user I select, then click submit and store the users name and also the number. I will use this information to populate another database later.

My question is, I cant seem to reference the user or the number in the table to extract the user and number I enter. When I try and request the numbered entered in the index.php, it will only ever display a number if I enter a number for a the final user in the table. When I try and view the FullName it never works and I get 'Undefined index: FullName' error. I also specified to 'POST in the form but it doesnt seem to be doing that. Does anyone have any ideas?

Thanks

//function.php

function getName($tableName, $conn)
{
    try {
        $result = $conn->query("SELECT * FROM $tableName");
                return ( $result->rowCount() > 0)
            ? $result
            : false;
    } catch(Exception $e) {
        return false;
    }
}

//form.php

<form action "index.php" method "POST" name='form1'>
  <table border="1" style="width:600px">
    <tr>
      <th>Name</th>
      <th>Number Entered</th>
    <tr/>
    <tr>
      <?php foreach($users as $user) : ?>
        <td width="30%" name="FullName">
          <?php echo $user['FullName']; ?>
        </td>
        <td width="30%">
          <input type="int" name="NumberedEntered">
        </td>
    </tr>
    <?php endforeach; ?>
  </table>
  <input type="submit" value="submit"></td>
</form>

//index.php

$users = getName('users', $conn);
if ( $_REQUEST['NumberedEntered']) {
    echo $_REQUEST['NumberedEntered'];
    echo $_REQUEST['FullName'];
}
Daniel
  • 3,541
  • 3
  • 33
  • 46
  • you should put the `` tag within the foreach loop, as you also close it within the loop. At the moment you just open one row, but close one for each user. – Daniel Aug 02 '14 at 11:42

1 Answers1

-1

The variable FullName isn't transmitted by your form to index.php. Only values of form elemnts are sent. You can add a hidden form field, that contains FullName like this:

<input type="hidden" name="FullName" value="<?php echo $user['FullName']">

But your second problem is, that your foreach loop will create several input fields with the exact same name. You won't be able to recieve any of the entered numbers, except the last one. have a look at this question for possible solutions.


Update

Putting each row in individual form tags should solve your problem:

<?php foreach($users as $user) : ?>
  <form action="index.php" method="POST">
    <tr>
      <td align="center" width="40%" >
        <?php echo $user['FullName']; ?>
        <input type="hidden" name="FullName" value="<?php echo $user['FullName']; ?>" />
      </td>
      <td width="30%">
        <input name="NumberedEntered"/>
      </td>
      <td>
        <input type="submit" value="submit"/>
      </td>
    </tr>
  </form>
<?php endforeach; ?>
Community
  • 1
  • 1
Daniel
  • 3,541
  • 3
  • 33
  • 46
  • I updated my answer. There was en `echo` missing in my proposed solution before. – Daniel Aug 02 '14 at 13:13
  • Have you tried using the solution from the update? You need those additional form tags. – Daniel Aug 02 '14 at 13:27
  • 1
    Hi Daniel, that's perfect! I can output the name and number!! Thanks for your help! – BenjaminDisco Aug 02 '14 at 13:41
  • You should always (when possible) use POSt in favor of GET, so why not simply write: `echo $_POST['NumberedEntered'];`? – Daniel Aug 02 '14 at 13:49
  • Why would you even need that if statement? Your form seems fine to me. Please open a new question ofr this problem, as it has not that much in common with the original question... I'll help you there – Daniel Aug 02 '14 at 14:21