Version: 2.01
Type: Full Script
Category: Databases
License: GNU General Public License
Description: This is a simple url driven database viewer/editor. Note: this assumes you use a field called id as the index, if you're clever, i'm sure you can figure out how to change it to whatever you need.
<?php
//copyright 2003 averageJoe web production
//written by Joseph Vasquez
//questions, comments, smart remarks can go to joe_e563@hotmail.com
/*This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/
//session_start();
//include(???);
?>
<html>
<head>
<style type="text/css">
<?php //put stylesheet here//*************************** ?>
</style>
<head>
<title>averageJoe :: database tool</title>
</head>
<?php
//this script assumes your table has a unique field and that unique field is "id"
//this script isn't very secure because it shows your database and table name in the url,
//i use it mainly for an admin tool
//example url: http://www.mysite.net/myfolder/db_tool2.php?datbase_name&table_name&1,2,3,4,5&,1,2,3
//argument order: URL?database&table&field1,field2...(to show on main archive page)&field1,field2,...(to show on individual records)&action&sort&id&lo&hi
//*******************************//
//mysql info:
$user = "";
$password = "";
$host = "localhost";
//link to view indidual records on this field
$use_num = 2;
//style sheet stuff
$class4table = "db";
$class4row = "body";
$class4alt_row = "body_alt";
$class4link = "db";
//my admin level stuff is created from the login and session variables
//if you plan to use admin rights, you need to go and comment my admin stuff out farther in this script
//there are 4 places to edit for this
//right now, admin can delete and edit
//$admin_level = 1;
//*******************************//
$array = explode("?",$REQUEST_URI);
$array1 = explode("&",$array[1]);
$lo = 0;
$hi = 10;
$args = count($array1);
switch($args){
case 9:
$hi = $array1[8];
case 8:
$lo = $array1[7];
case 7:
$temp_id = $array1[6];
case 6:
$sort_by = $array1[5];
case 5:
$edit_action = $array1[4];
case 4:
$which_fields_show1 = $array1[3];
case 3:
$which_fields1 = $array1[2];
case 2:
$which_table = $array1[1];
case 1:
$which_db = $array1[0];
break;
}
if(isset($which_fields1) && !empty($which_fields1)){
$which_fields = explode(",",$which_fields1);
}
else{
$which_fields1 = "-1";
}
if(isset($which_fields_show1) && !empty($which_fields_show1)){
$which_fields_show = explode(",",$which_fields_show1);
}
else{
$which_fields1 = "-1";
}
$cur_page = intval($lo)/20;
$link = mysql_connect($host,$user,$password) or die("Couldn't establish link.");
mysql_select_db($which_db) or die("Couldn't select database.");
$result = mysql_query("SELECT * FROM $which_table");
$tot = mysql_num_rows(mysql_query("SELECT * FROM $which_table"));
if(!$tot){
$tot = 0;
}
switch($edit_action){
case "delete":
$query = "DELETE FROM $which_table where id='$temp_id' LIMIT 1";
$result = mysql_query($query) or die("Couldn't Query.");
echo java_redirect("$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&&$sort_by&$temp_id&$lo&$hi");
break;
case "show":
$query = "SELECT * FROM $which_table where id='$temp_id' LIMIT 1";
$result = mysql_query($query) or die("Couldn't Query.");
echo "<center><table width=\"75%\" border=0 cellpadding=0 class=\"$class4table\">\n";
echo "<tr class=\"$class4alt_row\"><td colspan=2><center><b>$which_table</b></center></td></tr>\n";
echo "<tr><td colspan=2><b><small>[<a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&&$sort_by&$temp_id&$lo&$hi\" target=\"_self\">back to archive</a>]</small></b></tr></td>";
$fields = mysql_list_fields($which_db,$which_table, $link);
$num_fields = mysql_num_fields($result);
if(intval($which_fields_show1) >= 0){
$num_fields = count($which_fields_show);
}
while($row = mysql_fetch_array($result,MYSQL_NUM)){
for($i = 0; $i <= $num_fields; $i++){
if($which_fields_show1 >= 0 && in_array(strval($i),$which_fields_show)){
$temp = mysql_field_name($fields, $i);
echo "<tr class=\"$class4row_alt\"><td align=\"left\" valign=\"top\" width=\"10%\"><b>$temp</b></td><td>$row[$i]</td><tr class=\"$class4row\">\n";
}
}
//this is what i use for admin rights
//change it to what ever you want
// if($logged_in && $user_level == $admin_level){
// echo "<td><a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&delete&$sort_by&$row[0]&$lo&$hi\" target=\"_self\">delete</a> <a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&edit&$sort_by&$row[0]&$lo&$hi\" target=\"_self\">edit</a></td>\n";
// }
//end admin rights
echo "</tr>\n";
}
echo "<tr><td colspan=2><b><small>[<a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&&$sort_by&$temp_id&$lo&$hi\" target=\"_self\">back to archive</a>]</small></b></td></tr>";
echo "</table></center>\n";
break;
case "edit":
$query = "SELECT * FROM $which_table where id='$temp_id' LIMIT 1";
$result = mysql_query($query) or die("Couldn't Query.");
echo "<center><form name=\"edit\" action=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&edit_second&$sort_by&$temp_id&$num&$num2\" method=\"post\"><table width=\"75%\" border=0 cellpadding=0 class=\"$class4table\">\n";
echo "<tr class=\"$class4alt_row\"><td colspan=2><center><b>$which_table</b></center></td></tr>\n";
echo "<tr><td colspan=2><b><small>[<a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&&$sort_by&$temp_id&$lo&$hi\" target=\"_self\">back to archive</a>]</small></b></tr></td>";
$fields = mysql_list_fields($which_db,$which_table, $link);
$num_fields = mysql_num_fields($result);
if(intval($which_fields_show1) >= 0){
$num_fields = count($which_fields_show);
}
while($row = mysql_fetch_array($result,MYSQL_NUM)){
for($i = 0; $i <= $num_fields; $i++){
if($which_fields_show1 >= 0 && in_array(strval($i),$which_fields_show)){
$temp = mysql_field_name($fields, $i);
echo "<tr class=\"$class4row_alt\"><td align=\"left\" valign=\"top\" width=\"10%\"><b>$temp</b></td><td><input type=\"text\" value=\"$row[$i]\" name=\"$temp\" length=25></td><tr class=\"$class4row\">\n";
}
}
//this is what i use for admin rights
//change it to what ever you want
// if($logged_in && $user_level == $admin_level){
// echo "<td><a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&delete&$sort_by&$row[0]&$lo&$hi\" target=\"_self\">delete</a></td>\n";
// }
//end admin rights
echo "</tr>\n";
}
echo "<tr><td colspan=2><input type=\"submit\" value=\"Update\"></td></tr>\n";
echo "<tr><td colspan=2><b><small>[<a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&&$sort_by&$temp_id&$lo&$hi\" target=\"_self\">back to archive</a>]</small></b></td></tr>";
echo "</table></form></center>\n";
break;
case "edit_second":
$query = "SELECT * FROM $which_table where id='$temp_id' LIMIT 1";
$result = mysql_query($query) or die("Couldn't Query.");
$string1 = array();
$fields = mysql_list_fields($which_db,$which_table, $link);
$num_fields = mysql_num_fields($result);
for($i = 0; $i <=$num_fields; $i++){
if($which_fields_show1 >= 0 && in_array(strval($i),$which_fields_show)){
$temp = mysql_field_name($fields, $i);
$string1[] = $temp . "= '${$temp}'";
}
}
$string2 = implode($string1,",");
$query = "UPDATE $which_table SET $string2 WHERE id='$temp_id'";
mysql_query($query) or die("couldn't query");
echo java_redirect("$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&$sort_by&&$lo$hi");
break;
default:
if(isset($sort_by) && !empty($sort_by)){
if(strpos($sort_by,"%") > 0){
$string = explode("%",$sort_by);
$query = "SELECT * FROM $which_table ORDER BY $string[0] $string[1] LIMIT $lo, 20";
}
else{
$query = "SELECT * FROM $which_table ORDER BY $sort_by LIMIT $lo, 20";
}
}
else{
$query = "SELECT * FROM $which_table LIMIT $lo, 20";
}
$result = mysql_query($query) or die("Couldn't Query.");
$fields = mysql_list_fields($which_db,$which_table, $link);
$num_fields = mysql_num_fields($result);
if(intval($which_fields1) >= 0){
$num_fields = count($which_fields);
}
if(isset($logged_in) && $logged_in && $user_level < 2){
$num_fields++;
}
//****************************** begin navigation
echo "<center><table width=\"75%\" border=0 cellpadding=0 class=\"$class4table\">\n";
echo "<tr class=\"$class4row\">\n";
if(intval($lo)-19 > 0){
$num = intval($lo) - 20;
$num2 = 20;
echo "<td align=\"left\">";
echo "<a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&$edit_action&$sort_by&$temp_id&$num&$num2\" target=\"_self\">back</a></td>\n";
}
else{
echo "<td align=\"left\"> </td>\n";
}
echo "<td align=\"center\"><small>Pages:</small> ";
for($i = 0; $i <= $tot/20; $i++){
$num = $i * 20;
$j = $i + 1;
if($i == $cur_page){
echo "[$j] ";
}
else{
echo "<a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&$edit_action&$sort_by&$temp_id&$num&$num2\" target=\"_self\">$j</a> \n";
}
}
echo "</td>\n";
if(intval($lo)+20 < $tot){
$num = intval($lo) + 20;
$num2 = 20;
echo "<td align=\"right\"><a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&$edit_action&$sort_by&$temp_id&$num&$num2\" target=\"_self\">forward</a></td>\n";
}
else{
echo "<td align=\"right\"> </td>\n";
}
echo "</tr>\n</table></center><br>\n";
//****************************** end navigation
echo "<center><table width=\"75%\" border=0 cellpadding=0 class=\"$class4table\">\n";
echo "<tr class=\"$class4alt_row\"><td colspan=$num_fields><center><b>$which_table</b></center></td></tr>\n";
echo "<tr class=\"$class4alt_row\">";
for($i = 0; $i <= $num_fields; $i++){
if($which_fields1 >= 0 && in_array(strval($i),$which_fields)){
$temp = mysql_field_name($fields, $i);
if(strcmp($sort_by,$temp . "%ASC") == 0){
$link = $temp . "%DESC";
}
else{
$link = $temp . "%ASC";
}
echo "<td><b><a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&$edit_action&$link&&$lo&$hi\" target=\"_self\">$temp</a></b></td>";
}
}
echo "</tr>\n";
$x = 0;
while($row = mysql_fetch_array($result,MYSQL_NUM)){
if($x%2==0){
echo "<tr class=\"$class4row\">\n";
}
else{
echo "<tr class=\"$class4alt_row\">\n";
}
//**link on
for($i = 0; $i < count($row); $i++){
if($which_fields1 >= 0 && in_array(strval($i),$which_fields)){
echo "<td>";
if($i==$use_num){
echo "<a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&show&$sort_by&$row[0]&$lo&$hi\" target=\"_self\">$row[$i]</a>";
//admin edit rights
// if($logged_in && intval($user_level) == $admin_level){
// echo "<br><a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&edit&$sort_by&$row[0]&$lo&$hi\" target=\"_self\">edit</a>\n";
// }
//end edit rights
}
else{
echo "$row[$i]";
}
echo "</td>\n";
}
}
//this is what i use for admin rights
//change it to what ever you want
// if($logged_in && $user_level == $admin_level){
// echo "<td><a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&delete&$sort_by&$row[0]&$lo&$hi\" target=\"_self\">delete</a></td>\n";
// }
//end admin rights
echo "</tr>\n";
$x++;
}
echo "</table>\n<table border=0 cellpadding=0 cellspacing=0 width=75%><tr><td align=\"right\" valign=\"middle\"><span style=\"font-size:7pt;color:#003399;font-weight:bold;\">© Copyright 2003, AverageJoe Web Productions</span></td></tr></table>\n";
//****************************** begin navigation
echo "<br>\n<table width=\"75%\" border=0 cellpadding=0 class=\"$class4table\">\n";
echo "<tr class=\"$class4row\">\n";
if(intval($lo)-19 > 0){
$num = intval($lo) - 20;
$num2 = 20;
echo "<td align=\"left\">";
echo "<a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&$edit_action&$sort_by&$temp_id&$num&$num2\" target=\"_self\">back</a></td>\n";
}
else{
echo "<td align=\"left\"> </td>\n";
}
echo "<td align=\"center\"><small>Pages:</small> ";
for($i = 0; $i <= $tot/20; $i++){
$num = $i * 20;
$j = $i + 1;
if($i == $cur_page){
echo "[$j] ";
}
else{
echo "<a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&$edit_action&$sort_by&$temp_id&$num&$num2\" target=\"_self\">$j</a> \n";
}
}
echo "</td>\n";
if(intval($lo)+20 < $tot){
$num = intval($lo) + 20;
$num2 = 20;
echo "<td align=\"right\"><a class=\"$class4link\" href=\"$PHP_SELF?$which_db&$which_table&$which_fields1&$which_fields_show1&$edit_action&$sort_by&$temp_id&$num&$num2\" target=\"_self\">forward</a></td>\n";
}
else{
echo "<td align=\"right\"> </td>\n";
}
echo "</tr>\n</table>\n</center>\n";
//****************************** end navigation
break;
}
//java stuff
function java_message($message){
return "<script language=\"javascript\">alert(\"$message\");</script>";
}
function java_refresh_parent(){
return "<script language=\"javascript\">\nopener.location.reload(true);</script>";
}
function java_close(){
return "<script language=\"javascript\">\nself.close();\n</script>";
}
function java_redirect($loc){
return "<script language=\"javascript\">\nwindow.location=\"$loc\";</script>";
}
function java_redirect_parent($loc){
return "<script language=\"javascript\">\nopener.location=\"$loc\";</script>";
}
?>
</body>
</html>