MySQL:更改列中的值

时间:2015-03-08 17:37:45

标签: php sql undefined-index

标题几乎解释说实话。我正在尝试将我的删除列从0更改为1,具体取决于从我的列表播放器下拉菜单中选择的播放器。

我的listPlayers.php文件正在100%工作。球员们在下拉中表现得很好。 我应该补充说,错误发生在我的$ sql变量中。

<?php
include 'dbconnect.php';
$sql = "UPDATE players SET Delete = 1 WHERE playersID = $_POST[playersIDHidden]";

if(! mysql_query($sql, $conn))
{
    echo "Error " . mysql_error();
}

else
{
    if(mysql_affected_rows() != 0)
    {
        echo $_POST[pName] . " has been deleted <br>";
        echo "Person ID: " . $_POST ['playersID'] . "<br>";
        echo "Players Name: " . $_POST ['pName'] . "<br>";

        echo "Deletion complete";
    }

    else
    {
        echo "No records were changed";
    }
}

mysql_close($conn);
?>

<form action = "viewPlayers.php" method = "POST" />
    <input type = "submit" value = "View Players">
</form>

最后我的listPlayers.php在下拉列表中显示我的球员名字。

<?php
include "dbconnect.php"; //Opening Database Connection

$sql = "SELECT * FROM `players` WHERE `Delete` = 0;";

if (!$result = mysql_query($sql, $conn))
{
    die('Error in querying the database' . mysql_error());
}

echo "<br><select name = 'listPersons' id = 'listPersons' onclick = 'populate()'>";

while ($row = mysql_fetch_array($result))
{
    $id = $row['playersID'];
    $pName = $row['playersName'];
    $dob = $row['playersDateOfBirth'];
    $dob = date  ("d-m-Y", strtotime($dob));

    $allText = "$id, $pName, $dob";
    echo "<option value = '$allText'> $pName </option>";
}

echo "</select>";
mysql_close($conn);
?>

对不起相对较长的帖子。我通常能够修复99%的未定义错误。这个让我输了。

干杯!

1 个答案:

答案 0 :(得分:0)

使用已禁用的文本框为用户显示该文本。

使用其他隐藏字段来保存球员ID。

使用您的脚本填充它们,然后您可以在需要删除时使用隐藏字段中的值。

为了清楚起见,我向您展示了HTML的外观 - 这里是脚本:

<script>
    function populate()
    {
        var select = document.getElementById("listPersons"); 
        var result = select.options[select.selectedIndex].value;

        var personDetails = result.split(", ");
        document.getElementById("playersID_display").value = personDetails[0];
        document.getElementById("playersID").value = personDetails[0];
        document.getElementById("pName_display").value = personDetails[1];
        document.getElementById("pName").value = personDetails[1];
....

和html:

<form name = "myForm" action = "deletePlayer.php" method = "POST">    
    <p>
        <label for> Players ID:
            <input type = "text" name = "playersID_display" id = "playersID_display" disabled/>
            <input type = "hidden" name = "playersID" id = "playersID" />
        </label>
    </p>

    <p>
        <label for> Players Name:
            <input type = "text" name = "pName_display" id = "pName_display" disabled/>
            <input type = "hidden" name = "pName" id = "pName" />
        </label>
    </p>

    <p>
        <label for> Date of Birth:
            <input type = "date" name = "DoB_display" id = "DoB_display" disabled/>
            <input type = "hidden" name = "DoB" id = "DoB" />
        </label>
    </p>

    <input type = "Submit" value = "Delete" class = "button" onClick = "return confirm('Are you sure you want to delete this player?')"/>

</form>     

请注意,我为HTML输入元素的字段和唯一ID维护了唯一的名称。我也更喜欢type="hidden",而不是将hidden放在<input ... hidden />元素的末尾 - 我不熟悉该语法,所以我使用了我喜欢的方法。

显然,您的脚本必须单独更新这些字段中的每一个。