user1218122
user1218122

Reputation: 80

update multiple records in mysql database from php

In the php code below, if you type something into the top input box and click the button the mysql database is updated. I would like to do the same thing with the form below that has multiple boxes (the actual form will have more than two).

<?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>

The getuser3.php code is below.

<?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);
?>

Upvotes: 0

Views: 1500

Answers (2)

Taco
Taco

Reputation: 317

You can change the SQL query as

$sql="UPDATE user SET Age='".$_POST[fundAge]."' WHERE ID='2'";
mysql_query($sql);


I guess this will do the trick

Upvotes: 0

Marc B
Marc B

Reputation: 360672

Neglecting the gaping wide-open SQL injection hole you've got, you can update multiple fields in a single record like this:

UPDATE sometable
SET field1=value1, field2=value2, field3=etc...
WHERE ...

Upvotes: 2

Related Questions