Results 1 to 8 of 8

Thread: Is this possible

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    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???

  2. #2

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139
    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?

  4. #4
    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()); 

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139
    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")

  6. #6
    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.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139
    PHP Code:
    <?php

    mysql_connect 
    (localhostuserpass);
    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

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139
    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
  •  



Click Here to Expand Forum to Full Width