Jump to content

PhP writing date/time and birthdate to MySQL


fuzzylr
 Share

Recommended Posts

Greetings, I am really new at coding php. I have been working for a while and doing some Google searches. I am trying to make a registeration page for something I am working on. I can get everything in the database but dates & times. Right now it's all bogus information. I am curious on how to take a date (like a birthday) and write it to my MySql datebase? Every attempt I have made that is the only piece that doesn't get written. Also I am having an issue with the php date getting into the database. Html code

<!DOCTYPE HTML><html><head><title>Demo Game</title></head><body><h1>Test page</h1><p>This is a demo page.  More to come.</p><p>In the area below.  You will find a location for registration.  Please attempt to make an account.</p> <!-- Divide for form formatting --> <div id="container" style="width:250px"><!-- Form for registering new users into the game. --><form id="register" action="reg.php" method="post">	<fieldset> <legend>Registration</legend>		<div id="col1" style="width:75px; float:left;">			Username:<br>			Password:<br>			First name:<br>			Last name:<br>			Birthday:<br>			Email:		</div>		<div id="input" style="width:100px; float:left;">			<input type"text" name="username" placeholder="Username" required autofocus><br>			<input type="password" name="password" placeholder="Password" required><br>			<input type"text" name="fname" placeholder="First Name" required><br>			<input type"text" name="lname" placeholder="Last Name" required><br>			<input type="date" name="bday" placeholder="(MM/DD/YYYY)" required><br>			<input type="email" name="email" placeholder="Email address" required>		</div>				 <div id="gender" style="text-align:center; clear:both;">			Gender:			<select name="gender">				<option value="Male">Male</option>				<option value="Female">Female</option>			</select><br><br>		</div>		<input type="submit" value="Register">	</fieldset></form></div> </body></html>

php code

<?php// Variables//$Date = now();$User = "";$Password = "";$Database = "testgame";$Table = "players";$Host = "localhost";$sqlDate = date('Y-m-d H:i:s');  //Check to make sure variables are set from index.htmif(isset($_POST[username]) && isset($_POST[password]) && isset($_POST[fname]) && isset($_POST[lname])&& isset($_POST[bday]) && isset($_POST[email]) && isset($_POST[gender])){// Connect to the servermysql_connect($Host, $User, $Password) or die (mysql_error()); //Check connectivitymysql_select_db($Database) or die(mysql_error());  // Insert data into DB$insert = "INSERT INTO $Table (username, password, name, last, email, birthday, registered, gender) VALUES('$_POST[username]', '$_POST[password]', '$_POST[fname]', '$_POST[lname]', '$_POST[email]', '$_POST[bday]', '$sqlDate', '$_POST[gender]')"; //Check to see if the name exists (no duplicates)$result = mysql_query("SELECT * FROM $Table Where email = '$_POST[email]'"); echo $_POST[email]; if($result){	if(!mysql_query($insert)) { die (mysql_error()); echo "Record added. "; }}else { echo "Email already used."; }	mysql_close($con); }  //End of check ?>

Link to comment
Share on other sites

It depends which data types you're using in MySQL. If you have a datetime field, then you need to write the date in the format that MySQL expects for those fields. When I store dates I use integers in the database, and I store Unix timestamps. The time function returns a Unix timestamp, and the date function takes a timestamp to format. The getdate function will take a timestamp and return an array of information about it, and mktime will make a timestamp for a specific date and time. The strtotime function will convert most recognizable date strings to a timestamp.

Link to comment
Share on other sites

Ok, I have fixed the problem with posting the date times to the database. I have one last issue. I want the code to do a check in the DB to make sure the email does not already exist. here is my code. It either constantly denies the write or just always lets it through. I have a feeling it's not pulling data out of the database to compare to.

$result = mysql_query("SELECT * FROM $Table Where email = '$Email'");echo $result;if($result !== $Email){    mysql_query($insert);    header( 'location: complete.htm');}else { header( 'location: error.htm'); }

Link to comment
Share on other sites

Never mind. I figured it out. I needed to tranlate the data with mysql_fetch_array

$query = mysql_query("SELECT * FROM $Table Where email='$Email'");$query_row=mysql_fetch_array($query);if($query_row == NULL){    mysql_query($insert);    header( 'location: complete.htm');}else { header( 'location: error.htm'); }

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...