WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 : 9664
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  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
A Simple sign up script with PHP and JavaScript validations.
Categories : PHP, Java Script, MySQL, Databases
Download manager - A PHP script for adding a download page to any site.It also enables you track the no. of downloads.
Categories : PHP, Content Management, Filesystem, Databases, MySQL
Create and restore backup of MySQL databases
Categories : MySQL, Databases, PHP, PHP Classes, Complete Programs
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
Education Center is a set of PHP-scripts to administer a corporate education and examination system via Internet/intranet written in PHP for MySQL.
Categories : PHP, Databases, MySQL, Complete Programs
myCSV-dump converts a simple CSV-flatfile-database into an MySQL-dump.
Categories : PHP, MySQL, Databases, Complete Programs
email new items in db
Categories : PHP, Email, Databases, MySQL, Beginner Guides
Authorize Me! An authentication script.
Categories : MySQL, Databases, Authentication, PHP
Ajax PHP Tree (Left and Right) with MySQL
Categories : PHP, Databases, MySQL, AJAX, PHP Classes
Editing the virtusertable and sendmail.cw via PHP3.0 and Mysql
Categories : MySQL, HTML and PHP, PHP, Databases
YellowPages Content Grabber (PHP5 +)
Categories : PHP, PHP Classes, Regexps, Databases, MySQL
This function will populate the options in a drop down HTML select list in a form from a database query.
Categories : MySQL, General SQL, PHP, HTML and PHP, Databases
mysql_escape_string
Categories : PHP, MySQL, Databases, Strings
 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