I am trying to update multiple rows in a mysql db through php but I am getting an error message in this line:
$size = count($_POST['uid']);
My db field names are uid (which autoincrements and does not need to be updated), fname, lname and email fields. Here is a link for the project I need to develophttp://users.cis.fiu.edu/~vagelis/classes/COP5725/project.htm
Here is the content for the first page:
<!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>
<style type="text/css">
body {
margin:50px 0px; padding:0px;
text-align:center;
font:13px Tahoma,Geneva,sans-serif
}
#content {
width:1000px;
margin:0px auto;
text-align:left;
padding:15px;
border:1px dashed #333;
background-color:#eee;
}
</style>
<link rel="icon" href="http://www.mysite.com/favicon.ico" type="image/vnd.microsoft.icon" />
</head>
<body>
<div id='content'><h3><center>Update Information</center></h3>
<?php
//create a connection
$connection = mysql_connect("localhost","root","r2d2c3po");
//test the connection
if (!$connection)
{
die("Database connection failed: " . mysql_error());
}
//select database to use
mysql_select_db("music_social_network", $connection);
//setup query
$sql = "SELECT * FROM `user` ORDER BY uid";
//get results from query or die
$result = mysql_query($sql, $connection) or die(mysql_error());
//fetch rows from query
$rows = mysql_fetch_assoc($result);
?>
<?php
// find out how many records there are to update
$size = count($_POST['uid']);
// start a counter in order to number the input fields for each record
$i = 0;
print "<table width='100%' border='0' cellspacing='1' cellpadding='0'><tr><td>";
// open a form
print "<form name='namestoupdate' method='post' action='update.php'>
<table width='100%' border='0' cellspacing='1' cellpadding='1'><tr>
<td align='center'><strong>User ID</strong></td>
<td align='center'><strong>First Name</strong></td>
<td align='center'><strong>Last Name</strong></td>
<td align='center'><strong>Email</strong></td>
</tr>\n";
// start a loop to print all of the users with their information
// the mysql_fetch_array function puts each record into an array. each time it is called, it moves the array counter up until there are no more records left
while ($Update = mysql_fetch_array($result)) {
print "</tr>\n";
// assuming you have three important columns (the index (id), the course name (course), and the book info (bookinfo))
// start displaying the info; the most important part is to make the name an array (notice bookinfo[$i])
print "<td align='center'><p>{$Update['uid']}</p></td>\n";
print "<td align='center'><input type='text' fname='fname[$i]' value='{$Update['fname']}' /></td>";
print "<td align='center'><input type='text' size='40' lname='lname[$i]' value='{$Update['lname']}' /></td>\n";
print "<td align='center'><input type='text' size='40' email='email[$i]' value='{$Update['email']}' /></td>\n";
print "</tr>\n";
// add 1 to the count, close the loop, close the form, and the mysql connection
++$i;
}
print "<tr>
<td colspan='4' align='center'><input type='submit' value='submit' />";
print "</td>
</tr>
</table>
</td>
</tr>
</form>
</table>";
mysql_close($connection);
?><br /><br /><div></body>
</html>
here is the content for the update.php page:
<!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>
<style type="text/css">
body {
margin:50px 0px; padding:0px;
text-align:center;
font:13px Tahoma,Geneva,sans-serif
}
#content {
width:1000px;
margin:0px auto;
text-align:left;
padding:15px;
border:1px dashed #333;
background-color:#eee;
}
</style>
<link rel="icon" href="http://www.mysite.com/favicon.ico" type="image/vnd.microsoft.icon" />
</head>
<body>
<div id='content'><h3><center>Success! </center></h3>
<table width='100%' border='0' cellspacing='1' cellpadding='0'><tr><td>
<table width='100%' border='0' cellspacing='1' cellpadding='1'>
<tr>
<td align='center'><strong>User ID</strong></td>
<td align='center'><strong>First Name</strong></td>
<td align='center'><strong>Last Name</strong></td>
<td align='center'><strong>Email</strong></td>
</tr>
<?php
//create a connection
$connection = mysql_connect("localhost","root","r2d2c3po");
//test the connection
if (!$connection)
{
die("Database connection failed: " . mysql_error());
}
//select database to use
mysql_select_db("music_social_network", $connection);
//setup query
$sql = "SELECT * FROM `user` ORDER BY uid";
//get results from query or die
$result = mysql_query($sql, $connection) or die(mysql_error());
//fetch rows from query
$rows = mysql_fetch_assoc($result);
// find out how many records there are to update
$size = count($_POST['uid']);
// start a loop in order to update each record
$i = 0;
while ($i < $size) {
// define each variable
$uid = $_POST['uid'][$i];
$fname = $_POST['fname'][$i];
$lname = $_POST['lname'][$i];
$email = $_POST['email'][$i];
// do the update and print out some info just to provide some visual feedback
$query = "UPDATE `user` SET `fname` = '$fname', `lname` = '$lname', `email` = '$email' WHERE `uid` = '$uid' ";
mysql_query($sql) or die ("Error in query: $query");
print "
</tr>
<td align='left'><p>$uid</p></td>
<td align='left'>$fname</td>
<td align='left'>$fname</td>
<td align='left'>$lname</td>
</tr>
";
++$i;
}
mysql_close($connection);
?>
<tr>
<td colspan='4' align='center'>
</td>
</tr>
</table>
</td>
</tr>
</table></div></body></html>
Mod edit:
. . .
tags added.