Page 1 of 1

How can we edit multiple records and update

Posted: Thu Aug 04, 2005 4:21 am
by mysteryminds
well to make it simpler for you the code below will give you the insigh

Code: Select all

$result = mysql_query("SELECT * FROM $table WHERE surname = '$surname' and add1 = '$add1' ORDER BY add1",$db);


$new = stripslashes($area);
$new2 = htmlspecialchars("$new", ENT_QUOTES);
$new1 = stripslashes($new2);
$rn = mysql_numrows($result);

echo "Total Number of Records under $surname ($rn)<br>";
echo "<a href=/ab/>home</a> &raquo; <a href='name-view.php?area=$new1'>$new1<a> &raquo; <a href='name-detail-view.php?surname=$surname&area=$new1'>$surname</a> &raquo; $add1<br>";
echo " <form method=post action='xyz.php'>";
echo "<table cellpadding=2 cellspacing=1 border=0 width=100%>"; 
echo "<tr class=hdr><td>Name</td><td>Address</td><td>Gender</td><td>Religion<br>Sub</td><td>Family</td><td>Tel-Mob-Fax</td><td>Email</td></tr>";


$alternate = "2"; 
while ($row = mysql_fetch_array($result)) { 
$id = $row["id"]; 
$surname = $row["surname"];
$forename = $row["forename"];
$mobile = $row["mobile"];
$email = $row["email"];
$gender = $row["gender"];
$religion = $row["religion"];
$sub_rel = $row["sub_rel"];
$family = $row["family"];
$tel = $row["tel"];
$fax = $row["fax"];
$add1 = $row["add1"];
$area = $row["area"];
$district = $row["district"];
$b = mysql_numrows($result);


if ($alternate == "1") { 
$color = "#ffffff"; 
$alternate = "2"; 
} 
else { 
$color = "#efefef"; 
$alternate = "1"; 
} 

echo"<input type=hidden name=b value='$b'/><input type=hidden name=id value='$id'/><tr><tr bgcolor=$color><td nowrap valign=top><input type=text name=surname size=20 value='$surname'/><br><input type=text name=forename size=20 value='$forename'/><br></td>
<td nowrap valign=top><input type=text name=add1 size=40 value='$add1'/><br><input type=text name=area size=40 value='$area'/><br><input type=text name=district value='$district'/></td><td valign=top><input type=text name=gender size=10 value='$gender'/></td><td valign=top><input type=text name=religion size=10 value='$religion'/><br><input type=text name=sub_rel size=10 value='$sub_rel'/></td><td valign=top><input type=text name=family size=15 value='$surname'/></td><td valign=top nowrap>T :<input type=text name=tel size=10 value='$tel'/><br>M :<input type=text name=mobile size=10 value='$mobile'/> <br>F :<input type=text name=fax size=10 value='$fax'/> </td><td valign=top><input type=text name=email value='$email'/></td></tr>";
} 
//echo "<tr><td colspan=5><a href='edit-rec.php?'>UPDATE $table SET religion = 'muslim', sub_rel = 'muslim', family = '$surname' WHERE surname ='$surname' AND add1='$add1' </td></tr>";
echo "</table>";
echo "<input type=Submit name=update value='Update information'>";
here i'm able to edit all the records, now the bigger problem is that when i post only the last record is posting to the page. How can i post all the records to update page

Posted: Thu Aug 04, 2005 4:43 am
by s.dot
include your line of 'echo'd HTML (last line) in the while loop. So when it iterates through the loop, it will post a new row on each iteration.

Edit: Please use the PHP /PHP tags for posting PHP code. It makes it easier to read. :P

Posted: Thu Aug 04, 2005 5:27 am
by timvw
$rn = mysql_numrows($result); missing a _ between num and rows..

I would give the user checkboxes to choose which records he wants... When the form posted, you only need to iterate through the selections...

Posted: Thu Aug 04, 2005 7:27 am
by mysteryminds
could you please explain how can i do that.

Well about $rn thats no problem while pasting the code i forgot to put it properly.

I mainy would like to have some kinda sample so that I can work on that

Posted: Thu Aug 04, 2005 8:44 pm
by timvw
Here is a demo. Currently the source is a quick hack, but i'm refactoring it and writing unit tests for each component.

Offcourse, instead of displaying each record for separate update/delete, you could simply loop through the $selections and update/delete them all at once.