|
-
May 2nd, 2004, 01:48 AM
#1
Thread Starter
Frenzied Member
Is this possible
To retrive all my records from a database and then edit them in a text box and click save to update them all???
-
May 2nd, 2004, 02:14 AM
#2
-
May 3rd, 2004, 06:17 PM
#3
Thread Starter
Frenzied Member
how does the query look?
PHP Code:
if (! mysql_query ("UPDATE DB_Stats Values ('','$name','$team','$tries','$goals','$fgoals','$total')"))
die ('Query Failed: ' . mysql_error());
Does it work out itself which record is getting updated itself?
-
May 3rd, 2004, 06:47 PM
#4
Member
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());
-
May 3rd, 2004, 11:04 PM
#5
Thread Starter
Frenzied Member
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")
-
May 3rd, 2004, 11:27 PM
#6
Member
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:
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>';
Then you can loop through the username array, adding to SQL to execute:
PHP Code:
foreach($_POST['username'] as $k => $v)
{
mysql_query("UPDATE DB_Stats VALUES('', $v) WHERE id=$k") or die('Error');
}
Not sure how easy that was to understand 
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.
-
May 3rd, 2004, 11:31 PM
#7
Thread Starter
Frenzied Member
PHP 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);
?>
Parse error: parse error in /home/httpd/vhosts/httpdocs/savestatedit.php on line 17
help
-
May 4th, 2004, 12:37 AM
#8
Thread Starter
Frenzied Member
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:
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>';
Then you can loop through the username array, adding to SQL to execute:
PHP Code:
foreach($_POST['username'] as $k => $v)
{
mysql_query("UPDATE DB_Stats VALUES('', $v) WHERE id=$k") or die('Error');
}
Not sure how easy that was to understand 
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.
Cool got it all in my code!! I get "error" hmmm i have id and just name as two fields
what could be wrong??
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|