|
|
|
| Title : |
Tired to edit forms? this simple script will edit or update ANY MySQL database.It can handle set, enum, textarea and creat and update date fields
data. |
| Categories : |
MySQL, Databases, PHP |
 Benedetto Patrice |
| Date : |
Jul 17th 2000 |
| Grade : |
2 of 5 (graded 2 times) |
| Viewed : |
8214 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Benedetto Patrice |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
<?
/* Original version July 2000 Patrice Benedetto webmestre@labynet.org */
/* Please use and reuse this script as you want but don't forget credit */
/* feel free to tell or show me some improvement of your own */
/* as bugs you may find thank you */
/* this is a sheap script , with comments it will be more expensive */
/* B-Z stand for Be Zen , join the BZ project :))) */
/* USE : generik.php3?db=BASE&table=TABLE&kn=USED_INDEX_NAME&key=ID_OF_INDEX */
<?
echo "<html><head>
<STYLE TYPE='text/css'>
.Legende { background-color: #CCCCCC; }
.fond { background-color: #000000; }
.Data { background-color: #EEEEEE; }
.foot { background-color: #FFFFFF; }
P.titre
{
font-family: helvetica, arial, geneva, sans-serif;
color: #003300;
font-weight: 800;
}
</STYLE>
<head><body>" ;
$mysql_link = mysql_connect("localhost", $db, "PUT YOUR PASSWORD HERE ");
mysql_select_db($db, $mysql_link);
/*****************************************/
function BZ_ShowSet ( $row_table_def , $data )
{
$field = $row_table_def["Field"];
$set = str_replace("set(", "", $row_table_def["Type"]);
$set = ereg_replace("\)$", "", $set);
$set = explode (",", $set ) ;
for ($vals = explode(",", $data); list($t, $k) = each($vals);) {
$vset[$k] = 1;
}
$size = min(4, count($set));
for ($j=0; $j<count($set);$j++)
{
echo "<INPUT TYPE='checkbox' name=$field$j option value='".htmlspecialchars(substr($set[$j], 1, -1))."'";
if ($vset[substr($set[$j], 1, -1)])
echo " checked";
echo "> ".htmlspecialchars(substr($set[$j], 1, -1))." <br> \n";
}
}
/*****************************************/
function BZ_ShowEnum ( $row_table_def , $data )
{
$field = $row_table_def["Field"];
$set = str_replace("enum(", "", $row_table_def["Type"]);
$set = ereg_replace("\)$", "", $set);
$set = explode (",", $set ) ;
echo "<select name='$field'>\n";
echo "<option value=\"\">\n";
for ($j=0; $j<count($set);$j++)
{
echo '<option value="'.substr($set[$j], 1, -1).'"';
if ($data == substr($set[$j], 1, -1) || ($data == "" && substr($set[$j], 1, -1) == $row_table_def["Default"]))
echo " selected";
echo ">".htmlspecialchars(substr($set[$j], 1, -1))."\n";
}
echo "</select>";
}
/*****************************************/
function BZ_CalcSet ($setName )
{
$out = 0 ;
$bitVal = 1 ;
for ( $bit = 0 ; $bit < 64 ; ++$bit )
{
$theGlobalName = $setName.$bit ;
eval ( "global $"."$theGlobalName ; " ) ;
$theString = "$"."thevalue = $".$theGlobalName.";" ;
eval ( $theString );
if ( $thevalue ) { $out |= $bitVal ; } ;
$bitVal<<= 1;
}
return $out ;
}
/**************** START ******************/
$table_def = mysql_db_query($db , "SHOW FIELDS FROM $table") ;
if (isset($action))
{
if ( $table_def )
{
if( $action =="update" ) $query =" UPDATE $table SET " ;
if( $action =="write" ) $query =" INSERT into $table VALUES ( " ;
$virgule = "" ;
for ($i=0;$i<mysql_num_rows($table_def);$i++)
{
$row_table_def = mysql_fetch_array($table_def);
$field = $row_table_def["Field"];
if ( "TIME_UPDT" == $field )
{
$value = date("Y-m-d H:i:s", time());
$theString = "$"."$field = '".$value ."';" ;
eval ($theString) ;
}
if ($action =="write" && "TIME_CREA" == $field )
{
$value = date("Y-m-d H:i:s", time());
$theString = "$"."$field = '".$value ."';" ;
eval ($theString) ;
}
$theString = "$"."value = $"."$field ; " ;
switch (ereg_replace("\\(.*", "", $row_table_def['Type'])) {
case "set":
$value = BZ_CalcSet ( $field) ;
if( $action =="update" ) $query .= "$virgule $field = (($field &0 ) | $value ) ";
if( $action =="write" ) $query .= "$virgule $value ";
break;
default:
eval ( $theString );
$value = addslashes ($value );
if( $action =="write" ) $query .= "$virgule '$value' ";
if( $action =="update" ) $query .= "$virgule $field= '$value' ";
break;
}
$virgule ="," ;
}
}
if( $action =="update" ) $query .= "WHERE ID = '$ID' " ;
if( $action =="write" ) $query .= ")" ;
if ( mysql_query ( $query ) )
{
echo "<h1>Success </h1>" ;
}
else
{
echo "<h1>Wrong query </h1>" ;
}
echo $query ;
exit ;
}
if (isset($kn))
{
$result = mysql_db_query($db, "SELECT * FROM $table WHERE $kn = '$key' ");
$row = mysql_fetch_array($result);
}
else
{
$result = mysql_db_query($db, "SELECT * FROM $table LIMIT 1");
}
if (!$table_def)
{
exit();
}
else
{
echo " <tr> <td class='bar'> ";
echo "<p class='titre'> Edit a ". $table ." record on database ".$db." </p> </td></tr> ";
echo " <tr> <td class='fond'> ";
?>
<form method="post" action="generik.php3">
<input type="hidden" name="server" value="<?php echo $server;?>">
<input type="hidden" name="db" value="<?php echo $db;?>">
<input type="hidden" name="table" value="<?php echo $table;?>">
<input type="hidden" name="goto" value="<?php echo $goto;?>">
<?php
if ( $key !="" )
{
echo '<input type="hidden" name="kn" value="' . $kn . '">' ;
echo '<input type="hidden" name="key" value="' . $key . '">' ;
echo '<input type="hidden" name="action" value="update">' ;
}
else
{
echo '<input type="hidden" name="action" value="write">' ;
}
echo "\n" ;
echo " <table border=0 cellspacing='1' bgcolor='#FFFFF' WIDTH='100%' > " ;
echo "\n" ;
for ($i=0;$i<mysql_num_rows($table_def);$i++)
{
$row_table_def = mysql_fetch_array($table_def);
$field = $row_table_def["Field"];
$len = mysql_field_len($result,$i);
if( ! ereg ("ID" , $field ) )
{
echo "<tr><td class='Legende".$a."' > <STRONG> ". ereg_replace ("_"," ", $field ). " </STRONG> </td>\n";
}
switch (ereg_replace("\\(.*", "", $row_table_def['Type']))
{
case "set":
$type = "set";
break;
case "enum":
$type = "enum";
break;
default:
$type = $row_table_def['Type'];
break;
}
if (isset($row) && isset($row[$field])) {
$special_chars = htmlspecialchars($row[$field]);
$data = $row[$field];
}
/******** HERE you have to check for some condition to HIDE some fields *****/
if( ! ereg ("ID" , $field ) )
{
echo "<td class=Data >" ;
if (strstr($row_table_def["Type"], "text"))
{
echo "<textarea name='$field' ROWS= 6 COLS=32 >$special_chars</textarea>\n";
}
elseif (strstr($row_table_def["Type"], "enum"))
{
BZ_ShowEnum ( $row_table_def , $data ) ;
}
elseif (strstr($row_table_def["Type"], "set"))
{
BZ_ShowSet ( $row_table_def , $data ) ;
}
else
{
$len = ($len>40)? 40 : $len ;
echo "<input type=text name='$field' value=\"".$special_chars."\" size =\"$len\" maxlength=$len>";
}
echo "</td></tr>\n";
}
else
{
echo "<input type=hidden name='$field' value=\"".$special_chars."\" >";
}
}
echo "</table>\n";
}
echo " <center> <input type='submit' value='OK'></center> \n </form> \n</td></tr> \n </body>\n<html>" ;
?>
testpage : http://generik.labynet.org/generik.php3?db=labynet_org&table=generik&kn=ID
|
|
| This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs. Categories : Databases, PHP, MySQL, Complete Programs | | | phpAds, a complete banner and ad management system with detailled tracking and stats. Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases | | | Point and Click Interface ala MS Access for creating SQL statements. Categories : MySQL, Complete Programs, General SQL, PHP, Databases | | | Message of the Day - Random Message (Needs MySQL!) Categories : Databases, HTML and PHP, PHP, MySQL | | | email new items in db Categories : PHP, Email, Databases, MySQL, Beginner Guides | | | Alternating background color for HTML table rows Categories : PHP, Databases, MySQL, HTML and PHP | | | color codes for positive and negative numbers Categories : PHP, MySQL, Databases, HTML | | | Authorize Me! An authentication script. Categories : MySQL, Databases, Authentication, PHP | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Linked comboboxes with php-mysql & javascript Categories : PHP, Java Script, Databases, MySQL | | | mysql_escape_string Categories : PHP, MySQL, Databases, Strings | | | Automatically printing the contents of an sql table in MySQL. Categories : MySQL, PHP, HTML and PHP, Databases | | | usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | |
| | | | benedetto patrice wrote : 412
Reading this code i saw an error there change
$len = ($len>40)? 40 : $len ;
echo "<input type=text name=`$field` value=\"".$special_chars."\" size =\"$len\"
maxlength=$len>";
by ....
$limit = ($len>40)? 40 : $len ;
echo "<input type=text name=`$field` value=\"".$special_chars."\" size =\"$limit
\" maxlength=$len>";
| | | | Philip Olson wrote :414
things i did to get this script to work:
OLD:
- if you have no username/password to define, just remove
the variables
- you can define $db here or in url and maybe the
username/password/host too, i haven`t tried
-----------------------------------------
$mysql_link = mysql_connect("localhost", $db, "PUT YOUR
PASSWORD HERE ");
mysql_select_db($db, $mysql_link);
-----------------------------------------
NEW:
-----------------------------------------
$dbhost = "localhost"; // define this
$dbuser = "myusername"; // define this
$dbpass = "mypassword"; // define this
$mysql_link = mysql_connect
("$dbhost", "$dbuser", "$dbpass");
mysql_select_db($db);
-----------------------------------------
OLD:
- i prefer putting large blocks of html as html vs echo
-----------------------------------------
<?
echo "<html><head>
-----------------------------------------
and
-----------------------------------------
<head><body>" ;
-----------------------------------------
NEW:
-----------------------------------------
?>
<html><head>
-----------------------------------------
and
-----------------------------------------
<head><body>
<?
-----------------------------------------
OLD :
- much more generik this way ;)
- allows you to name it whatever and it`ll work
-----------------------------------------
<form method="post" action="generik.php3">
-----------------------------------------
NEW :
-----------------------------------------
<form method="post" action="<? echo $PHP_SELF ?>">
-----------------------------------------
and everything now works great. good luck! also, we`ll see if
this
formatting style makes sense (with all the -----`s) and if it
does
not i won`t duplicate in the future ;)
philip olson
theprojects.org
| |
|
|
|