dounei5721 2013-06-13 04:19
浏览 53
已采纳

MySQLi通过从PHP和SQL加载的表单更新记录。 数据库不会更新

I am required to create a drop down box which gathers the records from a database to populate the drop down. When one of the values is selected, a form is to be displayed which contains the data relating to the value selected.

The form is to have the function of showing the selected data, but also to update the record in the database when filled out and Submit.

I have created a php file to try and accomplish this, but Im getting errors such as undefined index and unknown column.

I am only new to PHP so this is a big task for me. Could someone have a look at my code and inform me if there are any errors.

Ive been trying to piece code together here and there from the net but its been tricky trying to get it all to work.

I am not getting any errors now after some tweaking, but the record wont update. I get a 'record updated successfully' message but the record isn't updated.

I am pretty sure the lack of a record update is coming down to the ID not getting collected properly via the $q=$row["BearId"] but if I use $q=$_GET["q"] I get nothing but errors. I am not completely positive this is the problem though, that is why Im asking the question here.

I would appreciate any help that you can give. Ive gotten so far with this thing and yet I cant get it to update the record.

EDIT: I have pinpointed the problem down to the id in

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

$q=$row["BearId"];

If I manually change BearId to equal '1' then the record is updated.

updatebears.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>

getvalues.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();
    }
?>

Thanks for the help.

  • 写回答

1条回答 默认 最新

  • douci1541 2013-06-13 04:57
    关注

    I believe what you need to do is create a hidden input type for the BearId within the getvalues.php file. That way when your form performs the post you can get the BearId from the post as opposed to trying to get it from the $row['BearId']. I'm fairly certain $row['BearId'] is not the same $row['BearId'] that the user selected when he first goes to the getvalues.php form. Have you tried printing $row['BearId'] to html to verify it's a legitimate value?

        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();
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题