简体   繁体   English

SQL查询无法将新行插入表

[英]SQL query failing to INSERT new row into table

I have a chunk of PHP code that triggers if a button is pressed by the user. 我有一段PHP代码,如果用户按下了按钮,就会触发该代码。 What I expect to happen is it checks to see if the user has the skill already assigned to them, and if not it performs an INSERT. 我希望发生的事情是检查用户是否已经分配了技能,如果没有,它会执行INSERT。 Then if it does do nothing. 然后,如果它什么也不做。 And finally if the checkbox next to a skill is un-checked it checks to see if they have the skill already assigned and delete it if found. 最后,如果未选中某个技能旁边的复选框,它将检查他们是否已经分配了该技能,如果找到,则将其删除。

The code is deleting the skills from the user no matter the condition of the checkboxes. 无论复选框处于何种状态,该代码都将删除用户的技能。 Im sure I must be missing something but after starring at the code for hours I cannot see it. 我确定我一定会丢失一些东西,但是在看了几个小时的代码之后,我看不到它。

Can anyone suggest a resolution? 有人可以提出解决方案吗?

PHP code: 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 Code: 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>

Im not familiar with PHP, but yesterday I help in a similar problem 我不熟悉PHP,但是昨天我帮助解决了类似的问题

Here you create your query but your COUNT will always return a row, and if not skill the value is 0 在这里,您创建查询,但您的COUNT将始终返回一行,如果不熟练,则值为0

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

So instead of if ($row = mysqli_fetch_assoc($sql)) you need something like 因此, if ($row = mysqli_fetch_assoc($sql))您还需要

 $row = mysqli_fetch_assoc($sql);
 $skill = $row['total'];
 if ($skill == 0 )

But that doesnt solve the error you describe, delete all skills or just the one selected ? 但这不能解决您描述的错误, delete all skillsjust the one selected吗?

Any way you have to check this IF that is the branch sending your skill to delete . 您必须以任何方式检查此IF ,这是发送您要delete的技能的分支。

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

This mean your $skills_name isnt defined. 这意味着您的$skills_name未定义。 Maybe you should check for the value inside the array $skills_array ? 也许您应该检查$skills_array数组中的值?

My second guess check the code create on the webpage. 我的第二个猜测是检查在网页上创建的代码。 Right click your page and selece see source code 右键单击您的页面并选择see source code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM