0

我需要创建一个下拉框,它从数据库中收集记录以填充下拉列表。When one of the values is selected, a form is to be displayed which contains the data relating to the value selected.

表单是具有显示选中数据的功能,同时也是在填写和提交时更新数据库中的记录。

我创建了一个 php 文件来尝试完成此操作,但我收到诸如未定义索引和未知列之类的错误。

我只是 PHP 新手,所以这对我来说是一项艰巨的任务。有人可以看看我的代码并告诉我是否有任何错误。

我一直试图从网上到处拼凑代码,但要让它全部工作起来很棘手。

经过一些调整,我现在没有收到任何错误,但记录不会更新。我收到“记录更新成功”消息,但记录未更新。

我很确定缺少记录更新归结为没有通过正确收集 ID$q=$row["BearId"]但如果我使用$q=$_GET["q"]我只会得到错误。我并不完全肯定这是问题所在,这就是我在这里提出问题的原因。

我将不胜感激您可以提供的任何帮助。我已经用这个东西到目前为止,但我不能让它更新记录。

编辑:我已将问题归结为 id

$sql = "UPDATE //snip WHERE BearId = '$q'";

$q=$row["BearId"];

如果我手动更改BearId为相等'1',则记录将更新。

更新熊.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Update Bears</title>
<script>
function showUser(str)
{
if (str=="")
{
    document.getElementById("result").innerHTML="";
    return;
} 
if (window.XMLHttpRequest)
{
    xmlhttp=new XMLHttpRequest();
}
else
{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("result").innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("GET","getvalues.php?q="+str,true);
xmlhttp.send();
}
</script>
<style>
// style elements
</style>
</head>
<body>
<h1>Update Bears</h1>
Select a Bear:
<br />
<select name="bears" onchange="showUser(this.value)">
<option value="">Select a BearId</option>
<?php
    $query = "SELECT * FROM bears";
    $mysqli = new mysqli('localhost','User','123','bears');
    $result = $mysqli->query($query);
    while($row = $result->fetch_assoc())
    echo '<option value="'.$row["BearId"].'">'.$row["BearId"].'</option>';
?>
</select>
<br />
<?php
$q=$row["BearId"];
$mysqli = new mysqli('localhost','User','123','bears');
$sql = "SELECT * FROM bears WHERE BearId='".$q."'";
if(array_key_exists('_submit_check', $_POST))
{
    $weight = $_POST['Weight'];
    $sex = $_POST['Sex'];
    $type = $_POST['Type'];
    $colour = $_POST['Colour'];
    $breed = $_POST['BreedId'];
    $sql = "UPDATE bears SET Weight = '$weight', Sex = '$sex', Type = '$type', Colour = '$colour', Breed = '$breed' WHERE BearId = '$q'";
    if($mysqli->query($sql) === TRUE)
    {
        echo 'Record updated successfully<br />';
    }
        else
    {
        echo $sql.'<br />' . $mysqli->error;
    }
    $mysqli->close();
}
?>
<br />
<div id="result"></div>
<br />
<a href="insertbear.php" class="Task2">Click here to Visit Task 2 (Insert Bears)</a> |     <a href="displaybears.php" class="Task3">Click here to Visit Task 3 (Display Bears)</a>
</body>
</html>

获取值.php

<?php
$q=$_GET["q"];
$mysqli = new mysqli('localhost','User','123','bears');
$sql = "SELECT * FROM bears WHERE BearId='".$q."'";
if($stmt = $mysqli->prepare($sql))
{
    $stmt->execute();
    $stmt->bind_result($BearId, $Weight, $Sex, $Type, $Colour, $Breed);
    while ($stmt->fetch())
    {
        echo "<form method='post' name='form1' onsubmit='return validateForm()' action='updatebears.php'>";
        echo "<p>";
        echo "<label for='BreedId'>BreedId:</label>";
        echo "<br />";
        echo "<select id='BreedId' name='BreedId' />";
        echo "<option value='".$Breed."'>".$Breed."</option>";
        echo "<option value='1'>1. Polar</option>";
        echo "<option value='2'>2. Brown</option>";
        echo "<option value='3'>3. Panda</option>";
        echo "</select>";
        echo "</p>";
        echo "<p>";
        echo "<label for='Weight'>Weight(kg):</label>";
        echo "<br />";
        echo "<input type='text' id='Weight' name='Weight' value='".$Weight."' />";
        echo "</label>";
        echo "</p>";
        echo "<p>";
        echo "Sex: ";
        echo "<br />";
        echo "<label for='M'>Male</label><input type='radio' id='M' value='M' name='Sex'";
        if($Sex=='M') echo "checked";
        echo "/>";
        echo "<label for='F'>Female</label><input type='radio' id='F' value='F' name='Sex'";
        if($Sex=='F') echo "checked";
        echo "/>";
        echo "</p>";
        echo "<p>";
        echo "<label for='Type'>Type:</label> ";
        echo "<br />";
        echo "<input type='text' id='Type' name='Type' maxlength='100' value='".$Type."' />";
        echo "</p>";
        echo "<p>";
        echo "<label for='Colour'>Colour:</label>";
        echo "<br />";
        echo "<input type='text' id='Colour' name='Colour' maxlength='20' value='".$Colour."' />";
        echo "</p>";
        echo "<p>";
        echo "<input type='submit' value='Submit' />";
        echo "<input type='reset' value='Reset' />";
        echo "<input type='hidden' name='_submit_check' value=1 />";
        echo "</p>";
        echo "</form>";
        }
    }
    else
    {
        echo 'Unable to connect';
        exit();
    }
?>

谢谢您的帮助。

4

1 回答 1

2

我相信您需要做的是在 getvalues.php 文件中为 BearId 创建一个隐藏的输入类型。这样,当您的表单执行帖子时,您可以从帖子中获取 BearId,而不是尝试从 $row['BearId'] 中获取它。我相当肯定 $row['BearId'] 与用户第一次进入 getvalues.php 表单时选择的 $row['BearId'] 不同。您是否尝试过将 $row['BearId'] 打印到 html 以验证它是合法值吗?

    if(array_key_exists('_submit_check', $_POST))
    {
    $id = $_POST['BearId']
    $weight = $_POST['Weight'];
    $sex = $_POST['Sex'];
    $type = $_POST['Type'];
    $colour = $_POST['Colour'];
    $breed = $_POST['BreedId'];
    $sql = "UPDATE bears SET Weight = '$weight', Sex = '$sex', Type = '$type', Colour = '$colour', Breed = '$breed' WHERE BearId = '$id'";
    if($mysqli->query($sql) === TRUE)
    {
        echo 'Record updated successfully<br />';
    }
        else
    {
        echo $sql.'<br />' . $mysqli->error;
    }
    $mysqli->close();
    }
    ?>


<h1>getvalues.php</h1>
    <?php
    $q=$_GET["q"];
    $mysqli = new mysqli('localhost','User','123','bears');
    $sql = "SELECT * FROM bears WHERE BearId='".$q."'";
    if($stmt = $mysqli->prepare($sql))
    {
        $stmt->execute();
        $stmt->bind_result($BearId, $Weight, $Sex, $Type, $Colour, $Breed);
        while ($stmt->fetch())
        {
            echo "<form method='post' name='form1' onsubmit='return validateForm()' action='updatebears.php'>";
            echo <input type="hidden" name="BearId" value='".$q."'>
            echo "<p>";
            echo "<label for='BreedId'>BreedId:</label>";
            echo "<br />";
            echo "<select id='BreedId' name='BreedId' />";
            echo "<option value='".$Breed."'>".$Breed."</option>";
            echo "<option value='1'>1. Polar</option>";
            echo "<option value='2'>2. Brown</option>";
            echo "<option value='3'>3. Panda</option>";
            echo "</select>";
            echo "</p>";
            echo "<p>";
            echo "<label for='Weight'>Weight(kg):</label>";
            echo "<br />";
            echo "<input type='text' id='Weight' name='Weight' value='".$Weight."' />";
            echo "</label>";
            echo "</p>";
            echo "<p>";
            echo "Sex: ";
            echo "<br />";
            echo "<label for='M'>Male</label><input type='radio' id='M' value='M' name='Sex'";
            if($Sex=='M') echo "checked";
            echo "/>";
            echo "<label for='F'>Female</label><input type='radio' id='F' value='F' name='Sex'";
            if($Sex=='F') echo "checked";
            echo "/>";
            echo "</p>";
            echo "<p>";
            echo "<label for='Type'>Type:</label> ";
            echo "<br />";
            echo "<input type='text' id='Type' name='Type' maxlength='100' value='".$Type."' />";
            echo "</p>";
            echo "<p>";
            echo "<label for='Colour'>Colour:</label>";
            echo "<br />";
            echo "<input type='text' id='Colour' name='Colour' maxlength='20' value='".$Colour."' />";
            echo "</p>";
            echo "<p>";
            echo "<input type='submit' value='Submit' />";
            echo "<input type='reset' value='Reset' />";
            echo "<input type='hidden' name='_submit_check' value=1 />";
            echo "</p>";
            echo "</form>";
            }
        }
        else
        {
            echo 'Unable to connect';
            exit();
        }
于 2013-06-13T04:57:14.970 回答