Page 1 of 1
Registration
Posted: Sat Apr 11, 2015 7:20 pm
by Rackdos
hi, im just trying to create a registration page now...but when i click register i cant put the data on the database. here is my code, can you tell me what i have to do, please?
Code: Select all
<?php
function connect(){
$conn=mysqli_connect("localhost","root","","game");
}
function protect($string){
return mysql_real_escape_string(strip_tags(addslashes($string)));
}
?>
Register
<br /><br />
<?php
if(isset($_POST['register'])){
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if(strlen($username) > 20){
echo "Username must be less than 20 characters!";
}elseif(strlen($password) > 100){
echo "E-mail must be less than 100 characters!";
}
}
?>
<br /><br />
<form action="register.php" method="POST">
Username: <input type="text" name="username"/><br />
Password: <input type="password" name="password"/><br />
E-mail: <input type="text" name="email"/><br />
<input type="submit" name="register" value="Register"/>
</form>
Re: Registration
Posted: Sat Apr 11, 2015 7:43 pm
by Sim
wrap protect around your $POST before assigning them for starters.
I would do your error reporting like this though:
Code: Select all
//I prefer this method: Notice I added <br> to your strings.
if(strlen($username) > 20){
$err = "Username must be less than 20 characters!<br>"; // assign error to a variable.
}
if(strlen($password) > 100){ //no else if, just another if
$err .= "E-mail must be less than 100 characters!<br>"; // notice this one has .= which means APPEND.
}
//if there is no error, $err is blank, hence no error
if(empty($err))
{
//insert your variables into database
// Reference to function to insert the username/password/ect.: http://php.net/manual/en/mysqli.query.php
// Refernece to query command your need to use: http://www.w3schools.com/sql/sql_insert.asp
}
else //ELSE: error was not blank, meaning ERROR
{
echo $err; //display ALL ERRORS at ONCE (YUMMY), very user friendly
}
This is a starter guide. You also need to check if the $_POST are not blank since you don't do that
Re: Registration
Posted: Sat Apr 11, 2015 7:52 pm
by Rackdos
thanks for your comment. but i dont know whats the code to place the data on the database.
Re: Registration
Posted: Sat Apr 11, 2015 8:13 pm
by Sim
http://php.net/manual/en/mysqli.query.php //function your use
http://www.w3schools.com/sql/sql_insert.asp //should help you get started on the QUERY needed to insert the record
Re: Registration
Posted: Sat Apr 11, 2015 11:53 pm
by Rackdos
i visited those links before...but still cant insert data in my database. dont know what code i must use. im a begginer and that site is really confusing to me. i have already created a page with,username, password and email, and a button that says register. i want to record data on my data base when i click on register...but it just refreshes the page. how do i save data on my data base?
Re: Registration
Posted: Sun Apr 12, 2015 4:25 pm
by Sim
From the first link:
http://php.net/manual/en/mysqli.query.php
It would use this function:
mysqli_query($conn, "SQL STATEMENT. THE QUERY");
From the 2nd Page:
http://www.w3schools.com/sql/sql_insert.asp
The Syntax:
Code: Select all
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
A demo from 2nd page:
Code: Select all
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
So combined you would have something like this:
Code: Select all
mysqli_query($conn, "INSERT INTO UserNameTable (userNameField, userPassField, userEmailField) VALUES ($username, $password, $email));
You will have a problem using the variable $conn in your function connect, as its only local which means outside functions can't access it.
Re: Registration
Posted: Sun Apr 12, 2015 11:16 pm
by Rackdos
i really apreciate your help, thanks. but i inserted your code
Code: Select all
mysqli_query($conn, "INSERT INTO UserNameTable (userNameField, userPassField, userEmailField) VALUES ($username, $password, $email));
and it gave me a sintaxe error on line 30, on post:method...i want to understand this, but im really noob

Re: Registration
Posted: Mon Apr 13, 2015 2:04 am
by Sim
Of course it's gonna give you an error =]
Its an example, not an actual working code. The end " was missing for starters. Second, I doubt any of my fields I used actually match your database scheme.
mysqli_query($conn, "INSERT INTO UserNameTable (userNameField, userPassField, userEmailField) VALUES ($username, $password, $email)");
Re: Registration
Posted: Mon Apr 13, 2015 8:05 am
by Rackdos
i put this code
Code: Select all
mysqli_query($conn, "INSERT INTO user (username, password, email) VALUES ($username, $password, $email)");
and it gave me that error...i really want to create a registration page but im stuck for 5 days on this. when i click register the data doesnt go to the database
Re: Registration
Posted: Mon Apr 13, 2015 11:50 am
by Rackdos
FINALLY i put the data on the data base. now i just want to make some restriction. how can i restrict the number os usernames to 1 and its length? i used this if its empty:
Code: Select all
<script language="javascript" type="text/javascript">
function submitreg() {
var form = document.reg;
if(form.username.value == ""){
alert( "Enter username." );
return false;
}
else if(form.password.value == ""){
alert( "Enter password." );
return false;
}
else if(form.email.value == ""){
alert( "Enter email." );
return false;
}
}
</script>