To retrive all my records from a database and then edit them in a text box and click save to update them all???
Printable View
To retrive all my records from a database and then edit them in a text box and click save to update them all???
Of course :)
how does the query look?
Does it work out itself which record is getting updated itself?PHP Code:if (! mysql_query ("UPDATE DB_Stats Values ('','$name','$team','$tries','$goals','$fgoals','$total')"))
die ('Query Failed: ' . mysql_error());
No, you have to tell it which query you're updating or else it'll update the entire table with the same info ;) You should have a unique primary key, use that in thr query.
PHP Code:mysql_query("UPDATE DB_Stats VALUES ('','$name','$team','$tries','$goals','$fgoals','$total') WHERE my_unique_key=$thiskey") or die('Query Failed: ' . mysql_error());
Quote:
Originally posted by Chroder
No, you have to tell it which query you're updating or else it'll update the entire table with the same info ;) You should have a unique primary key, use that in thr query.
PHP Code:mysql_query("UPDATE DB_Stats VALUES ('','$name','$team','$tries','$goals','$fgoals','$total') WHERE my_unique_key=$thiskey") or die('Query Failed: ' . mysql_error());
How do I set there when I have all records showing!!
WHERE my_unique_key=$thiskey")
I think the easiest way would be to use each form element as an array, with the key being the ID. Let's pretend you have "id", "username" where "id" is the unique ID. You'd print your forms like:
Then you can loop through the username array, adding to SQL to execute:PHP Code:echo '<form action="action.php" method="post">';
while($row = mysql_fetch_array($result))
{
// Makes (for example) an input element "username[1]", where 1 would be your primary key.
echo '<input type="text" name="username[' . $row['id'] . ']" value="' . $row['username'] . '" />';
}
echo '<input type="submit" /></form>';
Not sure how easy that was to understand :pPHP Code:foreach($_POST['username'] as $k => $v)
{
mysql_query("UPDATE DB_Stats VALUES('', $v) WHERE id=$k") or die('Error');
}
Basically,
1. Use arrays for your forms and identify the key as your table's primary key
2. Loop through the arrays you made for your form, and use the key as your WHERE clause.
Parse error: parse error in /home/httpd/vhosts/httpdocs/savestatedit.php on line 17PHP Code:<?php
mysql_connect (localhost, user, pass);
mysql_select_db (nz);
if ("UPDATE DB_Stats VALUES ('','$name','$team','$tries','$goals','$fgoals','$total' WHERE id= $id") or die('Query Failed: ' . mysql_error);
?>
help
Cool got it all in my code!! I get "error" hmmm i have id and just name as two fieldsQuote:
Originally posted by Chroder
I think the easiest way would be to use each form element as an array, with the key being the ID. Let's pretend you have "id", "username" where "id" is the unique ID. You'd print your forms like:
Then you can loop through the username array, adding to SQL to execute:PHP Code:echo '<form action="action.php" method="post">';
while($row = mysql_fetch_array($result))
{
// Makes (for example) an input element "username[1]", where 1 would be your primary key.
echo '<input type="text" name="username[' . $row['id'] . ']" value="' . $row['username'] . '" />';
}
echo '<input type="submit" /></form>';
Not sure how easy that was to understand :pPHP Code:foreach($_POST['username'] as $k => $v)
{
mysql_query("UPDATE DB_Stats VALUES('', $v) WHERE id=$k") or die('Error');
}
Basically,
1. Use arrays for your forms and identify the key as your table's primary key
2. Loop through the arrays you made for your form, and use the key as your WHERE clause.
what could be wrong??