SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Los Angeles
    Posts
    325
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with insert news script

    i have this script:

    PHP Code:
    <?
    //connect to database
    $linkmysql_connect ("localhost""root""godzilla");
    mysql_select_db ("CSI");

    //build query
    $query"INSERT INTO tbl_articles (issue, authorID, catID, subcat, title, subhead1, subhead2, specs, body) 
    VALUES ('
    $issue', $authorID$catID, '$subcat', '$title', '$subhead1', '$subhead2', '$specs', '$body')";

    $result mysql_query ($query);

    if (!
    $result) {
            print (
    mysql_error());
    }else{
            print (
    "data added succesfully");
            }


    mysql_close ($link);
    ?>
    and its giving me this error:

    You have an error in your SQL syntax near ' , '', '', '', '', '', '')' at line 2


    I cant figure out what it is, can anyone help me out?
    From here on, it's instinctual...even straight roads meander.

  2. #2
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try rewriting your query like this:
    PHP Code:
    INSERT INTO tbl_articles SET
    issue    
    '$issue',
    authorID '$authorID',
    catID    '$catID',
    subcat   '$subcat',
    title    '$title',
    subhead1 '$subhead1',
    subhead2 '$subhead2',
    specs    '$specs',
    body     '$body'
    Structure (visually) isn't so important although helpful. The important part is to write the query as I have written it.(EDIT***That sounds pretty cocky doesn't it? Sorry) The "values" method works but is more difficult to look at and debug. The way I wrote it I know has good syntax, so if you get errors, check your field names or otherwise.

    Have fun.

    Sketch
    Last edited by Sketch; Apr 5, 2002 at 08:10.
    Aaron Brazell
    Technosailor



  3. #3
    SitePoint Enthusiast gcarlyle's Avatar
    Join Date
    Mar 2002
    Location
    Olympia Wa
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your line with
    PHP Code:
    VALUES ('$issue'$authorID$catID'$subcat''$title''$subhead1''$subhead2''$specs''$body')"; 
    $authorID and $catID needs to have the single quote ' around them.

    I think that is your problem.

    Hope it helps
    Jennifer
    Wanna be php coder!

  4. #4
    blonde.... Sarah's Avatar
    Join Date
    Jul 2001
    Location
    Berkshire, UK
    Posts
    7,442
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Jennifer,

    You don't need to single quote around number values i.e. if his ID columns are only numbers then he doesn't need to single quote them...

    sorry really bad english there

    Regular user

  5. #5
    SitePoint Enthusiast gcarlyle's Avatar
    Join Date
    Mar 2002
    Location
    Olympia Wa
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's greate. I didn't know if I was right but I had to take a stab at it any way.

    Just learned something so it was worth it.
    Wanna be php coder!

  6. #6
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Los Angeles
    Posts
    325
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sketch: thanks, that lokos great. I didint know you could it like that. I need to use addslashes with the body, so would i do it like :

    body = addslashes('$body');

    ?

    Jenn, thank you for your help, but as Sarah said you don't have to (and shouldn't) put single quotes around numeric values. I'm a PHP newbie too, and they say the best way to learn is to help someone else so keep at it.
    From here on, it's instinctual...even straight roads meander.

  7. #7
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ummm....
    PHP Code:
    $body addslashes($body
    Make sure you put that $ sign on the front.

    Sketch
    Aaron Brazell
    Technosailor



  8. #8
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Los Angeles
    Posts
    325
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry i didnt phrase that well...I would use addslashes before the insert statement, not when i am doing

    body = '$body';

    inside the insert stament, right?
    From here on, it's instinctual...even straight roads meander.

  9. #9
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it could be done either way, but just to be safe, do it prior to running the insert query.

    Sketch
    Aaron Brazell
    Technosailor



  10. #10
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    remember, you shouldn't use addslashes() if magic_quotes_gpc is on. you'll add too many slashes! i hate this, one of the things that really complicates things for beginners. i hate magic_quotes. see the section magic_quotes, addslashes(), and stripslashes() in my PHP and MySQL coding tips thread for more info.

    just trying to help you get your text in the database correctly.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  11. #11
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Los Angeles
    Posts
    325
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, thats right, i did read that thread actually. Im not sure if magic quotes is on though, how do i check to see if it is, and how do i turn it off?

    actually, lemme take a gander at that thread again...
    From here on, it's instinctual...even straight roads meander.

  12. #12
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Los Angeles
    Posts
    325
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i see i am supposed to make a change to the php.ini file, but i cant find where it is located...I am using mac os x, does anyone know where it is?
    From here on, it's instinctual...even straight roads meander.


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
  •