To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
[RESOLVED] mysql_query: INSERT does not insert anything at all...
Hi there,
I am able to read from my database table, but I am not able to write.
As far as I can see, my code is correct, and my database table has been set up correctly. However, when I test my script, nothing is inserted into my table. I have no idea what is wrong.
Can anyone figure out what is missing or incorrect in my SQL query?
My script is as follows:
PHP Code:
include("archive/file.inc");
$connection = mysql_connect($host, $account, $password)
or die(mysql_error());
$db = mysql_select_db($dbname, $connection)
or die(mysql_error());
Anything in the PHP error log, in case display_errors is turned off in your config?
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett freelancer.internet.com Email me
$result = mysql_query($create)
or die("Error: ".mysql_error());
mysql_close($connection);
Here is the error message from SQL:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key, name, gender, dob, area) VALUES ('1257580509', '', '111.222.333.444', 'aa', ' at line 1
I wonder if it is related to my script, or to the values I try to enter? I am not strong at SQL. The MySQL server version is 5.0.
I now suspect that there is a problem with the way I handle date() and time() and the way I attempt to store these values. It seems that a value based on the time() function becomes empty and returns an error when trying to insert it into a NOT NULL field.
Don't laugh if the following is complete nonsense; at least then that is where my error is to be found:
PHP Code:
$dob = (int)$year.$month.$day;
$subscr_date = (int)time('Ymd');
$subsrc_time = (int)time('Hi');
$dob should become YYYYmmdd (e.g. 19600704)
$subscr_date should become YYYYmmdd (e.g. 20091107)
$subscr_time should become HHii (e.g. 1705)
For some reason, $subscr_time becomes NULL.
Also, in the SQL query, I have removed the quotations around the numeric values:
I tried to fix the script for the values $subscr_date and $subscr_time.
Now, $subscr_time is no longer NULL, so that was not the only problem. The values become 10 digits long, while the coloumns are INT (8) each. Perhaps this is generating the syntax error now, and further work with thos two values are required.
My first thought is that those fields should be DATE or TIME column types, not integers. If so, then the values you would insert would be strings ('2009-02-31' or '12:51') instead of being cast to integers. (In fact, I would just create a single DATETIME column for subscription date/time.)
Anyway, if you want to continue with the current scheme, use date() instead of time() to get the values for the current date/time.
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett freelancer.internet.com Email me
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key, name, gender, dob, area) VALUES (20091107, 1239, '123.123.123.123', 'aa', 'i' at line 1
I also found a related mistake, that I used $password for both the MySQL login and for the user password.
Once the script and query worked, I wondered why it stored the same password every time, although I tested with different password - and it was not stored as a 32-digit hex, either. Then I finally noticed that it was my own MySQL password, and when I looked at my script again, I knew I had to rename the password value from the form input to something else, and made it $userpw instead:
PHP Code:
include("archive/file.inc");
$connection = mysql_connect($host, $account, $password)
or die("Error: ".mysql_error());
$db = mysql_select_db($dbname, $connection)
or die("Error: ".mysql_error());