2

The form being used to populate the database has to be kept up to date with the respective fields within the database.

Therefore a dynamic form field is required:

<table>
    <tr>
       <th>Intermediate</th>
       <th>Advanced</th>
       <th>No selection</th>
    </tr>
    <?php 
       $sth = $dbh->prepare("SELECT SkillName FROM Skill");
       $sth->execute();
       $tempCounter = 1;

       while($row = $sth->fetch()) {
        echo ("<tr>
                <td colspan=\"3\">
                    <label for=\"td".$tempCounter."\">".$tempCounter.". ".$row['SkillName']."</label>
                </td>
            </tr>");

        echo ("
        <tr>
            <td>
                <input type=\"radio\" name=\"".$row['SkillName']."\" id=\"tdA".$tempCounter."\" value=\"1\"/>
            </td>
            <td><input type=\"radio\" name=\"".$row['SkillName']."\" id=\"tdB".$tempCounter."\"  value=\"2\"/>
            </td>
            <td><input type=\"radio\"  name=\"".$row['SkillName']."\" id=\"tdC".$tempCounter."\" value=\"\" /></td>
        </tr>");
       $tempCounter++;
       }  
       ?>
 </table>

However, I don't know how to go about inserting data into the database when it is not clear what the data may be (specifically the SkillName)

In pseudocode it would be ("INSERT INTO PersonSkill (PersonID, SkillName, SkillValue) values ($personid, $skillname, $skillvalue)")

where $personid is already defined, $skillname is unknown at the time of writing the form, and $skillvalue is an integer.

So if the HTTP header information might look something like

 - [personid] => 101
 - [firstaid] => 2
 - [swimming] => 1
 - [mechanics] => 0
 - [firearms] =>
 - [athletics] => 2

How might I iterate over such values? I could always use the database itself to anticipate the values, but the final piece of the puzzle is eluding me...

while($row = $sth->fetch()) {
if (isset($_POST[''.$row['SkillName'].''])) 
// And er... save some variable at this point I suppose, or perhaps append to an array...
}
4
  • Is storing the skillname into a session for future use a viable option? Commented Jul 3, 2015 at 16:24
  • @Kepoly I could do that... but that would only be deferring the problem, surely? Commented Jul 3, 2015 at 16:25
  • How do you plan to insert? Button Click? Jquery? Forms Post? I assume post "isset($_POST", but just want to make sure. Commented Jul 3, 2015 at 16:32
  • @jmcclure ah yes; post it is (form backend) Commented Jul 3, 2015 at 16:35

2 Answers 2

2

Turn your skill inputs into an array:

<input type=\"radio\" name=\"skill[".$row['SkillName']."\]" id=\"tdA".$tempCounter."\" value=\"1\"/>

So you can build your query in the page it posts to (since I understood from reading comments on your question that you do this):

$query = '
    INSERT INTO
        PersonSkill 
    (
        "PersonID",
        "'.implode('","', array_keys($_POST['skill'])).'"
    )
    VALUES
    (
        '.$personid.',
        "'.implode('","', array_values($_POST['skill'])).'"
    )
';

It should output something like this, if you left firearms on an empty value and chose 2 for dance in the form:

string(195) "
    INSERT INTO
        PersonSkill 
    (
        "PersonID",
        "firearms","dance"
    )
    VALUES
    (
        69,
        "","2"
    )
"

However, if you don't want to post the empty values you can use array_filter to clean them out:

$skills = array_filter($_POST['skill']); 

$query = '
    INSERT INTO
        PersonSkill 
    (
        "PersonID",
        "'.implode('","', array_keys($skills)).'"
    )
    VALUES
    (
        '.$personid.',
        "'.implode('","', array_values($skills)).'"
    )
';

Unless I misunderstood you of course. Note: This is just an example. At least escape your variables.

Sign up to request clarification or add additional context in comments.

Comments

1

How about looping through the POST vars instead, and inserting them using $key => $value

foreach($_POST as $skillname => $skillvalue){

    $sql = "INSERT INTO PersonSkill (PersonID, SkillName, SkillValue) values $personid, $skillname, $skillvalue);";

}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.