在下面的php代码中,如果在顶部输入框中键入内容并单击按钮,则会更新mysql数据库。我想对下面有多个框的表格做同样的事情(实际表格将有两个以上)。
<?php
?>
<html>
<head>
<script language='javascript'>
function showList(){
var i=0;
var rankNum;
rankNum = 1;
var vfundsym;
while (i<=5)
{
alert(rankNum + " " + document.getElementById("o" + (i+1)).value);
i++;
rankNum++;
}
}
</script>
</head>
<body>
<form action="getuser3.php" method="post">
<input type="text" id='fundAge' name="fundAge" size="7" maxlength="7" />
<input style="visibility: visible" id="addFundtoDB" type="submit" value="Update Database" />
</form>
<p>The form above updates one record in the database. How can I loop through the form below and update each record?</p>
<form name="orderedlist" action="getuser3.php" method="post">
<form>
<table>
<tr><td><textarea class="orderedlist" name="p1" id="o1"></textarea></td></tr>
<tr><td><textarea class="orderedlist" name="p2" id="o2"></textarea></td></tr>
</table>
<input type="button" value="run SHOWLIST function" onclick="showList()">
</form>
</body>
</html>
getuser3.php代码如下。
<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("devtechw_ajax_demo", $con);
mysql_query("UPDATE user SET Age=('$_POST[fundAge]')
WHERE ID='2'");
mysql_close($con);
?>
答案 0 :(得分:2)
忽略你所拥有的大开的SQL注入漏洞,你可以在一条记录中更新多个字段,如下所示:
UPDATE sometable
SET field1=value1, field2=value2, field3=etc...
WHERE ...
答案 1 :(得分:0)
您可以将SQL查询更改为
$sql="UPDATE user SET Age='".$_POST[fundAge]."' WHERE ID='2'";
mysql_query($sql);
我想这可以解决问题