Grow your CSS skills. Land your dream job.

PHP Syntax Error – Please Help!

  • # February 25, 2012 at 7:30 pm

    I’m basically doing a submit form, and for some extremely irritating reason, it won’t work! I’m inserting values into my database which looks like this:

    id - int(11) primary key auto
    name - varchar(100)
    s_info - varchar(200)
    info - text
    type - varchar(200)
    by - varchar(200)
    published - varchar(50)
    codelink - varchar(500)

    Now here is my form action page:

    < ?php
    session_start();
    $username = $_SESSION;
    $password = $_SESSION;
    $connect = mysql_connect(".....");
    $select_db = mysql_select_db(".....", $connect);

    $name = $_REQUEST;
    $s_info = $_REQUEST;
    $info = $_REQUEST;
    $type = $_REQUEST;
    $by = $_REQUEST;
    $codelink = $_REQUEST;

    $published = "no";
    $category = "usersubmitted";
    if ($username == "Schart"){
    $category = "original";
    }

    $insert = mysql_query("INSERT INTO apps (name, s_info, info, category, type, by, published, codelink) VALUES ('$name', '$s_info', '$info', '$category', '$type', '$by', '$published', '$codelink')");
    if (!$insert){
    die("Error: " .mysql_error());
    }
    ?>

    This is the error I get:

    Error: 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 'by, category) VALUES ('no', 'App', 'Schart Clock', 'A nice clock to add to your ' at line 1

    This is extremely frustrating! Any suggestions? Please help.

    Thank’s :)

    # February 25, 2012 at 7:41 pm

    All the values need to match up and be in the same order
    The error message suggests the code you are running is slightly different to whats posted

    # February 25, 2012 at 8:09 pm

    Can you see anything wrong with the code ?

    # February 25, 2012 at 8:12 pm

    I have gone trough the code several times, even redone it :/

    # February 25, 2012 at 8:16 pm

    Can you try this?

    $insert = mysql_query("INSERT INTO apps
    ('name', 's_info', 'info', 'category', 'type', 'by', 'published', 'codelink') VALUES
    ('$name', '$s_info', '$info', '$category', '$type', '$by', '$published', '$codelink')");

    In the meantime I’m going to setup a database and try it myself

    # February 25, 2012 at 8:23 pm

    Thank’s but it didn’t work.



    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 ''name', 's_info', 'info', 'category', 'type', 'by', 'published', 'codelink') VAL' at line 2

    # February 25, 2012 at 8:39 pm

    This worked for me, inside the ‘ is a ” to escape back to PHP and the . to append

    INSERT INTO `apps`(`name`) VALUES ('".$name."')

    so its ‘ ” . $name . ” ‘

    # February 25, 2012 at 8:49 pm

    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 ”apps’ (‘name’, ‘s_info’, ‘info’, ‘category’, ‘type’, ‘by’, ‘published’, ‘codeli’ at line 1

    :/… Really thought that one would help. I see the logic in it

    # February 25, 2012 at 8:54 pm

    :/ I gotta hit the hay

    Try it with one value at a time to narrow it down
    It could be an issue with one of the values you are inserting
    Keep at it and you’ll get there :)

    btw take a look into PDO

    http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

    # February 25, 2012 at 9:00 pm

    That is a very good idea! Whether or not it work’s I’m going to close this thread and try to figure it out. Thank’s for your great support, you have surely helped me a lot recently! Seriously, is there any website of yours I can promote or anything?

    # February 26, 2012 at 6:27 am

    You can follow me on twitter if you like http://twitter.com/karlpcrowley
    Have a few sites in the works myself but don’t wanna promote them yet :)

    # February 26, 2012 at 8:32 am

    OK – If you ever need something, I’m right here :)

    Rob
    # March 1, 2012 at 4:06 am

    Just looking at this:

    ...('$name', '$s_info', '$info', '$category', '$type', '$by', '$published', '$codelink')");

    and what Karl tried

    INSERT INTO `apps`(`name`) VALUES ('".$name."')

    Notice that there was no concatenation in the first one. You need to break out of your “string” to insert the vars. The first part should look like Karls basically.

    ...('" . $name . "', '" . $info . "' ..... 

    You could, because you are using ” ” (double quotation marks) wrap each var in { } so…

    ...('{$name}', '{$s_info}', '{$info}', '{$category}', '{$type}', '{$by}', '{$published}', '{$codelink}')");

    Other than that – I can’t really see a problem with your actual SQL itself…

    # March 2, 2012 at 6:28 am

    I think it works! Thank’s :)

    Rob
    # March 2, 2012 at 9:50 am

    Excellent :)

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".