MySql Insert Statement

Discussion in 'Databases' started by jaguarx, Sep 17, 2009.

  1. #1
    This is a pretty dumb question. But I'm stucked.

    I use this statement to add a new record to the dababase:

    {
    $query = 'INSERT INTO w_table (maker, series, model, part)
    VALUE ($maker, $series, $model, $part);
    mysql_query($query);
    echo mysql_error();
    }

    $maker, $series, $model and $part are retrieved from a file. Mysql_error ()
    always complain that "Unknown column $maker in the fieldlist". The only to stop this error is to use the acutal values intead of variables inside the VALUE clause. What did I do wrong?
     
    jaguarx, Sep 17, 2009 IP
  2. jaguarx

    jaguarx Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I figured it out.

    The variabble names need to be bwteen quotes. So that right way is

    VALUES ('$maker', '$series', '$model', ...)

    instead of VALUES ($maker, $series, $model, ...)

    I don't know why we need the qutation marks from parsing view point. But I don't want to argue with success.


    Warren
     
    jaguarx, Sep 17, 2009 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    The reason you need them is so the database interprets the data you are inserting as a string.

    Something like inserting a number into an INT field, or using a MySQL function like DATE() would work without them, but MySQL doesn't know to handle the data as a string unless you tell it to.
     
    jestep, Sep 18, 2009 IP
  4. jkapadia1983

    jkapadia1983 Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I think you have prob with datatype... for string you should use single quotations and for int you can write values without quotations
     
    jkapadia1983, Sep 30, 2009 IP
  5. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #5
    I am not sure if the problem is solved or not, but correct way of writing the query would be..

    $query = "INSERT INTO w_table (maker, series, model, part)
    VALUE ('$maker', '$series', '$model', '$part')";
     
    mastermunj, Oct 20, 2009 IP
  6. winaso97

    winaso97 Member

    Messages:
    50
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #6
    $query = "INSERT INTO w_table (maker, series, model, part)
    VALUE ($maker, $series, $model, $part)";
    should be VALUES
    and need the qutation marks '$maker', '$series', '$model', '$part'

    >> $query = "INSERT INTO w_table (maker, series, model, part) VALUES ('$maker', '$series', '$model', '$part')";

    CMIIW
     
    winaso97, Oct 22, 2009 IP