Results 1 to 7 of 7

Thread: MySQL Colum error?

  1. #1
    Join Date
    Oct 2007
    Posts
    53
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default MySQL Colum error?

    Hi, I wrote a script that grabs a user's text and adds it to a database... or so I thought I did.

    When it runs, I get a MySQL error:
    Error in query: Unknown column 'Type your post here.' in 'field list'

    Any help?

    PHP File:
    PHP Code:
    <?php

    // If the form hasent been filled and submited, then display it.
    if (!isset ($_POST['submit'])) { 

    ?>

    <form action="<?php $_SERVER['PHP_SELF'?>" method="post">
      <div align="center"><textarea name="post" rows="15" style="width:600px;">Type your post here.</textarea><br />
      <input name="submit" type="submit" value="Post">
      </div>
    </form>

    <?php

    // End form.
    }

    // If the post form has been submited, add it to the database and show all posts.
    if (isset ($_POST['submit'])) {

    // Open connection.
    $connection mysql_connect(localhostshadowte_shadowtikicat) or die ('Unable to connect!');

    // Select database.
    mysql_select_db(shadowte_cmstest1) or die ('Unable to select database!');

    // Get the post that the user submited...
    $post $_POST['post'];

    // Add slashes if Magic Quotes are off. (they should be)
    if(!get_magic_quotes_gpc())
    {
    $post addslashes($post);
    }

    // Add this post to the database.
    $addpost 'INSERT INTO posts (input) VALUES (`'.$post.'`)';

    // Execute addition.
    mysql_query($addpost) or die ('Error in query: '.mysql_error());

    // End posts.
    }

    ?>
    Example:
    http://www.shadowtechstudios.net/sha...stem/0.0.1.php

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Change this line:

    Code:
    $addpost = 'INSERT INTO posts (input) VALUES (`'.$post.'`)';
    to:

    Code:
    $addpost = "INSERT INTO posts `input` VALUES ('".$post."')";
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. The Following User Says Thank You to thetestingsite For This Useful Post:

    AmenKa (07-30-2008)

  4. #3
    Join Date
    Oct 2007
    Posts
    53
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Error in query: 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 '`input` VALUES ('Type your post here.')' at line 1

    the line is now exactly as you suggested, but for some reason the MySQL thinks there is an extra '?

  5. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $addpost = "INSERT INTO posts (input) VALUES ($post)";
    Also, use mysql_real_escape_string(), not just addslashes(). addslashes() will not catch some potentially dangerous MySQL constructs.
    Last edited by Twey; 07-30-2008 at 10:33 AM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. The Following User Says Thank You to Twey For This Useful Post:

    AmenKa (07-30-2008)

  7. #5
    Join Date
    Oct 2007
    Posts
    53
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    new error:
    Error in query: Unknown column '$post' in 'field list'

    ill escape that too, thanks for that

    current code:

    PHP Code:
    <?php

    // If the form hasent been filled and submited, then display it.
    if (!isset ($_POST['submit'])) { 

    ?>

    <form action="<?php $_SERVER['PHP_SELF'?>" method="post">
      <div align="center"><textarea name="post" rows="15" style="width:600px;">Type your post here.</textarea><br />
      <input name="submit" type="submit" value="Post">
      </div>
    </form>

    <?php

    // End form.
    }

    // If the post form has been submited, add it to the database and show all posts.
    if (isset ($_POST['submit'])) {

    // Open connection.
    $connection mysql_connect(localhostshadowte_shadowtikicat) or die ('Unable to connect!');

    // Select database.
    mysql_select_db(shadowte_cmstest1) or die ('Unable to select database!');

    // Get the post that the user submited...
    $post $_POST['post'];

    // Remove slashes if Magic Quotes are on.
    if(get_magic_quotes_gpc())
    {
    $post stripslashes($post);
    }

    // Make the post MySQL safe.
    $post mysql_real_escape_string($post);

    // Add this post to the database.
    $addpost 'INSERT INTO posts (input) VALUES ('.$post.')';

    // Execute addition.
    mysql_query($addpost) or die ('Error in query: '.mysql_error());

    // Close connection.
    mysql_close($connection);

    // End posts.
    }

    ?>
    and back to this error:

    Error in query: 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 '`input` VALUES ('Type your post here.')' at line 1
    Last edited by AmenKa; 07-30-2008 at 04:09 AM.

  8. #6
    Join Date
    Oct 2007
    Posts
    53
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    I actually edited this to be somewhat more complex and it is working now strangely enough...

  9. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes, I accidentally merged two forms of the code that I was having difficulty choosing between. Those should be double quotes, not single; edited to prevent reader confusion.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •