Results 1 to 11 of 11

Thread: Inserting into db

  1. #1

    Thread Starter
    Banned Michael_Kamen's Avatar
    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    1,180

    Inserting into db

    Hi,

    I'm trying to insert these values into my mysql database, but my browser throws in an error message at this line:

    PHP Code:
    $query "INSERT INTO staf_temp VALUES (', $_POST['achternaam'], $_POST['voornaam'],$_POST['tussenvoegsel'],$_POST['adres'],$_POST['postcode'], $_POST['woonplaats'],$_POST['telefoon'],, $_POST['email'],,$_POST['gebdat1'] . $_POST['gebdat2']. $_POST['gebdat3'],$_POST['voornaam'], Date('d m Y'), '3', $_POST['wachtwoord1']'); 
    The error is:

    Code:
    Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in blahblah/aanmelding.php on line 66

  2. #2

    Thread Starter
    Banned Michael_Kamen's Avatar
    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    1,180
    The first value is the ID, so I left that one blank. (hence only the ',' )

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    You had a lot of errors in that line. If you want to include a variable array in a string you need to enclose it in curly brackets. I.e.:

    PHP Code:
    $string "{$_POST['var']}
    If you dont do this then it will just print the varaible name as it is in the string.

    Have you checked your POST variables? Making sure all integers are integers and all strings have SQL meta characters escaped? - if not I suggest you do, otherwise your scriptwill be open to SQL injections.

    Also when you are writing an SQL query for MySql, enclose all all values in single quotes, even dates and numbers (mysql will convert them as necessary) and if you are omiting a field it is a good idea to put NULL in its place to show its being omitted. Although it makes your query bigger it is also a good idea to explicitly state only the fields you are going to insert. This that if you add or remove a column from your table , you won't need to change the query.

    I have corrected the line for you and it should work:
    PHP Code:
    $query "INSERT INTO staf_temp VALUES (NULL, '{$_POST['achternaam']}', '{$_POST['voornaam']}', " .
        
    "'{$_POST['tussenvoegsel']}', '{$_POST['adres']}', '{$_POST['postcode']}', " .
        
    "'{$_POST['woonplaats']}','{$_POST['telefoon']}', NULL, '{$_POST['email']}', NULL, " .
        
    "'{$_POST['gebdat1']}{$_POST['gebdat2']}{$_POST['gebdat3']}', '{$_POST['voornaam']}', '" .
        
    Date('d m Y') . "', '3', '{$_POST['wachtwoord1']}')"
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    you forgot to close the string.

    try this

    PHP Code:
    $query "INSERT INTO staf_temp VALUES (', $_POST['achternaam'], $_POST['voornaam'],$_POST['tussenvoegsel'],$_POST['adres'],$_POST['postcode'], $_POST['woonplaats'],$_POST['telefoon'],, $_POST['email'],,$_POST['gebdat1'] . $_POST['gebdat2']. $_POST['gebdat3'],$_POST['voornaam'], Date('d m Y'), '3', $_POST['wachtwoord1']')"
    also try using this syntax for your SQL statement

    Code:
    INSERT INTO table (name1, name2) VALUES ("value1", "value2")
    It means you only need to supply information for data you want to insert and not make mistakes with column name order (if that made sense )

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Michael, just curious -- what text editor do you use? Does it have syntax-coloring?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6

    Thread Starter
    Banned Michael_Kamen's Avatar
    Join Date
    May 2001
    Location
    The Netherlands
    Posts
    1,180
    I used notepad. Just switched to Crimson Editor.

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I used to use Notepad, and then I switched to EditPlus. One thing I noticed is that the syntax highlighting really helps catch errors like this. Like in the code above, since the semicolon is red like the text, you know something's up.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by Michael_Kamen
    The first value is the ID, so I left that one blank. (hence only the ',' )
    Also, in your code you have this:

    Code:
    VALUES (',
    That's going to generate an error on the MySQL end. You want:

    Code:
    VALUES ('',
    Two of them.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  9. #9
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Macromedia's Homesite 5.5 has excellent code coloring. It's a little expensive, but well worth it, especially if your company can buy it for you.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    gVIM - Open source and supports syntax highlighting for every language under the sun.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  11. #11
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I'd recommend EditPlus. It's not free, but it doesn't force you to purchase it.
    My evil laugh has a squeak in it.

    kristopherwilson.com

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