SitePoint Sponsor

User Tag List

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

    Help with update and while

    Hello,
    I have a problem with my form where the number of fields varies.
    When I submit the form, the table is updated with the new values.
    Here is the code I have:


    PHP Code:
    $sql_exp mysql_query ("select * from experience
    where ID ='
    $ID' ");

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


    echo(
    '<input type="text" name=\"title[$i]\" value="'.$row["title"].'" >');
    $a=$i;

    $i++;

    I put $a in a hidden field:
    <input type="hidden" value="<? print $a ?>" name="a" >

    After the form is submitted:

    PHP Code:

    $i 
    1;
    while ( 
    $i <= $a ) {


    $hellome1 = ("update experience
    set  location = 'location[
    $i]', title ='title[$i]' , dateexp ='dateexp[$i]' ) 
    where ID ='
    $ID' ") ;
    $i++;


    if (!
    mysql_query($hellome1))
            
    error("A database error occurred in processing 
                  your submission."
    ); 



    I get the error message ( A database error occurred in processing your submission )
    What's wrong ?
    Ginie G.

  2. #2
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Help with update and while

    PHP Code:
    $hellome1 = ("update experience
    set  location = 'location[
    $i]', title ='title[$i]' , dateexp ='dateexp[$i]' 
    where ID ='
    $ID' ") ; 
    that query above had an extra parenthesis... unless it's a typo

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

    I corrected my mistake bu I still get the error message...
    I was wondering if the part with ($i<= $a) is correct ?
    Ginie G.

  4. #4
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $hellome1 "update experience
    set  location = 'location[
    $i]', title ='title[$i]' , dateexp ='dateexp[$i]' 
    where ID ='
    $ID' "
    oops you also didn't need the enclosing parentheses...

    other than that, your location[$i] and so on must exist... i'm assuming they're coming from a form...

    <<edit>> i must add though that the way you're doing it is rather convoluted... i'd give you a better idea but it's late and my mind's going into hibernation mode
    Last edited by redemption; Mar 28, 2002 at 10:39.

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

    I still get the error message....
    Ginie G.

  6. #6
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    add this line:
    PHP Code:
    echo $hellome1
    after the $hellome1 declaration and post the query you get...

  7. #7
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tested my variables like this, just after the form:

    PHP Code:
    $i 1;
    while ( 
    $i <= $a ) {

    echo (
    $location[$i]);
    echo (
    $title[$i]);
    ech ($dateexp[$i]); 

    $i++; 
    When I do that, nothing happens...
    Ginie G.

  8. #8
    SitePoint Zealot ginieg's Avatar
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, I did what you suggested and I get:

    update experience set location = 'location[2]', title ='title[2]' , dateexp ='dateexp[2]' where ID ='156'

    ???????????????//
    Ginie G.

  9. #9
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $hellome1 "update experience
    set  location = '
    $location[$i]', title ='$title[$i]' , dateexp ='$dateexp[$i]
    where ID ='
    $ID' "
    ok my fault again for not noticing another error... you need the $ to indicate that location[$i] is a PHP variable...

    as for why $location[$i] has no value, you'd need to be sure that it's created upon form submission... as in you need a form field with the name="location[]"

  10. #10
    SitePoint Enthusiast z00om's Avatar
    Join Date
    Dec 2001
    Location
    California
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    let me add my 2 cents:

    mysql_error() is a VERY HANDY function!

    change...

    PHP Code:
    if (!mysql_query($hellome1))
            
    error("A database error occurred in processing 
                  your submission."
    ); 
    to this:

    PHP Code:
    $result mysql_query($hellome1) or die("An error occured in processing your submission, details:<br />Query: ".$hellome1."<br />Error: ".mysql_error()); 
    this will tell you:

    An error occured in processing your submission,
    Query: update experience set location = 'location[2]', title ='title[2]' , dateexp ='dateexp[2]' where ID ='156'
    Error: [what exactly mysql doesn't like]

    and its that 'what exactly mysql doesn't like' that'll help you solve the issue. mysql_error() is a very useful tool when you are getting db errors... I suggest you never leave hom.. er, never code without it!
    [z00om : home]
    [z00om@hotmail.com]
    [z00om : aim]
    [77968493 : icq]


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
  •