SQL查询无法将新行插入表中

时间:2016-02-06 11:05:04

标签: php mysql

如果用户按下某个按钮,我会触发一大块PHP代码。我期望发生的是它检查用户是否具有已分配给他们的技能,如果不是,则执行INSERT。然后,如果它什么都不做。最后,如果未选中某项技能旁边的复选框,则会检查他们是否已分配该技能,如果找到则将其删除。

无论复选框的条件如何,代码都会删除用户的技能。我确定我必须遗漏一些东西,但在代码主演几个小时之后,我看不到它。

任何人都可以建议解决方案吗?

PHP代码:

if(isset($_POST['Update']))
    {

        $default = 0;

            foreach($skills_array AS $skills_id=>$skills_name)
            {
                if (isset($_POST[$skills_name]))
                {
                    if (empty($_POST[$skills_name.'exp']))
                    {
                        $exp = $default;
                    }
                    else
                    {
                        $exp = $_POST[$skills_name.'exp'];
                    }

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

                    if ($row = mysqli_fetch_assoc($sql))
                    {
                        $sql = $con->query("INSERT INTO `userskills` ( `UserID`, `SkillID`, `Experience`) VALUES  ('$User', '$skills_id', '$exp')")
                        or die(mysqli_error($con));
                        //If the checkbox is not checked it will check to see if skill is already a skill assigned to the user. If they are it will delete it. If not it will ignore.   
                    }
                    else
                    {
                        $sql = $con->query("UPDATE `userskills` SET `Experience` = '$exp' WHERE `UserID` = '$User' AND `SkillID` = ".$skills_id)
                        or die(mysqli_error($con));
                    }
                } 
                else
                {
                    $sql = $con->query("DELETE FROM `userskills` WHERE `UserID` = '$User' AND `SkillID` = ".$skills_id)
                    or die(mysqli_error($con));
                }
            }

            header('Location: Account.php');
            die();
        }
        else
        {
            echo 'Incorrect password please try again.';
        }
    }

HTML代码:

<div class="RightBody">
            <form id="form2" name="form2" method="post" enctype="multipart/form-data">
                    <p><h3>Skills:</h3>
    <?php

    $result1 = $con->query("SELECT skills.`SkillID`, skills.`Description`, COUNT(userskills.`SkillID`) AS SkillUserHas, MAX(`Experience`) AS Experience
                            FROM `skills`
                            LEFT OUTER JOIN userskills
                            ON skills.`SkillID` = userskills.`SkillID` AND userskills.`UserID` = '$User'
                            GROUP BY skills.`SkillID`, skills.`Description`
                            ORDER BY FIELD(skills.`SkillID`, 1, 7, 9, 3, 4, 5, 6, 8)") 
                            or die(mysqli_error($con));



    while ($skillrow = $result1->fetch_assoc()) 
    {
    ?>
                    <div class="CheckboxText">
                    <?php
                        echo '<label>';
                        echo '<input type="checkbox" name="'.$skillrow['Description'].'" id="CheckboxGroup1_'.$skillrow['SkillID'].'" class="skillselect" value="yes" '.(($skillrow['SkillUserHas'] > 0) ? 'checked' : '').'>';
                        echo $skillrow['Description'].'</label>';
                        echo '<input type="number" name="'.$skillrow['Description'].'exp" class="expnumber" placeholder="Enter Experience in years." value="'.$skillrow['Experience'].'">';
                        echo '<br />';
                        echo '<br />';

                     } 
                     ?>
                    </div>
                    </p>
            </form>
    </div>

1 个答案:

答案 0 :(得分:0)

我不熟悉PHP,但昨天我帮助了类似的 problem

在这里您创建了查询,但您的COUNT将始终返回一行,如果不是技巧,则值为0

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

所以代替if ($row = mysqli_fetch_assoc($sql))你需要像

这样的东西
 $row = mysqli_fetch_assoc($sql);
 $skill = $row['total'];
 if ($skill == 0 )

但这并不能解决您描述的错误,delete all skillsjust the one selected

您必须检查此IF这是将您的技能发送到delete的分支。

 if (isset($_POST[$skills_name]))

这意味着您的$skills_name未定义。也许您应该检查数组$skills_array中的值?

我的第二次猜测检查网页上创建的代码。右键单击您的页面并选择see source code