SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Where is the error? - no error displayed!

    I'm tryng to create a query that checks a table to find a match, and if there is no match it finds the MAX and then adds 1, and insert the new value. It's then supposed to display it. I've tried to add mysql_error() to see what they say, but there is nothing displayed, so I don't know where I go wrong. Please help me!

    PHP Code:
    function create_faktura($orderid)
    {
        
    $conn db_connect();
    //Let's find the fakturanr that matches the orderid
        
    $query "select fakturanr from faktura where orderid = '$orderid'";
        
    $result mysql_query($query);
    //If there is no match let's find the largest fakturanr
        
    if (!$result)
        {
            
    $query2 "select fakturanr from faktura where fakturanr = (select MAX(fakturanr) from faktura)";
            
    $result2 mysql_query($query2);
    //Then add 1 to the number and insert it as a new record with matching orderid
            
    $nyfakt $result2 1;
            
    $query "insert into faktura values ('', '$orderid', '$nyfakt', '')";
            
    $result mysql_query($query);
            if (!
    $result)
                return 
    false;
    //If all goes well, display the fakturanr
            
    else
            {
                
    $fakturanr $nyfakt;
                
    display_faktura($fakturanr);
            }
        }
        else
        {
    //If there was a match in the beginning, display the fakturanr
            
    $fakturanr mysql_fetch_array($result);
            
    display_faktura($fakturanr);
        }

    The table (faktura) is empty (new table hasn't been used before) and it contains four columns. I have tried this function, and no data has been inserted into the table at any time yet

    Fakturanr is the same as invoice#, and needs to be in order. Just using auto-increment won't work if an invoice is deleted for some reason.

    I have tried to get it to work, made some changes, searched the forum and the MySQL website, and now I'm lost.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the problem is how your checking the results. Using
    PHP Code:
    $result mysql_query($query
    will just give you a resource id or such. What you could try is after doing that do
    PHP Code:
    $num mysql_num_rows($result); 
    which will give you a count of the rows (which should return 0 in this case)

    So, the code would look like
    PHP Code:
    function create_faktura($orderid

        
    $conn db_connect(); 
    //Let's find the fakturanr that matches the orderid 
        
    $query "select fakturanr from faktura where orderid = '$orderid'"
        
    $result mysql_query($query);
        
    $num mysql_num_rows($result);
    //If there is no match let's find the largest fakturanr 
        
    if (!$num
        { 
            
    $query2 "select fakturanr from faktura where fakturanr = (select MAX(fakturanr) from faktura)"
            
    $result2 mysql_query($query2); 
    //Then add 1 to the number and insert it as a new record with matching orderid 
            
    $nyfakt $result2 1
            
    $query "insert into faktura values ('', '$orderid', '$nyfakt', '')"
            
    $result mysql_query($query); 
            if (!
    $result
                return 
    false
    //If all goes well, display the fakturanr 
            
    else 
            { 
                
    $fakturanr $nyfakt
                
    display_faktura($fakturanr); 
            } 
        } 
        else 
        { 
    //If there was a match in the beginning, display the fakturanr 
            
    $fakturanr mysql_fetch_array($result); 
            
    display_faktura($fakturanr); 
        } 

    hope this helps
    SE

  3. #3
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems logical I'm going to try it right now.

    <edit>It added as it should, but didn't display it. Needs to go through the display-part again. Thanks for getting me on the right track
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  4. #4
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK...
    Somewhere I get the display wrong...

    I removed the mysql_fetch_array() bit to make it simple. There is only one value so there can't be an arry, or...?

    PHP Code:
    function display_faktura($fakturanr)
    {
        echo 
    '<div class="faktura">Faktura nr: '.$fakturanr.'</div>';

    I get Resource ID #14 as a result.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, it looks like you're not processing the queries quite right. I haven't tested this but try it and if it works have a look at what changes i made:
    PHP Code:
    function create_faktura($orderid

        
    $conn db_connect(); 
    //Let's find the fakturanr that matches the orderid 
        
    $query "select fakturanr from faktura where orderid = '$orderid'"
        
    $result mysql_query($query); 
    //If there is no match let's find the largest fakturanr 
        
    if (!$result
        { 
            
    $query2 "SELECT fakturanr FROM faktura ORDER BY fakturanr DESC LIMIT 1";
            
    $result2 mysql_query($query2);
            
    $rs mysql_fetch_array($result2);
            
    $fa $rs["fakturanr"];
    //Then add 1 to the number and insert it as a new record with matching orderid 
            
    $nyfakt = ($rs+0)+1;
            
    $query "insert into faktura values ('', '$orderid', '$nyfakt', '')"
            
    $result mysql_query($query); 
            if (!
    $result
                return 
    false
    //If all goes well, display the fakturanr 
            
    else 
            { 
                
    $fakturanr $nyfakt
                
    display_faktura($fakturanr); 
            } 
        } 
        else 
        { 
    //If there was a match in the beginning, display the fakturanr 
            
    $fakturanr mysql_fetch_array($result); 
            
    display_faktura($fakturanr); 
        } 

    SE

  6. #6
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got a real wopping error on the line: $rs = mysql_fetch_array($result2);

    And it didn't add up. You are right though... I have a problem in that part. Let me see if I can try something else...

    ERROR:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  7. #7
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks like this now:
    PHP Code:
    function create_faktura($orderid)
    {
        
    $conn db_connect();
        
    $query "select fakturanr from faktura where orderid = '$orderid'";
        
    $result mysql_query($query);
        
    $num mysql_num_rows($result);
        if (!
    $num)
        {
            
    $query2 "SELECT fakturanr FROM faktura ORDER BY fakturanr DESC LIMIT 1";
            
    $result2 mysql_query($query2);
            
    $nyfakt = ($result2+0)+1//This becomes 16 when the next row is added
            
    $query "insert into faktura values ('', '$orderid', '$nyfakt', '')";
            
    $result mysql_query($query);
            if (!
    $result)
                return 
    false;
            else
            {
                
    $fakturanr mysql_fetch_array($result2);
                
    display_faktura($fakturanr);
                
    print_r($fakturanr);
            }
        }
        else
        {
            
    $fakturanr mysql_fetch_array($result);
            
    display_faktura($fakturanr);
            
    print_r($fakturanr);
        }
    }

    function 
    display_faktura($fakturanr)
    {
        echo 
    '<div class="faktura">Faktura nr: '.$fakturanr['fakturanr'].'</div>';

    When it adds up, the result isn't 2, but 16! I used print_r to check what's in the array. fakturanr becomes 1 in both instances (displaying old order or after just adding a new).
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you echo what $result2 is giving you, i think it'll just be giving you a resource id. You need to get the value of the result before adding on 1

  9. #9
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Resource id #15

    How do I make it give me what is in fakturanr in the table?
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  10. #10
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    as i put before
    PHP Code:
    $query2 "SELECT fakturanr FROM faktura ORDER BY fakturanr DESC LIMIT 1";
    $result2 mysql_query($query2);
    $rs mysql_fetch_array($result2);
    $fa $rs["fakturanr"]; 
    then $fa will hold the value of fakturanr, which should be the last entry (and the highest number) in the table.

    You said you tried that but you got an error, what error did it give you?

  11. #11
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hang on... I'll try it again to give you the right error...

    Now it works... and I know why it didn't work when I first tried it... in your eaxmple there was an error...
    PHP Code:
    query2 "SELECT fakturanr FROM faktura ORDER BY fakturanr DESC LIMIT 1"
            
    $result2 mysql_query($query2); 
            
    $rs mysql_fetch_array($result2); 
            
    $fa $rs["fakturanr"]; 

            
    $nyfakt = ($rs+0)+1//It shouldn't be $rs but $fa!!! 
    Fixed it and a couple of other things with the display-part. When ther already is a record it is an array in the result, and when it is added it is just a number.

    This is the complete code for the creation and the display of my invoice# (fakturanr)
    PHP Code:
    function create_faktura($orderid)
    {
        
    $conn db_connect();
        
    $query "select fakturanr from faktura where orderid = '$orderid'";
        
    $result mysql_query($query);
        
    $num mysql_num_rows($result);
        if (!
    $num)
        {
            
    $query2 "SELECT fakturanr FROM faktura ORDER BY fakturanr DESC LIMIT 1";
            
    $result2 mysql_query($query2);
            
    $rs mysql_fetch_array($result2);
            
    $fa $rs["fakturanr"];
            
    $nyfakt = ($fa+0)+1;
            
    $query "insert into faktura values ('', '$orderid', '$nyfakt', '')";
            
    $result mysql_query($query);
            if (!
    $result)
                return 
    false;
            else
            {
                
    $fakturanr $nyfakt;
                
    display_faktura($fakturanr);
            }
        }
        else
        {
            
    $fakturanr mysql_fetch_array($result);
            
    $fakturanr $fakturanr['fakturanr'];
            
    display_faktura($fakturanr);
        }
    }

    function 
    display_faktura($fakturanr)
    {
        echo 
    '<div class="faktura">Faktura nr: '.$fakturanr.'</div>';

    For those who wish to translate it: faktura = invoice, fakturanr = invoice_no. Also on the table there is fakturaid = invoice_id and makulera = void.

    Now with this it's possible to make a mistake when pulling up an invoice the first time, but when an invoice is void when no misstake has been made (like clicking the wrong button), it can be marked as such.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  12. #12
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just saw i made a typo before. here's the code i think *should* work
    PHP Code:
    function create_faktura($orderid

        
    $conn db_connect(); 
    //Let's find the fakturanr that matches the orderid 
        
    $query "select fakturanr from faktura where orderid = '$orderid'"
        
    $result mysql_query($query); 
    //If there is no match let's find the largest fakturanr 
        
    if (!$result
        { 
            
    $query2 "SELECT fakturanr FROM faktura ORDER BY fakturanr DESC LIMIT 1"
            
    $result2 mysql_query($query2); 
            
    $rs mysql_fetch_array($result2); 
            
    $fa $rs["fakturanr"]; 
    //Then add 1 to the number and insert it as a new record with matching orderid 
            
    $nyfakt = ($fa+0)+1
            
    $query "insert into faktura values ('', '$orderid', '$nyfakt', '')"
            
    $result mysql_query($query); 
            if (!
    $result
                return 
    false
    //If all goes well, display the fakturanr 
            
    else 
            { 
                
    $fakturanr $nyfakt
                
    display_faktura($fakturanr); 
            } 
        } 
        else 
        { 
    //If there was a match in the beginning, display the fakturanr 
            
    $fakturanr mysql_fetch_array($result); 
            
    display_faktura($fakturanr); 
        } 


  13. #13
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just think what a typo can do
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  14. #14
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that'll teach me not to test anything

    *jumps out window*

  15. #15
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't worry about it You really helped me a lot! You gave me things to test, and that's the main point. I feel great about spotting the typo myself. It means I'm really getting the hang of it.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  16. #16
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    UK
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no problem, typo's are part of my work - keep's me busy


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
  •