PHP - Easy Database Manager
From Global Programming Syntax
Sometimes you may have a database but want a form generated so that every entry in the database will appear in one large form. Well below is a script which will allow you to update all the data within the table of the database of your selection and after you have altered the fields, just click submit. It's that simple. So to configure the script, just adjust the variables at the very top of the script.
<?
//configure below variables
$dbhost='localhost';
$accountname='root';
$password='';
$database='my database';
$table="my table";
mysql_connect($dbhost,$accountname,$password)
or die("Could not connect to MySQL server");
mysql_select_db($database) or die(mysql_error()."Could not select database");
$result=mysql_query("SELECT * FROM `".$table."`");
if (isset($_POST['update']))
{
$headings=array();
$data=$_POST['update'];
foreach($data AS $key => $value) { $headings[]=$key; } unset ($key); unset ($value);
$rowid=0;
mysql_query("DELETE FROM `".$table."`") or die(mysql_error());
while (isset($data[$headings[0]][$rowid]))
{
$sql.="INSERT INTO `".mysql_real_escape_string($table)."` SET";
$headid=0;
while (isset($headings[$headid]))
{
$sql.=" `".$headings[$headid]."` = '".mysql_real_escape_string($data[$headings[$headid]][$rowid])."',";
$headid+=1;
}
$sql=preg_replace('/(.*),/','$1',$sql);
mysql_query($sql) or print mysql_error();
unset($sql);
$rowid+=1;
}
$sql=preg_replace('/(.*)[;][ ]/','$1',$sql);
unset($_POST['update']);
$currentpage=preg_replace('/.*\/([^\/]+)?/','$1',$_SERVER['REQUEST_URI']);
if ($currentpage=='') {$currentpage='./'; }
header('Location: '.$currentpage);
}
echo preg_replace('/.*\/([^\/]+)?/','$1',$_SERVER['REQUEST_URI']);
$field = array_keys( mysql_fetch_array(mysql_query("SELECT * FROM `".mysql_real_escape_string($table)."`"), MYSQL_ASSOC));
echo "<form method='post' style='margin:0px padding:0px;'>
<table border=1 cellpadding=3 cellspacing=0><tr>";
foreach ($field AS $column)
{
echo "<td bgcolor='#CCCCCC'>".$column."</td>";
}
unset($column);
echo "</tr>";
$rowid=0;
while ($row=mysql_fetch_array($result))
{
echo "<tr>";
foreach ($field AS $column)
{
echo "<td><input type='text' style='width:180px;' value=\"".str_replace('>','>',$row[$column]).
"\" name='update[".str_replace("'",'\\\'',$column)."][".$rowid."]'></td>";
}
unset($column);
echo "</tr>";
$rowid+=1;
}
echo "<tr><td colspan=".count($field)." align='right'><input type='submit' value='submit'></td></tr></table></form>";
?>
