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
PHP Web Logs (BLogs)
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
Submit Site
Forex Trading Online forex trading platform

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 : A generic mysql database select/update/insert page for site administrators
Categories : PHP, Databases, MySQL Click here to Update Your Picture
Ofri Markus
Date : Dec 03rd 2003
Grade : 3 of 5 (graded 42 times)
Viewed : 32961
File : 3763.php
Images : No Images for this code example.
Search : More code by Ofri Markus
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?

/*
    Author - Ofri Markus
    Date   - 3/12/03
   
    This is a generic script to view and modify mysql databases.
    All you need to do to use this script is:
    1. Put it in your site, and call the file "admin.php".
    2. Fill in the connection details on the first line, replace DB_USER AND DB_PASSWORD.
    3. Fill in the database name on 2 lines, replace DB_NAME.
   
    The advantage of this script is that is you don't need to
    modify it to your own scheme. it will work on any scheme.
   
    I would be happy to receive comments and improved versions of
    this script to:
   
    markus_ofri@hotmail.com

    Enjoy!
   
*/

// Initial connection to the database
$database = mysql_connect("localhost", "DB_USER", "DB_PASSWORD");
mysql_select_db("DB_NAME");

// Because the first time we enter the site we have'nt selected
// a table to view we init the number of rows in the current table
$rowNum=0;

?>

<html>
<body bgcolor="#ffffff">

<?

// Get the data of the tables on the scheme
$result = mysql_list_tables ("DB_NAME");

$i=0;
while (
$i < mysql_num_rows ($result))
{
   
$tb_names[$i] = mysql_tablename ($result, $i);$i++;
}

// Check if we got here after pressing submit on the page
if (isset($_POST['submit']))
{
   
// If we did press the submit button, we sould view the table that was on the select
   // box
   
$submit=$_POST['submit'];
   
$table=$_POST['table'];
}
else
{
   
// If we didn't get here after pressing the submit button, check if we already
    // viewed one of the tables (and saved it's name on the hidden field hidtable
       
if (isset($_POST['hidtable']))
    {
       
$table = $_POST['hidtable'];
    }
    else
    {
       
$table="<i>not selected</i>";$xnum=0;
    }
}

?>

<center>
<form name=ff method=post action="/admin.php">
<b>Select table</b>:<select name="table">
<?
for($x=0;$x<$i;$x++)
{
?>
    <option value="<? echo $tb_names[$x];?>" <? if (isset($table) && $table==$tb_names[$x]) {echo " selected ";} ?>><? echo $tb_names[$x];?></option>
<? }?>
</select>
<input type="submit" name="submit" value="submit">



<?

// Check if we pressed the submit button and if we did - fetch the table data
if(isset($submit) || isset($_POST['hidtable'])){
$SQL="SELECT * FROM $table";
$result = mysql_query($SQL);
$xnum = mysql_num_fields($result);
$rowNum = mysql_num_rows($result);

// Read all the data in the table
for ($j = 0; $j<$rowNum; ++$j)
{
   
$row = mysql_fetch_array($result);
   
$currTable[$j]=$row;
}
}




echo
"<b><center>$table contains $xnum fields.<br><br></b></center>";?>

<center>
<table bgcolor=black><tr>
<td bgcolor="#e3e3e3">Offset</td><td bgcolor="#e3e3e3">Field Name</td><td bgcolor="#e3e3e3">Field type</td></tr>


<?
// Get the data about the primary keys and the numeric fields in the table
for($x=0;$x<$xnum;$x++)
{
   
$name[$x]=mysql_field_name($result,$x);$type[$x]=mysql_field_type($result,$x);
   
$currField = mysql_fetch_field($result,$x);
   
$key[$x]=$currField->primary_key;
   
$numeric[$x]=$currField->numeric;
?>

<tr>
<td bgcolor="white"><? echo $x;?></td>
<td bgcolor="white"><? echo $name[$x];?></td>
<td bgcolor="white"><? echo $type[$x];?></td>
</tr>
<? }?>
</table></center>
<br>

<?

// Check to see if there was an update to a row
for ($j = 0; $j<$rowNum; ++$j)
{
    if (isset(
$_POST["update".$j]))
    {
       
// Make an sql update query
       
echo "<center>There was an update to row $j</center>";
       
$sql="update $table set ";
        for (
$i = 0; $i < $xnum; ++$i)
        {
            if (
$numeric[$i]==1)
            {
               
$sql.=$name[$i]."=".$_POST["$name[$i]".$j]." ";
            }
            else
            {
               
$sql.=$name[$i]."='".$_POST["$name[$i]".$j]."' ";           
            }
           
            if (
$i != $xnum-1) {$sql.=",";}
        }
       
$sql.="WHERE ";
       
$notFirstKey = 0;
        for (
$i = 0; $i < $xnum; ++$i)
        {   
            if (
$key[$i]==1)
            {
                if (
$notFirstKey == 0) {$notFirstKey=1;}
                else {
$sql.=" AND ";}
               
$sql.=$name[$i]."=".$currTable[$j][$i];
               
            }
           
        }
        if (
$notFirstKey == 0) {
            echo
"Table does not have a primary key, not doing anything";
        }
        else {
              echo
$sql;
           
$result = mysql_query($sql);
        }

       
    }
}

// Check to see if there was a delete to a row
for ($j = 0; $j<$rowNum; ++$j)
{
    if (isset(
$_POST["delete".$j]))
    {
        echo
"<center>There was an delete to row $j</center>";
       
$sql="delete from $table ";
       
$sql.="WHERE ";
       
$notFirstKey = 0;
        for (
$i = 0; $i < $xnum; ++$i)
        {   
            if (
$key[$i]==1)
            {
                if (
$notFirstKey == 0) {$notFirstKey=1;}
                else {
$sql.=" AND ";}
               
$sql.=$name[$i]."=".$currTable[$j][$i];
               
            }
           
        }
        if (
$notFirstKey == 0) {
            echo
"Table does not have a primary key, not doing anything";
        }
        else {
              echo
$sql;
           
$result = mysql_query($sql);
        }
       
    }
}

// Check to see if there was an insert of a row
   
if (isset($_POST["insert"]))
    {
        echo
"<center>There was an insert of a row </center>";
       
$sql="insert into $table values (";
        for (
$i = 0; $i < $xnum; ++$i)
        {
           
$sql.="'".$_POST["$name[$i]"."insert"]."' ";
            if (
$i != $xnum-1) {$sql.=",";}
        }
       
$sql.=")";
          echo
$sql;
       
$result = mysql_query($sql);
       
    }




///////////////







if(isset($submit) || isset($_POST['hidtable'])){
$SQL="SELECT * FROM $table";
$result = mysql_query($SQL);
$xnum = mysql_num_fields($result);
$rowNum = mysql_num_rows($result);

}

?>




<center>
<table bgcolor=black>
<tr>
<?
for ($i=0; $i<$xnum; ++$i)
{
$name[$i]=mysql_field_name($result,$i);
?>
<td bgcolor="#e3e3e3"><? echo $name[$i]; ?></td>   
<? } ?>
</tr>

<? for ($j = 0; $j<$rowNum; ++$j)
{
$row = mysql_fetch_array($result);
$currTable[$j]=$row;
}
?>


<? for ($j = 0; $j<$rowNum; ++$j)
{
?>
<tr>
<? for ($i=0; $i<$xnum; ++$i)
{
?>
<td><input name=<? echo "\"$name[$i]".$j."\""; ?> type="text" id=<? echo "\"$name[$i]\""; ?> value=<? $currRow=$currTable[$j]; echo "\"$currRow[$i]\""; ?>></td>

<? } ?>
<td><input type="submit" name=<? echo "\"update".$j."\""; ?> value=<? echo "\"update\""; ?> height="10">
    <input type="submit" name=<? echo "\"delete".$j."\""; ?> value=<? echo "\"delete\""; ?> height="10">
</td>
</tr>
<? }
for (
$i=0; $i<$xnum; ++$i)
{
?>
<td><input name=<? echo "\"$name[$i]"."insert\""; ?> type="text" id=<? echo "\"$name[$i]\""; ?>></td>

<? } ?>

<td><input type="submit" name="insert" value="insert"></td>




</table>

</center>
<input type="hidden" name="hidtable" value=<? echo "\"$table\""; ?>>


</form>
</center>
</body>
</html>



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
 shantanu oak wrote :1037
Is it possible to place the "update" button at the end of the table? I make several changes to the data and will like to save all by clicking on a single button. And a checkbox next to each record will help me to delete several rows at a time. (Just like a delete several e-mail messages while I check my yahoo mail)