FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Auto create tables





ncwdavid
Hey,I made my own registration form today in a html document and it looks like this:
Quote:
<html>
<head>
<title>Register To Recieve Full Access To Our Members Area</title>
</head>
<body>
<h3 align="center">Please Fill In The Form Below To Register</h3>
<p>You must register in order to recieve access to the members area. It only takes 30 seconds!</p>
<p>Titles with a (*) are required</p>
<form action="test.php" method="post">
<p>Username:<br><input type="text" name="srnm"></input></p>
<p>First Name:<br><input type="text" name="frstnm"></input></p>
<p>Last Name:<br><input type="text" name="lstnm"></input></p>
<p>Email Address:<br><input type="text" name="mldrs"</input></p>
<p>Country:<br><input type="text" name="adrs5"></input></p>
<p>Password:<br><input type="password" name="pswrd" min="6" max="12"></input></p>
<input type="submit" value="Submit"></input>
<input type="reset" value="Clear"></input>
</form>
</body>
</html>


and I made a php file like this:
Quote:
<html>
<head>
<title>Thank You For Registering <?php $frstnm ; ?></title>
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
$frstnm = $_POST['frstnm'];
$pswrd = $_POST['pswrd'];
$srnm = $_POST['srnm'];
?>
<h2 align="center">Thank You For Registering <?php echo $frstnm ; ?>!</h2>
<p><?php echo $frstnm ; ?> you now have unlimited access to the members area!
<p>Here are your login details:
<br>
<b>Username:</b>
<?php echo $srnm ; ?>
<br>
<b>Password:</b><?php echo $pswrd ; ?>
</body>
</html>


When I enter in the details into test.html and click "Submit" it brings me to test.php and It says everything it should. I am very happy I have gotten that far as I only started learning php today.

I want the person to be able to enter in there details into the form on test.html and then when they click "Submit" they will be brought to the test.php but I also want the info that was entered in the form to be sent to a database called ncwdavid_members and a new table made for every piece of info entered in the form. How can that be done?
hexkid
ncwdavid wrote:
and I made a php file like this:

Code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<html>
<head>
<title>Thank You For Registering <?php $frstnm ; ?></title>
</head>
<body>
<?php
### error_reporting(E_ALL);
### ini_set('display_errors', 1);
?>
...

In the code above, with the error_reporting after the <title> line you will miss the notice about the undefined $frstnm Smile
I moved the lines to the very top of the script and the Notice will be caught now.

ncwdavid wrote:
I also want the info that was entered in the form to be sent to a database called ncwdavid_members and a new table made for every piece of info entered in the form. How can that be done?

Well ... first thing is that you do not want to create new tables for every registration. You create the tables when you set up the site and after that you only insert rows into the tables.

So let's say you have one table for all the info in the form. The table could be something like
Quote:
userid: int primary key auto_increment
username: varchar
firstname: varchar
lastname: varchar
email: varchar
country: varchar (*)
password: varchar

(*) I think it would be better for this column to be "int" and then link it to a table with the countries full name.


To insert a row into the table you need to do something like
Code:
<?php
### constants, better if they were in a file outside the web directory
### and you required it instead
// require_once '../../../../private/constants.inc';
define('DB_HOST', 'localhost');
define('DB_USER', 'ncwdavid_members');
define('DB_PASS', 'my_easy_to_remember_password');
define('DB_DATA', 'ncwdavid_members');
define('MASTER_EMAIL', 'ncwdavid@hotmail.com');

### connect to the database
$con = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('connect: ' . mysql_error());
if (!mysql_select_db(DB_DATA)) die('select db: ' . mysql_error());

### sanitize inputs
if (get_magic_quotes_gpc()) {
  $srnm = mysql_real_escape_string(stripslashes($_POST['srnm']));
  $frstnm = mysql_real_escape_string(stripslashes($_POST['frstnm']));
  $lstnm = mysql_real_escape_string(stripslashes($_POST['lstnm']));
  $mldrs = mysql_real_escape_string(stripslashes($_POST['mldrs']));
  $adrs5 = mysql_real_escape_string(stripslashes($_POST['adrs5']));
  $pswrd = mysql_real_escape_string(stripslashes($_POST['pswrd']));
} else {
  $srnm = mysql_real_escape_string($_POST['srnm']);
  $frstnm = mysql_real_escape_string($_POST['frstnm']);
  $lstnm = mysql_real_escape_string($_POST['lstnm']);
  $mldrs = mysql_real_escape_string($_POST['mldrs']);
  $adrs5 = mysql_real_escape_string($_POST['adrs5']);
  $pswrd = mysql_real_escape_string($_POST['pswrd']);
}

### insert record
$sql = "insert into userinfo (username, firstname, lastname, email, country, password)
        values ('$srnm', '$frstnm', '$lstnm', '$mldrs', '$adrs5', '$pswrd')";
mysql_query($sql) or die('query: ' . mysql_error());
if (mysql_affected_rows() == 1) {
  echo 'Your information was saved to the database.';
} else {
  echo 'Unable to save information in the database. Please try again in a few minutes.';
  mail(MASTER_MAIL, 'unable to insert row', print_r($_POST, true));
}

### close the connection to the database
mysql_close();
?>
ncwdavid
Thank you for that. It works great! Now I need to know how to have a pre written email sent to the email address that was entered in the form which has hello <?php echo $frstnm ; ?> and then all of my own stuff plus there login details.

Now I need to know how to create a login box and password protect http://mydomain.com/members.php. How can that be done. Any help at all will be appreciated. Thank in advance.
hexkid
ncwdavid wrote:
Now I need to know how to have a pre written email sent to the email address that was entered in the form which has hello <?php echo $frstnm ; ?> and then all of my own stuff plus there login details.

Code:
<?php
$msg = 'bla bla bla
##LOGIN## / ##PASSWORD##
bla bla bla

Thank you'; // or you could file_get_contents() from a template file

$msg = str_replace('##LOGIN##', $srnm, $msg);
$msg = str_replace('##PASSWORD##', $pswrd, $msg);

mail($mldrs, 'Your registration info for mydomain.com', $msg)
?>



ncwdavid wrote:
Now I need to know how to create a login box and password protect http://mydomain.com/members.php. How can that be done. Any help at all will be appreciated.

1. Make an HTML login form
2. After it is submitted, check the database
3. If the username/password does not exist output an error message and a link to the login page
4. If it exists set a session variable
5. On all other pages check the session variable and redirect to the login page if it isn't properly set
ncwdavid
Sorry but I didn't realy understand your post with the codes and the login form explaination as I have only been doing php for a few days. If you do not mind could you please go throught it in more detail for me? Thank you
rfwrangler
If you want to send an automated email to the user with there details here is the code for that.
Code:
//Email the user with the registration information.
$subject = "Thank you for registering with My Website";
$message = "Dear $first_name $last_name,
Thank you for registering with my website, http://www.my.frih.net!

You will be able to login with the following information:
   Username: $member_name
   Password: $password
   
Thank you,
The Webmaster
My Website

This is an automated response, please DO NOT reply!";
      mail($email_address, $subject, $message, "From: My Website<noreply@my.frih.net>
nX-Mailer: PHP/" . phpversion());


$subject is the email subject
$message is the formatted message that you send the user
$first_name is the user's first name
$last_name is the user's last name
$member_name is the user's member name
$password is the user's password unencrypted

Where the code says my.frih.net it would be the address that you use
and where it says My Website would be the name of your website.
This code snippet you would put right before the echo statement where you tell the user that registration is complete.

If you have anymore questions let me know.
hexkid
ncwdavid wrote:
Sorry but I didn't realy understand your post with the codes and the login form explaination as I have only been doing php for a few days. If you do not mind could you please go throught it in more detail for me? Thank you


In my version of your template email, things that get replaced by user values are enclosed in "##" (##LOGIN## for example).
So you want to get the template and replace those things by their proper values. That's what I used str_replace() for.
After the data is relative to the user (and not a template anymore), just mail it.
ncwdavid
Thank you. It works but now I have just figured something out. I went to the form page and I registered twiced with the same password, username and email address but it said nothing and the details got saved to the database. How can I stop that.?
hexkid
ncwdavid wrote:
Thank you. It works but now I have just figured something out. I went to the form page and I registered twiced with the same password, username and email address but it said nothing and the details got saved to the database. How can I stop that.?


Code:
<?php
### debugging help
error_reporting(E_ALL);
ini_set('display_errors', '1');

### constants
// ...

### connect to the database
// ...

### sanitize inputs
// ...

###########################
## CHECK IF $srnm EXISTS ##
###########################
$sql = "select count(*) from userinfo where username='$srnm'";
$res = mysql_query($sql) or die('query: ' . mysql_error());
$count = mysql_result($res, 0);
mysql_free_result($res);
if ($count == 1) {
  exit('That username is in use. Please <a href="test.html">try another</a>.');
}

### insert record
// ...

### close the connection to the database
// ...
?>
rfwrangler
Here is a tutorial about alot of the issues that you are working on it is the one that I used to create a membership style website and it is real informative and I learned alot from working through it. This tutorial will show you how to set up your database, do error checking on the information that you recieve from people signing up, generate a random password for your users, insure that the same username and email address are not entered into the database, and email the user with their information. Hopefully this will help you with alot of what you are trying to do. If I can be of any help let me know.

Here is the link to the tutorial:
http://www.phpfreaks.com/tutorials/40/0.php
ncwdavid
Thank for all yere help. That tutorial really helped me.
rfwrangler
You are very welcome. I am glad thst I could help you out. That is what this forum is all about. Thank to Frihost I have been able to learn PHP and develop a vision for my siite and some other sites that I will be working on. Good luck with your site and if I can help let me know.
Related topics
Shoutbox?
Do you use dreamweaver for web design.
.htaccess problem
How to make DVD's with Menu's using NeroVision
mySQL questions
importing SQL databases
Can't access the index of my boards!
could not connect to the database
Convert small table layout to CSS
all about visual basic and microsoft access!!!
SQL + help
SQL problem for solving :)
Creating tables with Mysql queries
Can someone create a mysql user for me on my website?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.