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
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Newbies

Newbies Help for those who are just getting started

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-06-2009, 09:12 PM   #1
Copenhagener
Junior Member
 
Join Date: Nov 2009
Posts: 21
resolved [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());

            
$query = "INSERT INTO users (subscr_date, subscr_time, subscr_ip, signature, password, email, key, name, gender, dob, area) VALUES ('$subscr_date', '$subscr_time', '$subscr_ip', '$signature', '$password', '$email', '$key','$name', '$gender', '$dob', '$area')";

            
$result = $mysql_query($query)
                or die(
mysql_error());

            
mysql_close($connection);
PHP version: 5.2
MySQL version: 5.0

Thank you very much in advance for any help.
Copenhagener is offline   Reply With Quote
Old 11-06-2009, 09:24 PM   #2
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 11,752
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
NogDog is offline   Reply With Quote
Old 11-07-2009, 04:03 AM   #3
Copenhagener
Junior Member
 
Join Date: Nov 2009
Posts: 21
Rewrote the script to get some errors:

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());

            
$create = "INSERT INTO users (subscr_date, subscr_time, subscr_ip, signature, password, email, key, name, gender, dob, area) VALUES ('$subscr_date', '$subscr_time', '$subscr_ip',

'$signature', '$password', '$email', '$key','$name', '$gender', '$dob', '$area')"
;

            
$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.

Can anyone spot the mistake here?

Thanks a lot in advance.
Copenhagener is offline   Reply With Quote
Old 11-07-2009, 05:17 AM   #4
Copenhagener
Junior Member
 
Join Date: Nov 2009
Posts: 21
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:


PHP Code:
            $create = "INSERT INTO users (subscr_date, subscr_time, subscr_ip, signature, password, email, key, name, gender, dob, area) VALUES ($subscr_date, $subscr_time, '$subscr_ip',

'$signature', '$password', '$email', '$key','$name', '$gender', $dob, '$area')"
;

dob is INT (8) NOT NULL
subscr_date is INT (8) NOT NULL
subscr_time is INT (4) NOT NULL


I think I am approaching a solution, but any inputs or ideas are still more than welcome, as I have been stuck for hours with this.

Thanks..
Copenhagener is offline   Reply With Quote
Old 11-07-2009, 05:23 AM   #5
Copenhagener
Junior Member
 
Join Date: Nov 2009
Posts: 21
While testing some example results, I get:

$dob >> 19900304 (correct; 4 March 1990)

$subscr_date >> 1257584137 (something is definitely wrong here)

$subscr_time >> NULL
Copenhagener is offline   Reply With Quote
Old 11-07-2009, 06:30 AM   #6
Copenhagener
Junior Member
 
Join Date: Nov 2009
Posts: 21
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.
Copenhagener is offline   Reply With Quote
Old 11-07-2009, 07:26 AM   #7
NogDog
High Energy Magic Dept.
 
NogDog's Avatar
 
Join Date: Aug 2006
Location: Ankh-Morpork
Posts: 11,752
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
NogDog is offline   Reply With Quote
Old 11-07-2009, 07:51 AM   #8
Copenhagener
Junior Member
 
Join Date: Nov 2009
Posts: 21
Thanks for your inputs, MotDog.

I rewrote my script with regards to the date and time, and now at least I get the right values:

PHP Code:
    $time_array = getdate();

    foreach(
$time_array as $item => $val ) {
        
$item = $val;
    }

    
$thisYear = $time_array[year];
    
$thisMonth = $time_array[mon];
    
$thisDay = $time_array[mday];
    
$thisHour = $time_array[hours];
    
$thisMin = $time_array[minutes];

    if(
$thisMonth < 10) $thisMonth = "0".$thisMonth;
    if(
$thisDay < 10) $thisDay = "0".$thisDay;
    if(
$thisHour < 10) $thisHour = "0".$thisHour;
    if(
$thisMin < 10) $thisMin = "0".$thisMin;

    
$subscr_date = (int)$thisYear.$thisMonth.$thisDay;

    
$subscr_time = (int)$thisHour.$thisMin;
However, I still get a MySQL error:

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

My SQL query again:

PHP Code:
$create = "INSERT INTO users (subscr_date, subscr_time, subscr_ip, signature, password, email, key, name, gender, dob, area) VALUES ($subscr_date, $subscr_time, '$subscr_ip', '$signature', '$password', '$email', '$key','$name', '$gender', $dob, '$area')";
I store the md5'ed password as VARCHAR (32) is that correct?

Anything else that could generate the error?

I hoped I had found the problem, but appearantly it was not enough...
Copenhagener is offline   Reply With Quote
Old 11-07-2009, 09:28 AM   #9
Lars Berg
Senior Member
 
Join Date: Aug 2002
Location: Uppsala
Posts: 774
key is a reserved word in Mysql. Rename column or escape it as `key`
Lars Berg is offline   Reply With Quote
Old 11-07-2009, 09:45 AM   #10
Copenhagener
Junior Member
 
Join Date: Nov 2009
Posts: 21
resolved

Wow! Thanks, Lars - that was really the KEY to the whole problem!

Honestly, I would never had realised that I was using a reserved term.

Here is my final script and query which WORKS:

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());

            
$create = "INSERT INTO users (subscr_date, subscr_time, subscr_ip, signature, password, email, token, name, gender, dob, area) VALUES ($subscr_date, $subscr_time, '$subscr_ip', '$signature', '$password', '$email', '$token', '$name', '$gender', $dob, '$area')";

            
$result = mysql_query($create)
                or die(
"Error: ".mysql_error());

            
mysql_close($connection);
And the coloumn "key" has been renamed to "token" in the database table, being a VARCHAR (8).

Thank you, Lars.
Copenhagener is offline   Reply With Quote
Old 11-07-2009, 11:26 AM   #11
Copenhagener
Junior Member
 
Join Date: Nov 2009
Posts: 21
resolved

An important note to the solution:

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());

            
$create = "INSERT INTO users (subscr_date, subscr_time, subscr_ip, signature, userpw, email, token, name, gender, dob, area) VALUES ($subscr_date, $subscr_time, '$subscr_ip',

'$signature', '$userpw', '$email', '$token', '$name', '$gender', $dob, '$area')"
;

            
$result = mysql_query($create)
                or die(
"Error: ".mysql_error());

            
mysql_close($connection);
At the same time, in MySQL I renamed the coloumn 'password' to 'userpw'.

And now everything works correctly.
Copenhagener is offline   Reply With Quote
Reply

Bookmarks

Tags
insert, mysql, mysql_query, php


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 04:53 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.