SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    perfect code wont work

    Ok this is not the first time this has happened to me... As far as I can tell the code is perfect....

    ---- I have tried several codes for inserting data into my database... The first one on the list is actually copied and pasted directly from the MyAdmin generator....

    PHP Code:
    mysql_query("INSERT INTO `a5902540_arcadia`.`profile` (`email`, `mems`, `gifts`, `candles`, `tributes`, `join`) 
    VALUES (\'123\', \'0\', \'0\', \'0\', \'0\', \'123\')"
    );
                    
    mysql_query("INSERT INTO `a5902540_arcadia`.`profile` (`email`, `mems`, `gifts`, `candles`, `tributes`, `join`) 
    VALUES ('
    $email', \'0\', \'0\', \'0\', \'0\', '$date')");
                    
    mysql_query("INSERT INTO `a5902540_arcadia`.`profile` (`email`, `mems`, `gifts`, `candles`, `tributes`, `join`) 
    VALUES ('
    $email', '0', '0', '0', '0', '$date')"); 
    I've tried it all... Now my other code does work and does insert properly and this is it.

    PHP Code:
    mysql_query("INSERT INTO `members` (`firstname`, `lastname`, `email`, `password`)
    VALUES ('
    $firstname', '$lastname', '$email', '$password')"); 
    I have no clue what is going on... unfortunately this happens to me all the time... working with databases with me is always a hit and a miss... Can someone tell me wtf is going on?

  2. #2
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it doesn't work, it's not all that perfect.

    Debugging is a big part of coding. You need to get better at it. Things to try:

    1. Echo out the query. Run it by hand.
    2. Echo out mysql_error() right after running mysql_query to see what errors, if any, MySQL is returning.

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's no need to escape your single quotes in the first example.
    They are a legitimate part of the SQL query, and the PHP string is wrapped in double quotes, so the singles will not prematurely close the string.

  4. #4
    SitePoint Enthusiast premiumscripts's Avatar
    Join Date
    Aug 2009
    Location
    PremiumScripts.com
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Obviously it's not perfect, you're not even doing any error detection whatsoever. (mysql_error() etc)

  5. #5
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    344
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not working, therefore it is not perfect.

    Even working code is not perfect.

    Copying and pasting code does not work well, unless you understand every stanza of every line. Then, once you do, you will see that you have to rewrite the whole thing anyways.

  6. #6
    SitePoint Guru silver trophy JamesColin's Avatar
    Join Date
    May 2009
    Location
    Jomtien, Pattaya, Thailand
    Posts
    910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't like when someone says "it doesn't work" or "this code produces an error" and then doesn't give the exact error message or anything else at all.
    Do you really need traffic? Where to? What for?
    If you really do need traffic then stop messing around!
    Advertise on my sites today: She Told Me & Best Reviewer :
    200,000+ UV / Month

  7. #7
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thirst of all, do not use single quotes around integer values, only around strings:

    PHP Code:
    mysql_query("INSERT INTO profile ('email', 'mems', 'gifts', 'candles, 'tributes', 'join') 
    VALUES (123, 0, 0, 0, 0, 123)"
    ); 
    I am assuming the table columns are INT because you are trying to insert digits to the database. It would help if you posted the table structure.

    Another thing I noticed, you are trying to insert 123 into 'email' column, I guess that column is VARCHAR so the query will fail because of that.

  8. #8
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also I guess the column 'join' is DATE or DATETIME. In that case you must use properly formatted date, 123 will cause the query to fail.


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
  •