我需要創建一個下拉框,它收集數據庫中的記錄以填充下拉列表。當選擇其中一個值時,將顯示一個表格,其中包含與所選值相關的數據。MySQLi通過從PHP和SQL加載的表單更新記錄。數據庫不會更新
該表格具有顯示選定數據的功能,還可以在填寫和提交時更新數據庫中的記錄。
我已經創建了一個php文件來嘗試和完成這個,但我得到錯誤,如未定義的索引和未知的列。
我只是PHP新手,所以這對我來說是一個很大的任務。有人可以看看我的代碼,並告訴我是否有任何錯誤。
我一直試圖將代碼拼湊在這裏和那裏從網絡,但它很難試圖讓這一切工作。
經過一些調整後,我沒有收到任何錯誤,但記錄不會更新。我得到'記錄更新成功'的消息,但記錄沒有更新。
我很確定缺少記錄更新是通過$q=$row["BearId"]
沒有正確收集的ID,但如果我使用$q=$_GET["q"]
我什麼也得不到,但只有錯誤。我不完全肯定這是問題,但這就是爲什麼我在這裏問這個問題。
我將不勝感激任何幫助,你可以給。我已經得到了這個東西,但我不能得到它來更新記錄。
編輯:我曾在
$sql = "UPDATE //snip WHERE BearId = '$q'";
$q=$row["BearId"];
精確定位問題下降到ID如果我手動更改BearId
等於'1'
然後記錄被更新。
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();
}
?>
感謝您的幫助。
這是完美的。完全是我的問題,並以這種完美的方式解釋。我無法對此表示感謝。它現在非常有意義,但它不是我想到的。這是我應該一直在檢索的一件清楚的事情,但我試圖從javascript函數中檢索它,而不是像其他所有形式一樣。你爲我節省了很多麻煩。非常感謝。 – Curley5959