Skip to content

Instantly share code, notes, and snippets.

@mebrett
Created September 20, 2012 12:37
Show Gist options
  • Save mebrett/3755630 to your computer and use it in GitHub Desktop.
Save mebrett/3755630 to your computer and use it in GitHub Desktop.
Adding people to database table
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Form to Insert Persons, Maury1a</title>
</head>
<body>
<h2>Create New Person Record</h2>
<p>This form creates a new person record for the database Maury1a. Please include as much information as possible.</p>
<form action="insert.php" method="post">
First Name: <input type="text" name="firstname" /><br />
Middle Name: <input type="text" name="midname" /><br />
*Last Name: <input type="text" name="lastname" /><br />
Date of Birth: <input type="text" name="dob" />
Date of Death: <input type="text" name="dod" /><br />
Notes: <input type="text" name="notes" size="100" /><br />
<input type="submit" value="Send" />
</form>
<p>Dates must be entered as mm/dd/yyyy. Fields marked with * are required.</p>
</body>
</html>
<?php
//connect to database
$con = mysql_connect('localhost', 'user', 'password');
//check connection
if (!$con) {
die('Could not connect');}
/*else{echo 'Connection success<br />';}*/
mysql_select_db('maury1');
//store values
$nameF=mysql_real_escape_string($_POST['firstname']);
$nameM=mysql_real_escape_string($_POST['midname']);
$nameL=mysql_real_escape_string($_POST['lastname']);
$birth = explode ('/', $_POST['dob']);
$dob = $birth[2].'-'.$birth[0].'-'.$birth[1];
$death = explode ('/', $_POST['dod']);
$dod = $date[2].'-'.$date[0].'-'.$date[1];
$note=mysql_real_escape_string($_POST['note']);
//Check to make sure a last name is entered
if ($nameL == ""){
die('Error! Last name is a required field.<br /><a href="form2.php">Go back</a>');}
//formulate query
$new_name = "INSERT INTO person (pers_no, name_first, name_mid, name_last, dob, dod, pers_notes) VALUES ('NULL', '".$nameF."', '".$nameM."', '".$nameL."', '".$dob."', '".$dod."', '".$note."')";
//submit query
mysql_query($new_name) or die('Error '. mysql_error());
//get new prime key
$newPid = mysql_insert_id();
echo "Success: $nameF $nameL added with unique id $newPid";
echo "<br /> <a href='form.html'>Back</a>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment