我正在尝试使用下拉列表在“实时”表格中进行更新。我只会在内部使用这个网页。
我真的不知道很多javascript,所以我需要一些帮助。
我编写的脚本只获取第一行的值,因此不起作用......
这两个页面代码: HTML
<html>
<head>
<script>
function UpdateUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
var lastname = document.getElementById('lastname').innerHTML;
var firstname = document.getElementById('firstname').innerHTML;
var state = document.getElementById('state').innerHTML;
xmlhttp.open("GET","updateuser3.php?q="+str+"&lastname="+lastname+"&firstname="+firstname+"&state="+state,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
$con = mysql_connect('localhost','root','');
if (!$con)
{
die('Could not connect: ' . mysql_error($con));
}
mysql_select_db("test");
$sql="SELECT lastname, firstname, state FROM author";
$result = mysql_query($sql);
echo $sql;
echo "<br />";
echo "<table border='1'>
<tr>
<th>lastname</th>
<th>firstname</th>
<th>State</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td name='lastname' id='lastname'>" . $row['lastname'] . "</td>";
echo "<td name='firstname' id='firstname'>" . $row['firstname'] . "</td>";
echo "<td><select onchange='UpdateUser(this.value)'><option name='state' id='state' value='".$row['state']."'>".$row['state']."</option>";
if ($row['state']==='CA')
{
echo '<option value="OR">OR</option>';
echo '<option value="UT">UT</option>';
echo '<option value="MI">MI</option>';
echo '</select></td>';
}
else if ($row['state']==='OR')
{
echo '<option value="CA">CA</option>';
echo '<option value="UT">UT</option>';
echo '<option value="MI">MI</option>';
echo '</select></td>';
}
else
{
echo '<option value="CA">CA</option>';
echo '<option value="UT">UT</option>';
echo '<option value="MI">MI</option>';
echo '<option value="OR">OR</option>';
echo '</select></td>';
}
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<br>
<div id="txtHint"><b>State info will be listed here.</b></div>
</body>
</html>
PHP
<?php
$q=$_GET["q"];
$w=$_GET["lastname"];
$e=$_GET["firstname"];
$r=$_GET["state"];
echo "<br /> New State: ";
echo $q;
echo "<br /> Last Name: ";
echo $w;
echo "<br /> First Name: ";
echo $e;
echo "<br /> Old State: ";
echo $r;
echo "<br />";
$con = mysql_connect('localhost','root','');
if (!$con)
{
die('Could not connect: ' . mysql_error($con));
}
mysql_select_db("test");
$sql = "UPDATE author SET state = '$q' WHERE lastname = '$w' AND firstname = '$e' AND state = '$r'";
echo $sql;
echo "<br />";
mysql_close($con);
?>
有人可以帮忙吗? TY
答案 0 :(得分:0)
我会从查询开始进行故障排除,以查看您的查询是否返回多个结果。
在MySQL中运行SELECT lastname, firstname, state FROM author
查询。
如果此查询返回多个结果,那么您知道这是您的代码问题。
我使用phpMySQL来调试我的SQL。