如何从多个字段对中获取数据

时间:2014-05-06 20:17:24

标签: php forms

所以我在表格中有6个场对(技能和技能等级)。代码中的代码分别是以下重复的6次:

<input type="text" name="skill[]" value="" class="field-1-2 float-left" placeholder="Skill"> 
<div class="select-container field-1-2 float-right">
<select name="skill-rating[]">
    <option value="">Skill rating</option>
    <option value="1">1 (Beginner)</option>
    <option value="2">2 (Amateur)</option>
    <option value="3">3 (Intermediate)</option>
    <option value="4">4 (Pro)</option>
    <option value="5">5 (Expert)</option>
</select>

从$ _POST返回此内容:

[skill] => Array
    (
        [0] => CSS3
        [1] => HTML5
        [2] => jQuery
        [3] => 
        [4] => 
        [5] => 
    )

[skill-rating] => Array
    (
        [0] => 4
        [1] => 2
        [2] => 4
        [3] => 0
        [4] => 0
    )

我想知道的是如何将数据插入数据库,同时确保对保持他们的关系(即技能[0]匹配[技能等级[0])

数据库架构:

+--------------+-------------+------+-----+---------+-------+
| Field        | Type        | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| skill_id     | int(11)     | NO   | PRI | NULL    |       |
| skill        | varchar(50) | NO   | PRI | NULL    |       |
| skill_rating | int(11)     | YES  |     | NULL    |       |
+--------------+-------------+------+-----+---------+-------+

PDO功能:

    public function updateSkills($skill, $skillRating = NULL, $sessionUserID) {

        $insertQuery = $this->db->prepare("
            INSERT INTO " . DB_NAME . ".freelancer_skills
                (skill_id, skill, skill_rating)
            VALUES 
                (:userID, :skill, :skillRating)
        ");

        $insertQuery->bindValue(":skill", $skill);
        $insertQuery->bindValue(":skillRating", $skillRating);
        $insertQuery->bindValue(":userID", $sessionUserID);

        try{
            $insertQuery->execute();
        }catch(PDOException $e) {
            $users = new Users($db);
            $debug = new Errors();
            $debug->errorView($users, $e);  
        }
    }

4 个答案:

答案 0 :(得分:1)

制作数组,然后$_POST['skill'][1]将匹配$_POST['skill-rating'][1]

<input type="text" name="skill[]">
<select name="skill-rating[]">

您还可以使用foreach()循环播放它们。

答案 1 :(得分:1)

您构建表单的方式,$_POST['skill-rating'][$key]的选定值将始终具有$_POST['skill'][$key]的等效值,因此使用您的函数,我相信以下内容应该可以正常工作:

foreach($_POST['skill'] as $key => $value) {
    updateSkills($_POST['skill'][$key], $_POST['skill-rating'][$key], $sessionUserID);
}

答案 2 :(得分:0)

假设用户填写了3种技能,即6种输入。您将在用户提交时获得以下array

Array(
[skill-1] => Python
[skill-rating-1] => 2
[skill-2] => PHP
[skill-rating-2] => 3
[skill-3] => Ruby
[skill-rating-3] => 5
)

现在使用并循环并创建一个数组,以便在查询中使用它。

if(isset($_POST))
{
    $skills = $_POST;
    $skill_count = sizeof($skills)/2;

    $data = array();
    $j = 0;
    foreach($i=1; $i<=$skill_count; $i++)
    {
        $data[$j]['skill'] = $skills['skill'.$i];
        $data[$j]['skill_rating'] = $skills['skill-rating'.$i];
        $j++;
    }

    echo '<pre>'; print_r($data);
}

产生的数组:

Array(
   [0] => Array(
               [skill] => Python
               [skill-rating] => 2
              )
   [1] => Array(
               [skill] => PHP
               [skill-rating] => 3
              )
   [2] => Array(
               [skill] => Ruby
               [skill-rating] => 5
              )
)

答案 3 :(得分:0)

这可能不是最有效的,也就是说,每次插入一行,但使用当前设置(使用PDO功能),它应该可以正常工作。

for ($i=0; $i<6; $i++) {

    if (isset($_POST['skill'][$i])) {

        $skill_name = sanitize_user_input($_POST['skill'][$i]);
        $skill_rating = (isset($_POST['skill-rating'][$i]) ? sanitize_user_input($_POST['skill-rating'][$i]) : NULL)

        if (!$YourClass->updateSkills($skill_name, $skill_rating, $sessionUserID))
            echo "Insert failed";
    }
}

请注意,sanitize_user_input()只是我的虚构功能,代表了某种输入卫生。