SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast magnam's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Care to tell me what's wrong with my INSERT INTO code?

    I am trying to submit surveys to my database, like this one for example:
    http://didjoo.com/sur.php

    (the Q & A's are just for fun, examples)

    Code PHP:
    mysql_query("INSERT INTO `surveys` (id, title, desc, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q14, q15) VALUES ('0', $title, $desc, $q1, $q2, $q3, $q4, $q5, $q6, $q7, $q8, '$q9, $q10, $q11, $q12, $q13, $q14, $q15)");

    I'm not getting any errors with the above code, it simply does not record.

    I go into my new database and it says "0 Records"

    The "ID" field is auto_increment/INT/20 chars, I plan to use the ID to request each survey and display/give each survey their own page.

    I've successfully submitted to a mysql a few times before.

    Clearly I'm new at this so be nice, thanks!

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Location
    New York
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code MySQL:
    mysql_query("INSERT INTO `surveys` (id, title, desc, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q14, q15) VALUES ('0', $title, $desc, $q1, $q2, $q3, $q4, $q5, $q6, $q7, $q8, $q9, $q10, $q11, $q12, $q13, $q14, $q15)");
    Try

  3. #3
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if id is set to be auto incremented you do not need to specify a value for it explicitly. Try something like this:
    Code PHP:
    mysql_query("INSERT INTO `surveys` (title, desc, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q14, q15) VALUES ($title, $desc, $q1, $q2, $q3, $q4, $q5, $q6, $q7, $q8, '$q9, $q10, $q11, $q12, $q13, $q14, $q15)");

    Also all string values above should be wrapped within single quote.

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Location
    New York
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dont forgot to fix your $q9 variable. there should not be an ' infront of it

  5. #5
    SitePoint Enthusiast magnam's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by reldapimp View Post
    dont forgot to fix your $q9 variable. there should not be an ' infront of it
    If fixed the q9 variable.

    I used reldapimp's code and again, it submits but it does not record to the database.

    Right now I have all surveys displaying on the main page:
    www.didjoo.com

    Only the two I submitted manually through phpmyadmin are displaying.

    Give it a shot:
    http://www.didjoo.com/sur.php


    my db name, username, password and sql host are all correct.

    I am connecting at the top of the page.

  6. #6
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    add
    PHP Code:
    mysql_query("the query goes here") or die(mysql_error()); 
    See what is says. Chances are one or more of your values should be encapsulated in ' '
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  7. #7
    SitePoint Enthusiast magnam's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wheeler View Post
    add
    PHP Code:
    mysql_query("the query goes here") or die(mysql_error()); 
    See what is says. Chances are one or more of your values should be encapsulated in ' '

    It says:
    Parse error: syntax error, unexpected ';' in D:\hshome\servicep\didjoo.com\sur.php on line 235


    On:

    Code PHP:
    mysql_query("INSERT INTO `surveys` (title, desc, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q14, q15) VALUES ($title, $desc, $q1, $q2, $q3, $q4, $q5, $q6, $q7, $q8, $q9, $q10, $q11, $q12, $q13, $q14, $q15)" or die(mysql_error());

  8. #8
    SitePoint Enthusiast magnam's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  9. #9
    Team SitePoint santouras's Avatar
    Join Date
    Jul 2006
    Location
    planet earth
    Posts
    276
    Mentioned
    13 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    mysql_query("INSERT INTO `surveys` (title, desc, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q14, q15) VALUES ($title$desc$q1$q2$q3$q4$q5$q6$q7$q8$q9$q10$q11$q12$q13$q14$q15)") or die(mysql_error()); 
    my utility belt tells me its to the bar batman

    read the manual then google it then do a search THEN post....

  10. #10
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    With some nice formatting and relevant quotes:
    PHP Code:
    mysql_query("
        INSERT INTO 
            `surveys` 
        (
        title, 
        desc, 
        q1, 
        q2, 
        q3, 
        q4, 
        q5, 
        q6, 
        q7, 
        q8, 
        q9, 
        q10, 
        q11, 
        q12, 
        q13, 
        q14, 
        q15
        ) VALUES (
        '
    $title', 
        '
    $desc', 
        '
    $q1', 
        '
    $q2', 
        '
    $q3', 
        '
    $q4', 
        '
    $q5', 
        '
    $q6', 
        '
    $q7', 
        '
    $q8', 
        '
    $q9', 
        '
    $q10', 
        '
    $q11', 
        '
    $q12', 
        '
    $q13', 
        '
    $q14', 
        '
    $q15'
        )"
    ) or die(mysql_error()); 
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  11. #11
    SitePoint Enthusiast magnam's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by santouras View Post
    PHP Code:
    mysql_query("INSERT INTO `surveys` (title, desc, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q14, q15) VALUES ($title$desc$q1$q2$q3$q4$q5$q6$q7$q8$q9$q10$q11$q12$q13$q14$q15)") or die(mysql_error()); 
    That code above tells me:

    Code PHP:
    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 'desc, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q

  12. #12
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Staffordshire, UK & Florida, USA
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DESC is a reserved word, so you need to enclose it in backticks:
    INSERT INTO `surveys` (title, `desc`, ...) etc.

  13. #13
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by magnam View Post
    I am trying to submit surveys to my database, like this one for example:
    http://didjoo.com/sur.php

    (the Q & A's are just for fun, examples)

    Code PHP:
    mysql_query("INSERT INTO `surveys` (id, title, desc, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q14, q15) VALUES ('0', $title, $desc, $q1, $q2, $q3, $q4, $q5, $q6, $q7, $q8, '$q9, $q10, $q11, $q12, $q13, $q14, $q15)");
    First of all, your DB structure is wrong. What if you've got a survey with more than 15 questions? And all surveys with less than that will only take up space in the database. You actually need two tables: surveys(id, title, description) and questions(survey_id, ordinal_number, title).

  14. #14
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    if you repeatedly get unexpected results from a query you are trying to run via php, here's a suggestion:

    echo the sql statement, and copy/paste it directly into mysql

    this will make errors like the omission of quotes around strings very obvious



    oh, and stereofrog is right, you might consider properly normalizing the table design
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •