|
Posted by Tyrone Slothrop on 10/10/68 11:39
On 8 Feb 2006 12:17:46 -0800, "UKuser" <spidercc21@yahoo.co.uk> wrote:
>Hi Guys,
>
>I am playing with a PHP/MySQL script at
>http://nana46.coconia.net/test4.php
>
>What I have done is placed every record from the table in an input box,
>so that each box can be edited. I've given each field a unique name,
>and then assigned the overall name in the update query. However when I
>update, it does update everything but not with the information I've
>typed in. In short I'm hoping for users to be able to type in any
>boxes/fields, click Update and the record update, rather than editing a
>record at a time.
>
>Any thoughts would be great.
>
>Thanks
>
>A
>
>I am using the following code:
>
><HTML>
><TITLE>test 4</title>
><form method='post' action='test4.php'>
><?php
>function check_mysql()
>{
> if (mysql_errno() > 0)
> {
> die("<BR> MySQL error " . mysql_errno() . ": " .
>mysql_error());
> }
>}
>
>$db = mysql_connect("coconia.net", "nana46_nana46", "hello");
>if (!$db)
>{
> die("Failed to open connection to MySQL server.");
>}
>
>mysql_select_db("nana46_nana46");
>check_mysql();
>
>$requete = "SELECT id,lowerval,upperval,result FROM fig_lookup";
>$resulta = mysql_query($requete) or die (mysql_error());
>
>if($mode == "Update")
>{
> $query = "UPDATE fig_lookup SET lowerval ='$Lid', upperval='Uid',
>result='$Rid'";
> $req = mysql_query($query);
> check_mysql();
>}
>
>echo '<table border="2" cellpadding="0" cellspacing="0" width="100%">';
>
>$edit = "<img src='edit.gif' border='0' alt='Edit record'>";
>
>echo "<table border='2'>";
>echo "<td> </td>";
>echo "<td>id</td>";
>echo "<td>Lowerval</td>";
>echo "<td>Upperval</td>";
>echo "<td>Result</td>";
>
>while ($line = mysql_fetch_array($resulta, MYSQL_ASSOC))
>{
> $id = $line["id"];
> {
> $num = "0.$id";
> $Lid = "1.$id";
> $Uid = "2.$id";
> $Rid = "3.$id";
>
> $clinec = "<TD><input name=$num type=text value=";
> $clined = "<TD><input name=$Lid type=text value=";
> $clinee = "<TD><input name=$Uid type=text value=";
> $clinef = "<TD><input name=$Rid type=text value=";
> }
>
> echo "<tr>";
> echo ("<TD WIDTH=15><B><A
>HREF=\"test5.php?id=$id\">$edit</A></B></TD>\n");
>
> // Affichage des champs sur une line
> echo $clinec.$line["id"]."></td>";
> echo $clined.$line["lowerval"]."></td>";
> echo $clinee.$line["upperval"]."></td>";
> echo $clinef.$line["result"]."></td>";
>
>}
>
>echo "</tr>";
>echo "</table>";
>?>
> <input type='submit' name=mode value='Update'>
> </form>
> </HTML>
Don't worry about assigning unique names to the form fields. This
complicaters your code and, by the time you get it working, will make
you crazy. Instead, use arrays.
Rather than trying to figure out what fields are which, I am just
going to assign field name arbitrarily:
<? // display records
while ($l = mysql_fetch_array($resulta, MYSQL_ASSOC))
{ ?>
<tr><td><input type="text" name="f1[]" vallue="<?=$l[0]?>"></td></tr>
<tr><td><input type="text" name="f2[]" vallue="<?=$l[1]?>"></td></tr>
<tr><td><input type="text" name="f3[]" vallue="<?=$l[2]?>"></td></tr>
<?
} //end display
?>
<? // saving records
for ($i=0; $i<count($_POST['f1']; $i++)
{
mysql_query ("UPDATE table SET f2='{'$_POST['f2'][$i]}',
f3='{'$_POST['f3'][$i]}' WHERE f1='{'$_POST['f1'][$i]}'");
}
?>
Navigation:
[Reply to this message]
|