2

I'm sure this question has been asked a thousand times but after an hour of truly trying many examples on the web, I have failed to insert new data into my table. I have tried many methods as I said, the one I'm about to post is most recent. If anyone knows why my code is failing it would save so much stress. I have only so far managed to insert data via phpmyadmin. The database is called "test" and the table is called "getting". Please note that "key" is auto incremented.

Thank you

$username='****';
$password='****';
$database='test';



    $con= mysql_connect("localhost",$username,$password);

     mysql_select_db("test",$con);




    mysql_query("INSERT INTO getting (Key, Date, amount, tax, Extra) 
    VALUES ('', 'sept 26 2008', '35653', '46', '454')");
2
  • 1
    have you tried to escape the key? like: (`key`, ...) Commented Jan 24, 2012 at 0:45
  • 1
    You should put between backticks keywords, format date as YYYY-MM-DD to be sure it's correctly taken and show us which error you are getting... Commented Jan 24, 2012 at 0:47

4 Answers 4

10

You should try

  1. put keywords between backticks
  2. format date as YYYY-MM-DD
  3. don't use quotes for numbers
  4. use NULL for auto-increment keys (you could also remove it from INSERT)
  5. perform error checking

Try this query

$res = mysql_query(
    "INSERT INTO getting (`Key`, `Date`, amount, tax, Extra) 
    VALUES (NULL, '2008-09-26', 35653, 46, 454)");
if (!$res) {
    die('Invalid query: ' . mysql_error());
} else  {
    // Do here what you need
}
Sign up to request clarification or add additional context in comments.

2 Comments

This worked! Thanks Marco. Will accept. I believe the issue was that I put quotes around amount, tax, and extra? Not sure though.
@Teddy13: if you perform error checking, an error will be printed on screen if query is wrong telling you which part is wrong, so making it easier to fix it! :)
1
mysql_query("INSERT INTO getting (Key, Date, amount, tax, Extra) 
    VALUES ('', 'sept 26 2008', '35653', '46', '454')") or die(mysql_error());

What does it say after execution? If there is an error in request - you will see it.

2 Comments

@Marco Sorry, I'm here for the second day only ) It is not really a question, it is a hint.
Don't worry, after you edited adding or die part it's OK :)
0

my guess would have been "4. use NULL for auto-increment keys" by marco too

Comments

0

I belive if the 'key' filed is autoincremented you may not even bother mentioning it in your insert statement. Something like this

INSERT INTO getting (Date, amount, tax, Extra) 
VALUES ('sept 26 2008', '35653', '46', '454')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.