Welcome to WebmasterWorld Guest from 54.198.237.87

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Inserting into datetime column with PHP

Works in PHPMyAdmin.. not through PHP page

     
11:57 am on Mar 15, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Nov 17, 2002
posts:313
votes: 0


Hi all,

I have a column in a 'users' table with the following properties.

Type: datetime
Null: No
Deafult: 0000-00-00 00:00:00

When I want to add a record into the table through PHPMyAdmin, I click 'insert' and the current date & time appear in the text field, and it enters correctly, and the user works fine.

However, when I want to add a user via a PHP-web form (which is how it *needs* to be), it just enters in 0000-00-00 00:00:00 with no data, which means the user can't login :(

I have tried all sorts of different insert strings...none seem to work.
I have set it so that no value is entered, I have also tried having the current date/time passed to the php-processor page, and tried entering that as a value - still not working.

Any ideas? of how I can get this to work? Any help will be greatly appreciated.

Regards,
William.

12:25 pm on Mar 15, 2003 (gmt 0)

New User

10+ Year Member

joined:Mar 1, 2003
posts:19
votes: 0


Hi!
Send us the relevant part of the PHP code you're using, including your mySQL query.
So we can help you more easily.

Jocelyn

1:31 pm on Mar 15, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 14, 2002
posts:118
votes: 0


You can either use MySQL's NOW() function, or PHP's date() function.
I think the syntax for using date is: date ("Y-m-d H:m:s");

Check the manuals.

Allen

2:28 pm on Mar 15, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Nov 17, 2002
posts:313
votes: 0


Hi,

Thanks for the replies..I had tried the NOW() & Date feature..neither work!
Below is the part of my code which handles the adding:

} elseif ($action == adddealer) {
$newuser = addslashes($HTTP_GET_VARS['username']);
$newname = addslashes($HTTP_GET_VARS['name']);
$newemail = addslashes($HTTP_GET_VARS['email']);
$newpass = addslashes($HTTP_GET_VARS['password']);
$level = addslashes($HTTP_GET_VARS['level']);

$sql = "INSERT INTO users (id,username,password,email,lost_key,lost_date,level,dealer) ";
$sql = $sql . " VALUES ('','$newuser',MD5($newpass),'$newemail','0','NOW()','$level','$newname') ";
$result = mysql_query($sql);
print "Your request to add a user was successful";

Your thoughts would be greatly appreciated.

Regards,
William

2:41 pm on Mar 15, 2003 (gmt 0)

New User

10+ Year Member

joined:Mar 1, 2003
posts:19
votes: 0


Try that :
$sql = "INSERT INTO users (username,password,email,lost_key,lost_date,level,dealer) ";
$sql = $sql . " VALUES ('$newuser',MD5($newpass),'$newemail','0',NOW(),'$level','$newname') ";

The id field is probably set as autoincrement, so you don't need it in your query.
Notice that there must not be any quotes around NOW() in your query. Check with phpMyAdmin.
Also, to check for mysql errors in the PHP code, you can do so :
mysql_query($query) or die('Mysql error:' . mysql_error());

Sorry for the layout, I'm not familiar with BB code yet.

Jocelyn

3:34 pm on Mar 15, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Nov 17, 2002
posts:313
votes: 0


Hi,

Thanks for the info - still no joy :(.
Here is the error that I receive:

ERROR: Unknown column 'test' in 'field list' INSERT INTO arctic_dealers (username, password, email, lost_key, lost_date, level, dealer) VALUES ('test',MD5(test),'test@email.com','0',NOW(),'1','Test')

I have realised that it is saying it can't find the column, called 'whatever I enter into the password box'.

Any ideas, how to stop this?

Regards,
William

3:40 pm on Mar 15, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Nov 17, 2002
posts:313
votes: 0


Sorted!

I noticed that I was inserting: MD5($newpass), and I just changed it to MD5('$newpass') and it is working fine.

Thanks to all.