What am I doing wrong?

Discussion in 'PHP' started by cocaine, Feb 28, 2013.

  1. #1
    Hello Guys, I just started learning PHP. I come from C++ background.

    I was looking into database stuff. I wrote following:


    <?php
    $name = $_REQUEST['name'];
    $faculty = $_REQUEST['faculty'];

    mysql_connect("localhost","","") or die("Can not connect to the database");
    mysql_select_db(test);

    $query=insert into test_table(name,faculty) values('".$name."','".$faculty."');
    mysql_query(query) or die("Can not post anything");

    echo "Data successfully posted to the database.";
    ?>


    It gives me following error.

    Parse error: syntax error, unexpected T_STRING in /Users/basantathapa/Sites/blahsubmit.php on line 14

    Any idea what am I doing wrong?

    Thanks
     
    cocaine, Feb 28, 2013 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    What's on line 14? the script you are providing doesn't have 14 lines!
     
    EricBruggema, Feb 28, 2013 IP
  3. cocaine

    cocaine Well-Known Member

    Messages:
    1,082
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    178
    #3
    $query=insert into test_table(name,faculty) values('".$name."','".$faculty."');

    Above is the 14th line.
     
    cocaine, Feb 28, 2013 IP
  4. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #4
    I would reccommend you don't go any further with that code, you shouldn't be using old mysql functions any more as it is soon to be deprecated, look into using PDO or mysqli, it's much more secure
     
    malky66, Feb 28, 2013 IP
  5. Darkfortune

    Darkfortune Greenhorn

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    Its should work now!

    <?php
    $name = $_REQUEST['name'];
    $faculty = $_REQUEST['faculty'];
     
    mysql_connect("localhost","","") or die("Can not connect to the database");
    mysql_select_db(test);
     
    $query="INSERT INTO `test_table` (`name`, `faculty`) VALUES ('".$name."', '".$faculty."')";
    $result = mysql_query($query) or die("Can not post anything");
     
    echo "Data successfully posted to the database.";
    ?>
    Code (markup):
    You forgot a few qoutes and the dollar sign before query.
     
    Darkfortune, Mar 1, 2013 IP