What on earth am I doing wrong? (php+mysql)

Discussion in 'Web Design and Development' started by Poff, Aug 14, 2007.

  1. Poff macrumors 65816

    Poff

    Joined:
    Sep 16, 2003
    Location:
    Stavanger, Norway
    #1
    It's been a while since I've been doing anything with sql, and unfortunately I've deleted most of my old php files. I found a few, though, and tried to implement them. But something goes wrong, and I can't see what. Anyone else see it?

    PHP:
    <?php

    $periode
    ="10";
    $konto="20";
    $belop="40";

    mysql_connect("sql02.fastname.no","user","password");
    @
    mysql_select_db("flaatartist") or die("error connecting to database");

    mysql_query("INSERT INTO 2007 VALUES ('$periode','$konto','$belop')");


    mysql_close();
    ?>
     
  2. Aranince macrumors 65816

    Joined:
    Apr 18, 2007
    Location:
    California
    #2
    Maby try this:

    PHP:
    mysql_query("INSERT INTO 2007 VALUES (" $periode "," $konto "," $belop ")");
     
  3. Poff thread starter macrumors 65816

    Poff

    Joined:
    Sep 16, 2003
    Location:
    Stavanger, Norway
    #3
    thanks, but that didn't work. :(

    it needs to be ' since I allready have a ". And I didn't understand why the punctuations? (I'm a rookie when it comes to mySQL.)
     
  4. Aranince macrumors 65816

    Joined:
    Apr 18, 2007
    Location:
    California
    #4
    In PHP the . can be used to add a variable to a string. What is the problem exacly?

    PHP:
    mysql_query("INSERT INTO 2007 VALUES (\'" $periode "\' , \' " $konto " \',\'" $belop "\' )");  
     
  5. Poff thread starter macrumors 65816

    Poff

    Joined:
    Sep 16, 2003
    Location:
    Stavanger, Norway
    #5
    apparantly, the problem was me calling the table "2007". It worked nice in CocoaMySQL, but my php-script didn't like it. I renamed it "2007test" and now it works just fine.

    PHP:
    mysql_query("INSERT INTO 2007test VALUES ('$periode','$konto','$belop')");
    anyone know a way to insert into a table named "2007"? There has to be a way..
     
  6. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #6
    make sure the table value is int.
     
  7. Poff thread starter macrumors 65816

    Poff

    Joined:
    Sep 16, 2003
    Location:
    Stavanger, Norway
    #7
    I was adding values to three fields in a table, but I didn't specify the field names in my script so it might have been confusing. (picture describing this in bottom of post) But all is well now, since my table is no longer named "2007".

    I think we're misunderstanding each other. Here's a picture of what I'm talking about, I want to name the table "2007", and not "2007test", but my php-script doesn't manage to post to the "2007"-table for some reason.

    [​IMG]
     
  8. Knox Administrator

    Knox

    Staff Member

    Joined:
    Jul 1, 2002
    Location:
    UK
    #8
    You'd need to use ` (backtick) characters around the table name, e.g.

    PHP:
    mysql_query("INSERT INTO `2007` VALUES ('$periode','$konto','$belop')");
     
  9. Poff thread starter macrumors 65816

    Poff

    Joined:
    Sep 16, 2003
    Location:
    Stavanger, Norway
    #9
    I did try that, but it didn't work.

    ..and I still don't understand why using only numbers should be worse than using letters and numbers.. :/
     
  10. Aranince macrumors 65816

    Joined:
    Apr 18, 2007
    Location:
    California
    #10
    There is also a way to specify which column the values get put into. This might be a good thing to do...

    "INSERT INTO 2007 (column1, column2, column3) VALUES ('$periode', '$kanto', '$belop')
     
  11. Poff thread starter macrumors 65816

    Poff

    Joined:
    Sep 16, 2003
    Location:
    Stavanger, Norway
    #11
    yeah, but that doesn't solve the problem with the table being named "2007". (that line you wrote wouldn't work on my test-project.)

    I simply don't understand this.. :/
     
  12. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #12
    from here:
    so the note about using ticks was right on; there must be another reason it's not working for you.
     
  13. OutThere macrumors 603

    OutThere

    Joined:
    Dec 19, 2002
    Location:
    NYC
    #13
    how about trying this:

    PHP:
    mysql_query("INSERT INTO `2007` VALUES ('$periode','$konto','$belop')") or die(mysql_error());
     
  14. Matteh117 macrumors regular

    Joined:
    May 24, 2007
    Location:
    Surrey, UK
    #14
    You don't need a "backtick". An apostrophe works fine. ;)

    Also, if you're using an @ (which I believe ignores errors) then you don't need an "or die".

    PHP:
    mysql_query("insert into '2007' values ('".$periode."', '".$konto."', '".$belop."')");
    Should do it.
     
  15. Knox Administrator

    Knox

    Staff Member

    Joined:
    Jul 1, 2002
    Location:
    UK
    #15
    Code:
    mysql> describe '2007';
    ERROR 1064: You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near ''2007'' at line 1
    
    mysql> describe `2007`;
    +-------+------------------+------+-----+---------+-------+
    | Field | Type             | Null | Key | Default | Extra |
    +-------+------------------+------+-----+---------+-------+
    | id    | int(10) unsigned |      |     | 0       |       |
    +-------+------------------+------+-----+---------+-------+
    
    @ suppresses the display of errors but they should still be handled in some way. die() doesn't give the most user friendly of error pages, but does at least give something.
     
  16. Matteh117 macrumors regular

    Joined:
    May 24, 2007
    Location:
    Surrey, UK
    #16
    Meh, well AFAIK you don't need either to define a table name.
     
  17. macmanmatty macrumors regular

    Joined:
    Jul 10, 2005
    Location:
    I have calculated my velocity so precisely that a
    #17
    you arre correct, He neeeds no quotes or backticks around his table name
     
  18. zimv20 macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    toronto
    #18
    check my post #12; it seems one does need to quote it somehow.
     
  19. macmanmatty macrumors regular

    Joined:
    Jul 10, 2005
    Location:
    I have calculated my velocity so precisely that a
    #19
    You are correct. I was unaware that db with names that consist soley of numbers must quoted.
     
  20. Mr.nix macrumors member

    Joined:
    Jan 30, 2007
    #20
    To create

    PHP:
    mysql_query
        
    ("CREATE TABLE `2007` (
            id INT NOT NULL AUTO_INCREMENT, 
            PRIMARY KEY(id),
            field1 INT(5) NOT NULL, 
            field2 INT(5) NOT NULL,
            field3 INT(5) NOT NULL
        )"
    )
     or die(
    mysql_error());  

    To insert
    PHP:
    $periode '10';
    $konto '20';
    $belop '40';


    mysql_query("
        INSERT INTO `2007` 
            (field1, field2, field3)
        VALUES
            (
                '" 
    $periode "',
                '" 
    $konto "',
                '" 
    $belop "'
            )
        "
    );

    Or this. Depends on what you're doing.

    PHP:
    $periode '10';
    $konto '20';
    $belop '40';


    mysql_query("
        INSERT INTO `2007` 
            (field1, field2, field3)
        VALUES
            (
                
    $periode,
                
    $konto,
                
    $belop 
            )
        "
    );


    Formated so you can read it.
     

Share This Page