Why won't my insert into database function work

Discussion in 'PHP' started by Imozeb, Feb 10, 2010.

  1. #1
    I just created this code to imput data into my userdatabase. Why isn't it working. It just skips past the mysql_query, not entering the data into my database and then refreshes the page to index.html because of the header command. Why isn't it inserting my data?

    $name, $password, are gotten from an imput form and work perfectly with other parts of my code.

    $today = (date("m"). "/" . date("d") . "/" . date("Y"));
    mysql_select_db("userdatabase");
    //input data
    mysql_query("INSERT INTO usertable (name, pwd, datejoined)
    VALUES ('$name', '$password', '$today')");
    header("Location: http://www.example.com/index.html");

    Thanks in advance!

    ~imozeb :)
     
    Imozeb, Feb 10, 2010 IP
  2. thorie

    thorie Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You might want to see if there is a MySQL error getting spit out by temporarily removing the header() line. Most likely, it's the column name "name" which is a reserved word in MySQL. It's like calling a table "table". So, use `name` (backtick quotes) instead of just name and MySQL will understand.

    In fact, it's good to get in the habit of using backticks on all your tables and column names when you do a query.
     
    thorie, Feb 10, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Okay, I put backticks on my column names in the insert into part and deleted the header to see if there was an error message and it still does not work! :( There was no error message by the way. Please help.
     
    Imozeb, Feb 10, 2010 IP
  4. thorie

    thorie Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Make sure you have error reporting set to E_ALL and display_errors set to On.
    You should store the SQL query as a string and display it. Copy-and-paste that query and try it in mysql console or phpMyAdmin directly; eliminate PHP from being the culprit. Also, check the return value of mysql_query() as it may be telling you something.
     
    thorie, Feb 10, 2010 IP