Adding data to database

Discussion in 'PHP' started by bunny123, Feb 25, 2007.

  1. #1
    I cannot add data to a database
    This is the code im using

    <?php
    $con = mysql_connect("localhost","db","pass");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    echo "could not connect";
    }
    else
    {
    echo "connected";
    }

    @mysql_select_db("db", $con);
    mysql_query("INSERT INTO Prods (Product_ID, Department, Name, Prod_code, Description, Picture, Price, Stock, Prod_Type)
    VALUES (null, varA, varB, varC, varD, varE, varF, VarG, varH);");
    mysql_close($con);


    ?>

    Any ideas?
    Thanks
     
    bunny123, Feb 25, 2007 IP
  2. keiths

    keiths Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    what is varA, B, C, etc.? even if they're variables they should be $varA, $varB. and if they're text they should be in single quotes.
     
    keiths, Feb 25, 2007 IP
  3. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Already told you in a different thread to use

    mysql_query($bla) or die(mysql_error());
     
    Icheb, Feb 25, 2007 IP
  4. bunny123

    bunny123 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ya your right thanks
    they are variables but even with $ in front of them,nothing is entering the database and no errors are showing either
     
    bunny123, Feb 25, 2007 IP
  5. keiths

    keiths Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Take out the semicolon in the query and do this:

    
    $sql = "INSERT INTO Prods (Product_ID, Department, Name, Prod_code, Description, Picture, Price, Stock, Prod_Type)
    VALUES (null, '". $varA ."', '". $varB ."', '". $varC ."', '". $varD ."', '". $varE ."', '". $varF ."', '". $VarG ."', '". $varH ."')";
    echo $sql;
    mysql_query($sql);
    
    Code (markup):
    Then see what it echos
     
    keiths, Feb 25, 2007 IP
  6. bunny123

    bunny123 Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    got it working
    thanks a million!
     
    bunny123, Feb 25, 2007 IP
  7. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    mysql_query("INSERT INTO Prods (`Product_ID`,` Department`, `Name`, `Prod_code`, `Description`, `Picture`, `Price`, `Stock`, `Prod_Type`)
    VALUES (null, 'varA', 'varB', 'varC', 'varD', 'varE', 'varF', 'VarG', 'varH')");


    Try This
     
    jitesh, Mar 5, 2007 IP