<form name="register" action="register.php" method="post"> Username: <input type="text" name="username" /> Password: <input type="password" name="pass1" /> Password Again: <input type="password" name="pass2" /> E-mail: <input type="text" name="email" /> <input type="submit" value="Register" /> </form>
Ok so we have a basic registration form, now to add the data to a database (using mysql in my case) we would have to create a TABLE. For example:
CREATE TABLE users ( id INT NOT NULL AUTO_INCREMENT, username VARCHAR(15) NOT NULL UNIQUE, password VARCHAR(64) NOT NULL, email VARCHAR(64) NOT NULL, PRIMARY KEY(id) );
Or something similar... Ok so we create the TABLE, now that's fine but every time a user registers an account this code is there. Is it safe to be executed every time a user registers an account? Is there a better method for doing this, a better place to add the CREATE TABLE function to just run once?
I'll give you the full code I have for a better explanation.
register.php
<form name="register" action="register.php" method="post"> Username: <input type="text" name="username" /> Password: <input type="password" name="pass1" /> Password Again: <input type="password" name="pass2" /> E-mail: <input type="text" name="email" /> <input type="submit" value="Register" /> </form> <? $username=$_POST['username']; $pass1=$_POST['pass1']; $pass2=$_POST['pass2']; $email=$_POST['email']; if(strlen($username) > 15) header('Location: register.php'); if($pass1 != $pass2) header('Location: register.php'); if(strlen($email) > 64) header('Location: register.php'); mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="CREATE TABLE users ( id INT NOT NULL AUTO_INCREMENT, username VARCHAR(15) NOT NULL UNIQUE, password VARCHAR(64) NOT NULL, email VARCHAR(64) NOT NULL, PRIMARY KEY(id))"; mysql_query($query); mysql_close(); ?>
I'm still quite new to php but I am a bit confused about the reasoning and the tutorials are all the same they don't specify or explain why this function is run every time a user registers an account or if it is even safe. Perhaps y'all can enlighten me?