Log in

View Full Version : register script problem



hosam
01-25-2012, 09:04 AM
well I have this problem with my script:
couldn't insert data!

well I use this check error message inside my code after the checking if data is inserted inside database or not.
anyway the code has two part html,php
the html is used to handle form and show registration process and the php to handle the registration part.
the data base I have called fleet at my localhost server with this table users, users has rows (id,username, password, firstname,lastname,dat_reg)
sorry for taking too long to explain everything but I need help to resolve the database error shown at top
here html file code:


<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Registration Page</title>
<link rel="stylesheet" href="stylesheets/foundation.css">
<link rel="stylesheet" href="stylesheets/app.css">
<link rel="stylesheet" href="formly.css">
</head>

<body>
<div class="row">
<h1>Registration page</h1><br/><br/>

<form action='register.php' method='post' class="nice" id="ContactInfo" >

<label>Enter Username:</label>
<input type='text' name='username' class="medium input-text" />
<label for="password">Enter Password:</label>
<input type='password' name='password' class="medium input-text" title="Try to make it hard to guess."/>
<label>Repeat Password:</label>
<input type='password' name='repeatpassword' class="medium input-text"/>
<label>Enter firstname:</label>
<input type='text' name='firstname' class="medium input-text"/>
<label>Enter lastname:</label>
<input type='text' name='lastname' class="medium input-text"/>
<input type='submit' name='submit' value='register' class="white radius nice button"/>

</form>
</div>
</body>
</html>


and the register.php code:



<?php
//include("config.php");
//form data for registration
$firstname = strip_tags($_POST['firstname']);
$lastname = strip_tags($_POST['lastname']);
$username = strtolower(strip_tags($_POST['username']));
$password = strip_tags($_POST['password']);
$repeatpassword = strip_tags($_POST['repeatpassword']);
$date = ("y-m-d");
$submit = $_POST['submit'] ;
if(isset($submit)){
if($firstname&&$username&&$password&&$repeatpassword){
//check if password = repeatpassword
if($password == $repeatpassword){
//check username length
if(strlen($username)>25||strlen($username)<3){
echo"the allowed username length between 3 and 25!";
}//end username check length
//start else for username length
else{
//checking password length
if(strlen($password)>25||strlen($password)<5){
echo"your password must be between 5 and 25!";
}//end checking password length
//start else for checking password length
else{
//encrypt the passwords
$password = md5($password);
$repeatpassword = md5($repeatpassword);
$connect= mysql_connect("localhost","root","")or die("could not connect database!");
mysql_select_db("fleet",$connect)or die("couldn't select database!");
$queryreg = "INSERT INTO users (id,username,password,firstname,lastname,date_reg) VALUES ('0','".$username."','".$password."','".$firstname."','".$lastname."','".$date."')";

mysql_query($queryreg , $connect)or die("couldn't insert data!");
echo "registration sucessful! back to member page <a 'href='member.php'>here</a>";
}//end else for checking password length
}//end else for username length
}//end check if password = repeatpassword
else{
echo"password and repeatpassword not match!";
}
}//end of checking fields
else
echo"Please fill in all feilds!";
}

?>

ankush
01-25-2012, 09:30 AM
this is working fine, check again or may be you mis spelled your table name.

hosam
01-25-2012, 09:53 AM
well I don't know if it good or not, but what I'm sure the wrong part of my code only at the query part so I've double check the database table and test with phpmyadmin its working but inside this code isn't why that what I wish to know!!

hosam
01-26-2012, 09:45 AM
I found the problem now its wit $dat var when I delete it from both database and use comment the problem solved:
//$date = ("y-m-d");
but I wish to know how to make this var working so I could know when the user register, I read about it at php.net but it hard for me to understand how to make insert this kind of data inside the database and what I should use as row type.

traq
01-26-2012, 09:17 PM
is your dat_reg field a DATETIME (http://dev.mysql.com/doc/refman/5.0/en/datetime.html)type? If so, Y-m-d is not a valid format. You need to use a format like YYYY-MM-DD HH:MM:SS.

alternatively, if all you want is the current date/time, use mysql's NOW() function (or UTC_TIMESTAMP(), which is beneficial if you deal with multiple timezones).

hosam
01-27-2012, 12:25 AM
thank you very much for those information anyway if I want to do this dat_reg set at mysql database as DATETIME type then use this var:
$date = "YYYY-MM-DD HH:MM:SS";
then use this query:
$queryreg = "INSERT INTO users (id,username,password,firstname,lastname,date_reg) VALUES ('0','".$username."','".$password."','".$firstname."','".$lastname."','".$date."')"
does it going to work and what the data will show in database field if you can show me I really appriciate your help cause still facing problem with the date type I was use in date_reg DATE type but doesn't work with $date = "y-m-d";

traq
01-27-2012, 03:17 AM
a few options:
<?php
// this gives you a MySQL timestamp based on the current time _on_your_server_
// (this is how to do it according to your current approach)
$date = date( 'Y-m-d H:i:s' );
$query = "INSERT INTO `table` (`date`) VALUES ('$date')";

// this gives you a MySQL timestamp based on the current time _on_your_database_server_
$query = "INSERT INTO `table` (`date`) VALUES (NOW())";

// this gives you a MySQL timestamp based on the current _utc_time_
// (this is usually the best option:
// you can reliably compare times that came from different timezones,
// and you can still interpret the date to your local timezone when you need to.)
$date = date( 'Y-m-d H:i:s' );
$query = "INSERT INTO `table` (`date`) VALUES (UTC_TIMESTAMP())";

hosam
01-27-2012, 03:51 AM
thank you very much for those great information now I could do it correctly and know the user date of registration I also add thank :)