Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Create form :
- <form name="addPerson" action="addPerson.php" method="post">
- <div>First Name:</div>
- <div><input type="text" name="firstName" size="40" maxlength="255" /></div>
- <div>Last Name:</div>
- <div><input type="text" name="lastName" size="40" maxlength="255" /></div>
- <div>Email Address:</div>
- <div><input type="text" name="email" size="40" maxlength="255" /></div>
- <div>Phone Number:</div>
- <div><input type="text" name="phone" size="40" maxlength="255" /></div>
- <div><input type="submit" value="Add Person" /></div>
- </form>
- Get and View Information :
- <?php
- //get the form items.
- $firstName = addslashes(htmlspecialchars($_POST['firstName']));
- $lastName = addslashes(htmlspecialchars($_POST['lastName']));
- $email = addslashes(htmlspecialchars($_POST['email']));
- $phoneNum = addslashes(htmlspecialchars($_POST['phone']));
- //this will be for the error message to display
- //if they didn't fill out the form completely.
- $errorMessage = "";
- if(empty($firstName)) $errorMessage .= "You didn't enter a First Name<br/>";
- if(empty($lastName)) $errorMessage .= "You didn't enter a Last Name<br/>";
- if(empty($email)) $errorMessage .= "You didn't enter an Email Address<br/>";
- if(empty($phoneNum)) $errorMessage .= "You didn't enter a Phone Number<br/>";
- //now we check to see if there was an error
- if(empty($errorMessage)) {
- //everything is alright and we can now add it to the DB
- //these variables are used for database connection
- $host = "localhost";
- $user = "db_user";
- $pass = "db_pass";
- $database = "db_name";
- //connect to the database.
- $db = mysql_connect($host, $user, $pass);
- mysql_select_db ($database);
- //set up the query to add the information.
- $query = "INSERT INTO 'people' ('firstName', 'lastName', 'email', `phone`)
- VALUES ('$firstName', '$lastName', '$email', '$phone');";
- $result = mysql_query($query) OR die(mysql_error());
- echo 'You have successfully added ' . $firstName . ' to the database!';
- } else {
- //something was wrong.
- echo $errorMessage;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment