SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $row , update, while and co...

    Hello,

    My form has some fields with preset values like this:

    PHP Code:
    $i 1;
    while ( 
    $row mysql_fetch_array$sql_edu ) )


    echo (
    '<input name="school" value="'.$row["school"].'">');

    $i++;

    When the form is submitted, I want to update my table with new information.
    The problem is that $school is the value of the last input.

    How can I set a name to each field when i varies ?
    like:

    PHP Code:

    echo ('<input name="school[i]" value="'.$row["school"].'">'); 
    Thank you for helping me!
    Ginie G.

  2. #2
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    instead of school, you can use school[something], which will then be in the school array, with "something," etc. as the key(s), in your script.

    EDIT: oh, to create numeric indexes with $i, you can use this:

    PHP Code:
    echo "<input name=\"school[$i]\" value=\"$row[school]\">"
    Last edited by DR_LaRRY_PEpPeR; Mar 27, 2002 at 19:13.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  3. #3
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your answer, DR_LaRRY_PEpPeR !
    (love the name)
    Ginie G.

  4. #4
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another question...

    When I insert the data in my table, should I put:

    PHP Code:

    $i 
    1;
    while ( 
    $row mysql_fetch_array$sql) )


    $hellome = ("update education
    set  school = '
    $school[$i]') ;
    $i++;

    Ginie G.

  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's an UPDATE query you got, not an INSERT. but i guess that's what you want; to update the rows.

    i don't think you need that while () loop there, since fetching a result doesn't have to do with updating the table. you aren't actually executing the update query, either. and finally, if you were executing it, it would update all rows to the value that the last query specified since you don't have a WHERE clause. you need to specify some kind of id (the PRIMARY KEY from your table or something) in the form (hidden field) so you can tell which row to update in your query.

    so you might make your form like this:

    PHP Code:
    $i 1;
    while (
    $row mysql_fetch_array($sql_edu))
    {

    echo 
    "<input type=\"hidden\" name=\"id[$i]\" value=\"$row[id]\">";
    echo 
    "<input name=\"school[$i]\" value=\"$row[school]\">";

    $i++;

    then when updating you could do something like this (assuming your form is submitted via POST):

    PHP Code:
    for ($i 1$i <= count($_POST['school']); $i++)
    {
        
    mysql_query("UPDATE education SET school='{$_POST[school][$i]}' WHERE id={$_POST[id][$i]}");

    note: if your PHP version is less than 4.1.0, use $HTTP_POST_VARS instead of $_POST. of course, if you choose to, you can use the global variables $school, $id, etc.

  6. #6
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    Thank you so much for your answer!
    I'm sorry to bother you again....hmmmmm....
    OK, I've tested what you suggested in the code below:

    PHP Code:

    <?php 

    include("../common.php");
    include(
    "../db.php");

    dbConnect('gcomme');

    if (!isset(
    $submitupdate)): 


    ?>
    <html>

     <form method="POST" ACTION="<?=$PHP_SELF?>" >
    <?php

    $sql_edu 
    mysql_query ("select * from education
    where ID ='
    $ID' ");

    $i 1;
    while (
    $row mysql_fetch_array($sql_edu))
    {

    echo 
    "<input type=\"hidden\" name=\"id[$i]\" value=\"$row[id]\">";
    echo 
    "<input name=\"school[$i]\" value=\"$row[school]\">";

    $i++;

    }

    ?>

    <input type="submit" value="Update Resume" name="submitupdate" >
    </form>

    <html>

    <?php


    else:


    for (
    $i 1$i <= count($_POST['school']); $i++)
    {
        
    mysql_query("UPDATE education SET school='{$_POST[school][$i]}' WHERE id={$_POST[id][$i]}");
    }

    endif;
    ?>
    ...and guess what, it doesn't update the table!!!
    I'm very new to php, and I've never used _POST before.
    Can you show me what the code would be with $HTTP_POST_VARS ?

    Thank you!!!!
    Ginie G.

  7. #7
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, it looks like it's ID in your table and not id. so why don't you try changing $row[id] to $row[ID]. do the same in your UPDATE query; change "WHERE id=" to "WHERE ID=". see if that works.

    you will need to use $HTTP_POST_VARS if you have an older version of PHP. just change any instance of $_POST to $HTTP_POST_VARS.

    as i said, though, you can use the global variables, since you are in other parts of your code. e.g. instead of $_POST[school][$i] or $HTTP_POST_VARS[school][$i], you could use just $school[$i]. but i write my code using the arrays ($_POST/$HTTP_POST_VARS, etc.) becuase it's the preferred method and will work on all PHP installations, unlike using global variables. see the register_globals section in my PHP and MySQL coding tips thread for more info.

  8. #8
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, actually ID is another variable.
    I change $id into $eduid:


    PHP Code:
    <?php 

    include("../common.php");
    include(
    "../db.php");

    dbConnect('gcomme');

    if (!isset(
    $submitupdate)): 


    ?>
    <html>

     <form method="POST" ACTION="<?=$PHP_SELF?>" >
    <?php

    $sql_edu 
    mysql_query ("select * from education
    where ID ='
    $ID' ");

    $i 1;
    while (
    $row mysql_fetch_array($sql_edu))
    {

    echo 
    "<input name=\"eduid[$i]\" value=\"$row[eduid]\">";
    echo 
    "<input name=\"school[$i]\" value=\"$row[school]\">";

    $i++;

    }

    ?>

    <input type="submit" value="Update Resume" name="submitupdate" >
    </form>

    <html>

    <?php


    else:


    for (
    $i 1$i <= count($_POST['school']); $i++)
    {
        
    mysql_query("UPDATE education SET school='{$HTTP_POST_VARS[school][$i]}' WHERE eduid={$HTTP_POST_VARS[eduid][$i]}");
        

        print 
    $HTTP_POST_VARS[eduid][$i]; 
    }


    endif;

    ?>
    The "print $HTTP_POST_VARS[eduid][$i]; " doesn't return anything...
    Ginie G.

  9. #9
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you have an older version of PHP, you forgot to remove an instance of $_POST -- in your for loop. try it with this:

    PHP Code:
    for ($i 1$i <= count($HTTP_POST_VARS['school']); $i++) 

  10. #10
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HUUUUUUUUUUUUUUUUUUUUUURRRRRRRRRRRRRRRRRRRRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYYYYYYYYYYYYYYYYYY!!!!!

    It worked!!!

    Thank you so much!!!!!!!
    Ginie G.

  11. #11
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, good. i was running out of answers!

  12. #12
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My last question...

    If I want to update more than one field:
    I have 3 other vars like $school.
    How do I do that?
    Ginie G.

  13. #13
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just list as many as you want in the UPDATE query, seperated by commas:

    PHP Code:
    mysql_query("UPDATE education SET school='{$HTTP_POST_VARS[school][$i]}',
        otherthing='
    {$HTTP_POST_VARS[otherthing][$i]}',
        another='
    {$HTTP_POST_VARS[another][$i]}'
        WHERE eduid=
    {$HTTP_POST_VARS[eduid][$i]}"); 
    like that. BTW, i put the query on multiple lines just so it doesn't go off the screen here. you can make it one line, though, and it'll work fine.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •