如何创建一个方法来从DB表中指定字段名称和PK?

时间:2016-01-22 12:16:23

标签: php mysql

我有一系列复选框,可以通过3种方式与数据库进行交互; INSERTDELETE,无法采取行动。

我将总共有8-10个复选框,但每个复选框需要32行代码(包含3个语句)才能生效。而不是有10行复选框的320行代码可以使用PHP函数来定义这些元素的标记吗?

PHP代码:

//If the Java checkbox is checked it will check to see if java is already a skill assigned to the user. If so it will ignore, if not it will add.   
if (isset($_POST['java'])) {
    if (empty($_POST['javaexp'])) {
        //header('Location: UpdateAccount.php');
        //die();                    
    }
    else {
        $javaexp = $_POST['javaexp'];
    }

    $sql = $con->query("SELECT count(UserID) as total FROM userskills WHERE UserID = $User AND SkillID = 1")
        or die(mysqli_error($con));

    $row = mysqli_fetch_assoc($sql);

    if ($row ['total'] == "0") {

        $sql = $con->query("INSERT INTO userskills ( UserID, SkillID, Experience) VALUES  ($User, 1, $javaexp)");

        //If the Java checkbox is not checked it will check to see if java is already a skill assigned to the user. If they are it will delete it. If not it will ignore.   
    }
}
else {
    $sql = $con->query("SELECT count(UserID) as total FROM userskills WHERE UserID = $User AND SkillID = 1")
        or die(mysqli_error($con));

    $row = mysqli_fetch_assoc($sql);

    if ($row ['total'] == "1") {

        $sql = $con->query("DELETE FROM userskills 
                WHERE UserID = $User AND SkillID = 1");
    }
}

另一个php文件上的$ con = sql连接。

HTML代码:

<p>
    <label>
        <input type="checkbox" name="java" value="checkbox" id="CheckboxGroup1_0" class="skillselect">
        Java</label> <input type="number" name="javaexp" class="expnumber" placeholder="Enter Experience in years.">
    <br>
    <br>
    <label>
        <input type="checkbox" name="iOS" value="checkbox" id="CheckboxGroup1_1" class="skillselect">
        Checkbox</label> <input type="number" name="" class="expnumber" placeholder="Enter Experience in years.">
    <br>
    <br>
    <label>
        <input type="checkbox" name="PHP" value="checkbox" id="CheckboxGroup1_2" class="skillselect">
        Checkbox</label> <input type="number" name="" class="expnumber" placeholder="Enter Experience in years.">
    <br>
    <br>
</p>
<div class="FormElement">
    <input name="Update" type="submit" class="button" id="Update" value="Submit Changes">
</div>

1 个答案:

答案 0 :(得分:0)

你当然可以将其变成一个函数:

function updateSkillFromPost($skill, $skillId, $User, $con) {
    if (isset($_POST[$skill])) {
        if (empty($_POST["{$skill}exp"])) {
            //header('Location: UpdateAccount.php');
            //die();                    
        }
        else {
            $exp = $_POST["{$skill}exp"];
        }

        $sql = $con->query("SELECT count(UserID) as total FROM userskills WHERE UserID = $User AND SkillID = $skillId")
            or die(mysqli_error($con));

        $row = mysqli_fetch_assoc($sql);

        if ($row ['total'] == "0") {

            $sql = $con->query("INSERT INTO userskills ( UserID, SkillID, Experience) VALUES  ($User, $skillId, $exp)");

            //If the Java checkbox is not checked it will check to see if java is already a skill assigned to the user. If they are it will delete it. If not it will ignore.   
        }
    }
    else {
        $sql = $con->query("SELECT count(UserID) as total FROM userskills WHERE UserID = $User AND SkillID = $skillId")
            or die(mysqli_error($con));

        $row = mysqli_fetch_assoc($sql);

        if ($row ['total'] == "1") {

            $sql = $con->query("DELETE FROM userskills 
                    WHERE UserID = $User AND SkillID = $skillId");
        }
    }
}

然后将其称为

updateSkillFromPost('java', 1, $User, $con);
updateSkillFromPost('php', 2, $User, $con);
updateSkillFromPost('xml', 3, $User, $con);
... and so on ...

甚至更好,给定$skills形式的$id => $name数组:

foreach($skills as $skillId => $skill) {
  updateSkillFromPost($skill, $skillId, $User, $con);
}

请注意,您使用的代码以及我转换为函数的代码虽然容易受到SQL Injection的攻击。<​​/ p>