Skip to content

Instantly share code, notes, and snippets.

@resba
Created January 26, 2012 19:13
Show Gist options
  • Save resba/1684455 to your computer and use it in GitHub Desktop.
Save resba/1684455 to your computer and use it in GitHub Desktop.
SQLFunc
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_db = "localhost";
$database_db = "web";
$username_db = "root";
$password_db = "";
$db = mysql_pconnect($hostname_db, $username_db, $password_db) or trigger_error(mysql_error(),E_USER_ERROR);
?>
<?php
require_once('sqlfunc.class.php');
require_once('db.php');
?>
<?php
$sqlfunc = new sqlfunc();
$Recordset1 = "";
$Recordset2 = "";
$Recordset1 = $sqlfunc->retrieveRecords($Recordset1,"sessions",$database_db,$db);
$Recordset2 = $sqlfunc->retrieveRecordsWithFilter($Recordset2,"plans","view","1",$database_db,$db);
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if (isset($_GET['msg'])){
if($_GET['msg']=="1"){
$r3 = "";
$month = substr($_POST['date'],-8,2);
$day = substr($_POST['date'],-5,2);
$sqlfunc->uploadFile("location","/weblive/lesson/".$Recordset1['row']['year']."/".$_POST['sid']."_".$Recordset1['row']['name']."/",$month."_".$day.".pdf");
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO plans (sid, `date`, edate, location, `view`) VALUES (%s, %s, %s, %s, %s)",
$sqlfunc->GetSQLValueString($_POST['sid'], "text"),
$sqlfunc->GetSQLValueString($_POST['date'], "text"),
$sqlfunc->GetSQLValueString($_POST['edate'], "text"),
$sqlfunc->GetSQLValueString($_POST['location'], "text"),
$sqlfunc->GetSQLValueString($_POST['view'], "int"));
mysql_select_db($database_db, $db);
$Result1 = mysql_query($insertSQL, $db) or die(mysql_error());
}
}
}
/*
mysql_select_db($database_db, $db);
$query_Recordset1 = "SELECT * FROM sessions";
$Recordset1 = mysql_query($query_Recordset1, $db) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
mysql_select_db($database_db, $db);
$query_Recordset2 = "SELECT * FROM plans WHERE `view` = 1";
$Recordset2 = mysql_query($query_Recordset2, $db) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload</title>
</head>
<body>
<form action="<?php echo $editFormAction; ?>msg=1" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table align="center">
<tr valign="baseline">
<td nowrap="nowrap" align="right">Session ID</td>
<td><select name="sid">
<?php
do {
?>
<option value="<?php echo $Recordset1['row']['sessletter'];?>"><?php echo $Recordset1['row']['name']." - ".$Recordset1['row']['year'];?></option>
<?php
} while ($Recordset1['row'] = mysql_fetch_assoc($Recordset1['recordset']));
?>
</select></td>
</tr>
<tr> </tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Date:</td>
<td><input type="text" name="date" value="<?php echo date('m\/d\/y'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Ending Date:</td>
<td><input type="text" name="edate" value="<?php echo date('m\/d\/y'); ?>" size="32" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Lesson Plan:</td>
<td><label for="location"></label>
<input type="file" name="location" id="location" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">&nbsp;</td>
<td><input name="view" type="hidden" value="1" /></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right">&nbsp;</td>
<td><input type="submit" value="Insert Plan" /></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Recordset1['recordset']);
mysql_free_result($Recordset2['recordset']);
?>
<?php
class sqlfunc
{
var $query_Recordset;
var $Recordset;
var $row_Recordset;
var $totalRows_Recordset;
var $rs;
var $user;
var $pass;
var $db;
public function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
public function retrieveRecords($rs,$table,$database_db,$db)
{
$rs = "";
mysql_select_db($database_db, $db);
$query_Recordset = "SELECT * FROM ".$table;
$Recordset = mysql_query($query_Recordset, $db) or die(mysql_error());
$row_Recordset = mysql_fetch_assoc($Recordset);
$totalRows_Recordset = mysql_num_rows($Recordset);
return $rs = array("row" => $row_Recordset,
"totalRows" => $totalRows_Recordset,
"recordset" => $Recordset);
mysql_free_result($Recordset);
}
public function retrieveRecordsWithFilter($rs,$table,$filter,$value,$database_db,$db)
{
$rs = "";
mysql_select_db($database_db, $db);
$query_Recordset = "SELECT * FROM ".$table." WHERE `".$filter."` = ".$value;
$Recordset = mysql_query($query_Recordset, $db) or die(mysql_error());
$row_Recordset = mysql_fetch_assoc($Recordset);
$totalRows_Recordset = mysql_num_rows($Recordset);
return $rs = array("row" => $row_Recordset,
"totalRows" => $totalRows_Recordset,
"recordset" => $Recordset);
mysql_free_result($Recordset);
}
public function uploadFile($form,$dir,$filename)
{
//The files have a link on a page for downloading
//and filenames are also put in the progress bar so
//the file can be viewed in the browser (ie. PDF files)
//so replace a few characters. Since the file links are
//loaded onto another page via php and filenames
//are displayed, I wanted to use this method instead
//of url_encode() [just looks funny when displayed]
$SafeFile = $_FILES[$form]['name'];
$SafeFile = str_replace("#", "No.", $SafeFile);
$SafeFile = str_replace("$", "Dollar", $SafeFile);
$SafeFile = str_replace("%", "Percent", $SafeFile);
$SafeFile = str_replace("^", "", $SafeFile);
$SafeFile = str_replace("&", "and", $SafeFile);
$SafeFile = str_replace("*", "", $SafeFile);
$SafeFile = str_replace("?", "", $SafeFile);
$uploaddir = $dir;
$path = $uploaddir.$filename;
fclose(fopen($path, 'wr+') or die("can't open file"));
if($form != none){ //AS LONG AS A FILE WAS SELECTED...
if(copy($_FILES[$form]['tmp_name'], $path)){ //IF IT HAS BEEN COPIED...
//GET FILE NAME
$theFileName = $_FILES[$form]['name'];
//GET FILE SIZE
$theFileSize = $_FILES[$form]['size'];
if ($theFileSize>999999){ //IF GREATER THAN 999KB, DISPLAY AS MB
$theDiv = $theFileSize / 1000000;
$theFileSize = round($theDiv, 1)." MB"; //round($WhatToRound, $DecimalPlaces)
} else { //OTHERWISE DISPLAY AS KB
$theDiv = $theFileSize / 1000;
$theFileSize = round($theDiv, 1)." KB"; //round($WhatToRound, $DecimalPlaces)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment