Jump to content

Single PHP file with functions and insterts into MySql


Recommended Posts

I currently working on a small class roster that will submit user data and class evaluation to a mysql data base.  I have not coded in PHP for some time now so I am super rusty.  This would be used by 3 different class instructor covering 3 different classes.

 

The idea is that once we complete a period of instruction the students would open the website which would display the 3 different icons for the classes.  when clicking on one, it would then display the class roster form.  fill in the blanks..  etc  and it would be stored in my database.  I was trying to utilize minimal different php files and have place the code below.  (some of the code is incomplete, but you should get the general idea...)

 

// index.php

<?
/**
* Index.php
* 
* This is a Class Roster/registration form
* for the CLC2S, BCS3, and TCPT classes
* taught in III MEF. 
*
* Written by:  
* Last Updated: December 3, 2009
*/
include ("include/constants.php");
session_start();

class NewStudent
{
   /* Class constructor */
   function NewStudent(){
   // Checking to see our session is started
if (isset($_SESSION['name']))
	{
procRegister();
	}
Else
{
procHome();
exit;
};
  }
}

/**
* This displays the home page again.
**/
function procHome(){ 
//HTML code of the home page
?>

<html>
<body>	
     
    <form name="form1" action="" method="POST"> 
    <table width="400" height="600" border="0" align="center" valign="middle" cellpadding="20" cellspacing="0">
    <tr><td id="clc2s"><img src="include/CLC2S_Logo.gif" width="200" height="200" name="CLC2S" border="0" /></td><td id="bcs3"><a href="index.php?func=NewStudent"><input type="image" src="include/CLC2S_Logo.gif" height="200" width="200" name="BCS3" /></a></td><td id="tcpt"><img src="include/CLC2S_Logo.gif" width="200" height="200" /></td></tr>
    </table>
</form>
     

</body>
</html>	

<?	
//;
}


/**
* This displays the Class roster page.
**/
function procRegister(){ 
?>
<html>
<head>
<link href="include/register.css" rel="stylesheet" type="text/css" />
</head>
<body>

<h1><? echo $_SESSION['name'] ?> Class Roster</h1>

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=0123456789\-\|]/;
function dodacheck(val) {
var strPass = val.value;
var strLength = strPass.length;
var lchar = val.value.charAt((strLength) - 1);
if(lchar.search(mikExp) != -1) {
var tst = val.value.substring(0, (strLength) - 1);
val.value = tst;
   }
}
function doanothercheck(form) {
if(form.value.length < 1) {
alert("You must have a name.");
return false;
}
if(form.value.search(mikExp) == -1) {
alert("Your Input Was Accepted");
return false;
}
else {
alert("Sorry, but the following characters\n\r\n\r@ $ % ^ & * # ( ) [ ] \\ { + } ` ~ =  | \n\r\n\rare not allowed!\n");
form.select();
form.focus();
return false;
}
}
//  End -->
</script>

<script language="JavaScript" type="text/JavaScript">
<!--
function jm_datemask(t)
{var donepatt = /^(\d{2})\/(\d{2})\/(\d{4})$/;
var patt = /(\d{2}).*(\d{2}).*(\d{4})/;
var str = t.value;
if (!str.match(donepatt))
{result = str.match(patt);
if (result!= null)
{t.value = t.value.replace(/[^\d]/gi,'');
str = result[1] + '/' + result[2] + '/' + result[3];
t.value = str;
}else{
if (t.value.match(/[^\d]/gi))
t.value = t.value.replace(/[^\d]/gi,'');
}}}

function jm_phonemask(t)
{var patt1 = /(\d{3}).*(\d{3}).*(\d{4})/;
var patt2 = /^\((\d{3})\).(\d{3})-(\d{4})$/;
var str = t.value;
var result;
if (!str.match(patt2))
{result = str.match(patt1);
if (result!= null)
{t.value = t.value.replace(/[^\d]/gi,'');
str = '(' + result[1] + ') ' + result[2] + '-' + result[3];
t.value = str;
}else{
if (t.value.match(/[^\d]/gi))
t.value = t.value.replace(/[^\d]/gi,'');
}}}

function jm_ssnmask(t)
{var patt = /(\d{3}).*(\d{2}).*(\d{4})/;
var donepatt = /^(\d{3})-(\d{2})-(\d{4})$/;
var str = t.value;
var result;
if (!str.match(donepatt))
{result = str.match(patt);
if (result!= null)
{t.value = t.value.replace(/[^\d]/gi,'');
str = result[1] + '-' + result[2] + '-' + result[3];
t.value = str;
}else{
if (t.value.match(/[^\d]/gi))
t.value = t.value.replace(/[^\d]/gi,'');}
}}
//-->
</script>
<form name=register action="process.php" method="POST">
<table align="left" border="2" cellspacing="1" cellpadding="3">
<tr><td>First Name:</td><td><input type="text" name="fname" maxlength="20"></td><td></td></tr>
<tr><td>Middle Name:</td><td><input type="text" name="mname" maxlength="15"></td><td></td></tr>
<tr><td>Last Name:</td><td><input type="text" name="lname" maxlength="25"></td><td></td></tr>
<tr><td>Last 4 SSN:</td><td><input type="text" name="last4" maxlength="4"></td><td></td></tr>
<tr><td>Rank:</td><td><select name="rank"><option selected>Select</option>
<option>Pvt</option>
<option>PFC</option>
<option>LCpl</option>
<option>Cpl</option>
<option>Sgt</option>
<option>SSgt</option>
<option>GySgt</option>
<option>MSgt</option>
<option>1st Sgt</option>
<option>MGySgt</option>
<option>SgtMaj</option>
<option>2nd LT</option>
<option>1st LT</option>
<option>Capt</option>
<option>Maj</option>
<option>LtCol</option>
<option>Col</option></selected></td><td></td></tr>
<tr><td>MOS:</td><td><input type="text" name="mos" maxlength="4"></td><td></td></tr>
<tr><td>Rotation Date:</td><td><input type="text" name="rtd" maxlength="10"></td><td></td></tr>
<tr><td>RUC/UIC:</td><td><input type="text" name="ruc" maxlength="6"></td><td></td></tr>

<input type="hidden" value="campSelect" name="cs_config_country_field" id="cs_config_country_field">
<input type="hidden" value="unitSelect"   name="cs_config_state_field"   id="cs_config_state_field">
<!-- The id of the the fields holding the default values.  If more than one, seperate with spaces -->
<input type="hidden" value="countryDefault" name="cs_config_country_default" id="cs_config_country_default">
<input type="hidden" value="stateDefault"   name="cs_config_state_default"   id="cs_config_state_default">
<!-- The actual default values -->
<input type="hidden" value="" name="countryDefault" id="countryDefault">
<input type="hidden" value=""   name="stateDefault"   id="stateDefault">
<SCRIPT type="text/javascript" SRC="camp_unit.js"></SCRIPT>
<tr><TD>Location:</TD><td>
<div>
	     <select id='campSelect' name='camp' onchange='updateState(this.id)'>
      </select>
	</div>
<tr><td>Unit:</td><td><div>
	  <select id='unitSelect' name='unit'>
	  </select>
   </div></td><td></td></tr>
<SCRIPT  type="text/javascript">initCountry(); </SCRIPT>

<tr><td>Company/Section:</td><td><input type="text" name="co" maxlength="25" ></td><td></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" maxlength="75" ></td><td></td></tr>
<tr><td>Phone #:</td><td><input type="text" name="phone1" maxlength="14" onkeyup="jm_phonemask(this)" value="<? $_POST['phone1']; ?>"></td><td></td></tr>
<tr><td>Alt Phone #:</td><td><input type="text" name="phone2" maxlength="14" value="<? $_POST['phone2']; ?>"></td><td></td></tr>



<!--
<input type="hidden" name="date" value="1">
<input type="hidden" name="bcs3" value="<? IF ($_POST['clname'] == 'BCS3') { echo "Y";} else { echo "N";} ?>">
<input type="hidden" name="clc2s" value="<? IF ($_POST['clname'] == 'CLC2S') { echo "Y";} else { echo "N";} ?>">
<input type="hidden" name="tcpt" value="<? IF ($_POST['clname'] == 'TCPT') { echo "Y";} else { echo "N";} ?>">

-->

<tr><td colspan="2" align="right"><input type="submit" name="Process" value="Register"></td></tr>
<tr><td colspan="2" align="left"><a href="#" target="_top">Back to Home</a></td></tr>
</table>
</form>


</body>
</html>

<?	}   


/**
* This function will process the user registration data
**/
function procProcess(){
mysql_select_db($db_name, $conn);

$fname = isset($_POST['fname']) ? mysql_real_escape_string($_POST['fname']) : "";
$mname = isset($_POST['mname']) ? mysql_real_escape_string($_POST['mname']) : "";
$lname = isset($_POST['lname']) ? mysql_real_escape_string($_POST['lname']) : "";
$last4 = isset($_POST['last4']) ? mysql_real_escape_string($_POST['last4']) : "";
$rank = isset($_POST['rank']) ? mysql_real_escape_string($_POST['rank']) : "";
$mos = isset($_POST['mos']) ? mysql_real_escape_string($_POST['mos']) : "";
$rtd = isset($_POST['rtd']) ? mysql_real_escape_string($_POST['rtd']) : "";
$ruc = isset($_POST['ruc']) ? mysql_real_escape_string($_POST['ruc']) : "";
$camp = isset($_POST['camp']) ? mysql_real_escape_string($_POST['camp']) : "";
$unit = isset($_POST['unit']) ? mysql_real_escape_string($_POST['unit']) : "";
$co = isset($_POST['co']) ? mysql_real_escape_string($_POST['co']) : "";
$email = isset($_POST['email']) ? mysql_real_escape_string($_POST['email']) : "";
$phone1 = isset($_POST['phone1']) ? mysql_real_escape_string($_POST['phone1']) : "";
$phone2 = isset($_POST['phone2']) ? mysql_real_escape_string($_POST['phone2']) : "";



$sql = "INSERT INTO 'roster' (fname,mname,lname,last4,rank,mos,rtd,ruc,camp,unit,co,email,phone1,phone2) VALUES ('".$fname."','".$mname."','".$lname."','".$last4."','".$rank."','".$mos."','".$rtd."','".$ruc."','".$camp."','".$unit."','".$co."','".$email."','".$phone1."','".$phone2."')";

mysql_query($sql);

mysql_close($conn);

}

/* Initialize process */
new NewStudent;	

?>

the other php file is just the mysql connection variables.

 

Thanks for any assistance...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.