treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] PHP Syntax Error - Please Help!

  • 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['username'];
    $password = $_SESSION['password'];
    $connect = mysql_connect(".....");
    $select_db = mysql_select_db(".....", $connect);

    $name = $_REQUEST['name'];
    $s_info = $_REQUEST['shortinfo'];
    $info = $_REQUEST['info'];
    $type = $_REQUEST['type'];
    $by = $_REQUEST['by'];
    $codelink = $_REQUEST['appurl'];

    $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 :)
  • 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
  • Can you see anything wrong with the code ?
  • I have gone trough the code several times, even redone it :/
  • 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
  • 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

  • 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 . " '
  • 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
  • :/ 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/
  • 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?
  • 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 :)
  • OK - If you ever need something, I'm right here :)
  • 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...
  • I think it works! Thank's :)
  • Excellent :)