WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
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 Update Picture
Benedetto Patrice
Date : Jul 17th 2000
Grade : 2 of 5 (graded 2 times)
Viewed : 8843
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

  Submit your own code examples 
 

<?
/* 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




bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
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
Password reminder
Categories : PHP, PHP Classes, Databases, MySQL, Mail
Identify and log search engine access (spiders, robots, etc.) to a page.
Categories : HTTP, Environment Variables, PHP, MySQL, Databases
A simple script to count and report hits and the last modification time of an HTML page. Requires MySQL support (other DBs should work too, except possibly mSQL).
Categories : HTTP, MySQL, PHP, Databases
create a grid out of <INPUT TYPE=TEXT> then saving to a database. Uses a 'multi-dimension array', but not really as the array is just one big array with the index of "[$i][$j]". Have a look at the code and you'll see what I mean.
Categories : PHP, MySQL, Arrays, Databases
php-gtk mysql querying tool
Categories : PHP-GTK, MySQL, PHP, Databases
Simple conversion functions to change MySQL dates to arrays, arrays to MySQL dates.
Categories : PHP, Arrays, Date Time, Databases, MySQL
Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides
dynamic table columns
Categories : PHP, HTML and PHP, Arrays, Databases, MySQL
mediaCat-GTK v2.0.0 - an mp3/cd/dvd cataloging utility written in php-gtk which interfaces with mysql and ms access (or db supported by PHP's Unified ODBC Functions)
Categories : PHP, MySQL, MS Access, Utilities, Databases
Warning: Unknown(): A session is active. You cannot change the session module's ini settings at this time. in Unknown on line 0
Categories : PHP, Sessions, Databases, MySQL
Pull Down Surfing - Surf on Change
Categories : Java Script, MySQL, HTML and PHP, PHP, Databases
Online Automatic Class Generator for MySQL Tables
Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL
Cut your MySQL Connections to 1 line of code
Categories : PHP, Beginner Guides, Databases, MySQL
 benedetto patrice wrote : 412
  Reading this code i saw an error  there change 
$len = ($len&gt;40)? 40 : $len ;  
            echo "&lt;input type=text name=`$field` value=\"".$special_chars."\" size =\"$len\" 
maxlength=$len&gt;";
by ....
$limit = ($len&gt;40)? 40 : $len ;  
            echo "&lt;input type=text name=`$field` value=\"".$special_chars."\" size =\"$limit 
\" maxlength=$len&gt;";
 
 
 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
-----------------------------------------  
&lt;?
echo "&lt;html&gt;&lt;head&gt;
-----------------------------------------  
and
-----------------------------------------  
&lt;head&gt;&lt;body&gt;" ; 
-----------------------------------------  

NEW:
-----------------------------------------  
?&gt;
&lt;html&gt;&lt;head&gt;
-----------------------------------------  
and
-----------------------------------------  
 &lt;head&gt;&lt;body&gt;
&lt;?
-----------------------------------------  

OLD :
- much more generik this way ;)
- allows you to name it whatever and it`ll work

-----------------------------------------  
&lt;form method="post" action="generik.php3"&gt;
-----------------------------------------    

NEW :
-----------------------------------------  
&lt;form method="post" action="&lt;? echo $PHP_SELF ?&gt;"&gt;
-----------------------------------------  


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