Databases
|
|
|
|
<?php
include('db.php');
include('functions.php');
include('menu2/menu.inc.php');
connect();
?>
<?php
$today=Date("d-M-Y H:i:s"); echo $today;
$fp=fopen('log.htm','a+');
//fputs($fp,'<a name=''.$today.''>------------------'.$today.'----------------------<br>');
?>
<?php
// get tables
$tableNames=array();
$tableNames=getTables();
$NO_OF_TABLES=count($tableNames);
$tableSetter=0;
if(!isset($txtTable))
$txtTable= $tableNames[2];
else $txtTable=$_REQUEST['txtTable'];
if(isset($txtTable))
{
//$txtTable=$_REQUEST['txtTable'];
$tableDesc=getTableDesc($txtTable);
$NO_OF_COLS=count($tableDesc);
$p_field= getPrimaryField($txtTable);// echo $p_field;
if($p_field=='')
{
$message[]="No primary field defined for this table";
echo "No primary field defined for this table<br>Currently this facility of displaying such kind of tables is not provided. ";
exit();
//$qry1=getValues("select * from $txtTable");
//while($arr1=mysql_fetch_array($qry1))
//$p_field=$arr1[0];
}
}
if($_SERVER['REQUEST_METHOD']=='POST')
{
if(isset($subQuery))
{
$txtQuery=stripslashes($_REQUEST['txtQuery']);
$res=insertValues($txtQuery);
if(mysql_error())
{
$message[]= mysql_errno() . ": " .mysql_error();
addToLog(mysql_errno() . ": " .mysql_error());
}
else
{
$message[]="<b>Query successful: </b> Affected Rows :".mysql_affected_rows($res);
addToLog("Query successful: </b> Affected Rows :".mysql_affected_rows($res));
}
}
//Add Record
if(isset($subAdd))
{
$insertQuery="insert into $txtTable values (";
foreach($_REQUEST['add'] as $var )
$insertQuery.="'$var',";
$insertQuery=substr($insertQuery,0,(strlen($insertQuery)-1));
$insertQuery.=" )";
//echo $insertQuery;
addToLog($insertQuery);
$res= insertValues($insertQuery);
if(mysql_error())
{
$message[]= mysql_errno() . ": " .mysql_error();
addToLog(mysql_errno() . ": " .mysql_error());
}
else
{
$message[]="<b>Record Added</b>";
addToLog("Record Added");
}
}
// Modify Records
if(isset($subModify))
{
if(isset($chk))
{
$modArr=array();
$affRows=0;
foreach($_REQUEST['chk'] as $var )
{
$updateQuery="update $txtTable set ";
$primaryId=$var;
//echo "<br>PRIMARY FIELD ".$var;
$temp=0;
for ($i=0;$i<$NO_OF_COLS;$i++ )
{
$a=$var.'#'.$i;
//echo "<br>Field names ".$_REQUEST[$a];
if($_REQUEST[$a]!=$primaryId)
$updateQuery=$updateQuery.$tableDesc[$i]." ='".trim($_REQUEST[$a])."' , ";
else $temp=$i;
}
$updateQuery=substr($updateQuery,0,(strlen($updateQuery)-2));
$updateQuery=$updateQuery." where ".$tableDesc[$temp]." = '".$primaryId ."'";
//echo $updateQuery."<BR>";
addToLog($updateQuery);
$res= updateValues($updateQuery);
if(mysql_error())
{
$message[]= mysql_errno() . ": " .mysql_error();
addToLog( mysql_errno() . ": " .mysql_error());
}
else
{
$affRows+=mysql_affected_rows($res);
addToLog("Query successful: </b> Affected Rows :".mysql_affected_rows($res));
}
$updateQuery='';}
$message[]="<b>Query successful: </b> Affected Rows :".$affRows;
addToLog("Query successful: </b> Affected Rows :".$affRows);
}
else $message[]="Select a record to modify";
}
// Delete records
if(isset($subDelete))
{
if(isset($chk))
{
$modArr=array();
$affRows=0;
foreach($_REQUEST['chk'] as $var )
{
$updateQuery="delete from $txtTable where $p_field='$var' ";
//echo $updateQuery."<BR>";
addToLog("delete from $txtTable where $p_field='$var' ");
$res= updateValues($updateQuery);
if(mysql_error())
{
$message[]= mysql_errno() . ": " .mysql_error();
addToLog(mysql_errno() . ": " .mysql_error());
}
else
{
$affRows+=mysql_affected_rows($res);
addToLog("Query successful: </b> Affected Rows :".mysql_affected_rows($res));
}
$updateQuery='';
}
$message[]="<b>Query successful: </b> Affected Rows :".$affRows;
addToLog("Query successful: </b> Affected Rows :".$affRows);
}
else $message[]="Select a record to modify";
}
} // REQUEST METHOD end
?>
<html>
<head><link href="sheets/SSheet.css" rel="stylesheet" type="text/css">
<script LANGUAGE="JavaScript" TYPE="text/javascript">
function CheckAll(chk)
{
for (var i=0;i <document.forms[1].elements.length;i++)
{
var e = document.forms[1].elements[i];
if (e.type == "checkbox")
{
e.checked = chk.checked;
}
}
}
</script>
</head>
<style type="text/css">
<!--
A:hover { color: #cc3300; }
A.path:link, A.path:visited, A.path:active { color: #ffffff; }
A.path:hover { color: #cccccc; }
A.menu:link, A.menu:visited, A.menu:active { color: #000000; }
A.menu:hover { color: #cc3300; }
A.breadcrumbs:link { color: #ffffff; }
A.network:link, A.network:visited, A.network:active { font-family: Verdana,Arial,Helvetica; font-size: 8pt; color: #000000; text-decoration: none;}
A.network:hover { color:#000000; text-decoration: underline;}
A.footer:link, A.footer:visited, A.footer:active { color: #aaaaaa; text-decoration: none; }
td {font-family: Arial,Helvetica; font-size: 10pt;}
// -->
</style>
<body>
<?php <table width='100%'>
<tr>
<td align='top'>
<form action="contents2.php" method="POST" name="mainForm">
<table width='100%' border='1'> <!-- Top Table for query,message and menu bar-->
<tr>
<td>
<input name="subModify" value="Modify" class="sbutton" type="submit">
<input name="subDelete" value="Delete" class="sbutton" type="submit">
<input name="subDesc" value="Table" class="sbutton" type="submit">
<input name="subResolution" value="Resolution" class="sbutton" type="submit">
<input name="subModify" value="Modify" class="sbutton" type="submit">
<input name="subModify" value="Modify" class="sbutton" type="submit">
<input name="subModify" value="Modify" class="sbutton" type="submit">
<input name="subViewLog" value="View Log" class="sbutton" type="submit">
</td>
</tr>
</table>
</td>
</tr>
<tr> <td>
<table width='100%' border='1'>
<tr>
<td>Query</td>
<?php if (isset($txtQuery)) { ?>
<td colspan='6'><textarea name='txtQuery' rows='3' cols='80'><?php echo stripslashes($_REQUEST['txtQuery']); ?></textarea>
<?php } else { ?>
<td colspan='6'><textarea name='txtQuery' rows='3' cols='80'> </textarea>
<?php } ?>
<input name="subQuery" value="Run" class="sbutton" type="submit"></td>
</tr>
</table>
</td>
</tr>
<?php if(isset($message))
{
if($message!=NULL)
{ ?>
<tr>
<td>
<table width='100%'>
<?php foreach($message as $var)
echo "<tr><td><font color='#990000'>$var</font></td></tr>";
?>
</table>
<?php
}
} ?>
</td>
</tr>
<tr>
<td>
<table width='100%' cellpadding='0' cellspacing='0' border='1' >
<tr class='lightbrownbg'>
<td>
<strong><?php echo $txtTable; ?></strong><a href='log.htm#$today'>View Log</a></td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table width='100%' cellpadding='0' cellspacing='0' border='1' >
<tr class='lightbluebg'>
<td><input type=CHECKBOX name=checkAll disabled=true onclick='checkAll(this)'></td>
<?php
$txtWidth=array();
$fields=getFullTableDesc($txtTable);
for ($i=0;$i<$NO_OF_COLS;$i++ )
{
$txtWidth[]=strlen($fields[$i][0]);
echo "<td>|".$fields[$i][0]."</td>";
if ($fields[$i][0]==$p_field)
{
$index=$i;
}
}
?>
</tr>
<!-- add a new record -->
<tr>
<td><input type='submit' name='subAdd' class=sbutton value='Add'></td>
<?php
for ($i=0;$i<$NO_OF_COLS;$i++ )
{
$inputName=$p_field.'@'.$i;
echo "<td><input type='text' size='".$txtWidth[$i]."' value='' name='add[]' class='flatbox'></td>";
}
?>
</tr>
<?php
$qry=getValues("select * from $txtTable");
$tdcolor='lightbrownbg';
while($arr=mysql_fetch_array($qry))
{
if($tdcolor=='lightbrownbg')
$tdcolor='lightbluebg';
else $tdcolor='lightbrownbg';
?>
<tr class='<?php echo $tdcolor; ?>'>
<td><input type='checkbox' class='FlatChkBox' name='chk[]' value='<?php echo $arr[$p_field]; ?>'></td>
<?php
for ($i=0;$i<$NO_OF_COLS;$i++ )
{
$inputName=$arr[$p_field].'#'.$i;
if($arr[$i]== $arr[$p_field])
echo "<td width='".$txtWidth[$i]."'><input type='text' value='$arr[$i]' size='".$txtWidth[$i]."' name='$inputName' class='flatbox2'></td>";
else
echo "<td width='".$txtWidth[$i]."'><input type='text' value='$arr[$i]' size='".$txtWidth[$i]."' name='$inputName' class='flatbox'></td>";
}
} ?>
</tr>
</table>
</td>
</tr>
<tr>
<td><input type='hidden' name='txtTable' value='<?php echo $txtTable;?> '></td>
</tr>
</form>
<?php // for($i=0;$i< ($NO_OF_TABLES-$tableSetter-10);$i++)
//echo "<tr><td></td></tr>";
?>
</table>
</body>
</html>
|
|
|
Usage Example
|
|
|
Rate This Script
|
|
|
|