AJAX MySQL DBMS
Auteur: barry - 17 maart 2006 - 13:32 - Gekeurd door: nemesiskoen - Hits: 4490 - Aantal punten: 3.40 (5 stemmen)
Geen installatie nodig...
Het bestand met een willekeurige bestandsnaam plaatsen op een webserver met php/mysql en het werkt.
Ook je Javascript moet aanstaan.
Het loginsysteem gebruikt (zoals je ziet) sessions.
Let wel dat
alle SQL-instructies werken, dus ook DROP, ALTER, DELETE, kortom
alle mogelijkheden die MySQL ondersteund, pas hier dus mee op!!
Het script is werkend getest in IE 6.0 en FireFox 1.0.7!
Code:
willekeurigebestandsnaam.php
<?
session_start();
$nArray = explode('/', strrev($_SERVER['SCRIPT_NAME']));
$fileName = strrev($nArray[0]);
define ('definedFileName', $fileName);
if ($_POST['sbmitMysqlLog']) {
if (@mysql_connect($_POST['host'], $_POST['user'], $_POST['pass'])) {
$_SESSION['host'] = $_POST['host'];
$_SESSION['user'] = $_POST['user'];
$_SESSION['pass'] = $_POST['pass'];
$_SESSION['connected'] = mktime();
header("location: ".definedFileName);
}
else {
session_destroy();
header("location: ".definedFileName."?err=notlogged");
}
}
if ($_GET['action'] == 'logout') {
session_destroy();
header("location: ".definedFileName."");
}
if ($_GET['getStuff']) {
mysql_connect($_SESSION['host'], $_SESSION['user'], $_SESSION['pass']) or die ('<b>Error!</b><br>No connection with mysql...');
if (!mysql_select_db($_GET['db'])) {
echo '<h2>Databases:</h2>';
echo ' <table border=0 style="border: 1px #000000 solid; background-color: #E0DFE3; padding: 0px; margin: 2px;">';
$r = mysql_query("SHOW DATABASES");
while ($a = mysql_fetch_array($r, MYSQL_ASSOC)) {
$c = ($c == '#CCC4E8') ? '#C4DDE8' : '#CCC4E8';
$mouseDown = "document.getElementById('db').value = '".$a['Database']."'; ";
$mouseDown .= "document.getElementById('tbls').value = 'Show tables in ".$a['Database']."'; ";
$mouseDown .= "document.getElementById('sql').value = 'SHOW TABLES'; ";
$mouseDown .= "getList('getStuff', 'sql', 'db', 'output', '".definedFileName."', 'GET');";
echo '<tr><td style="background-color: '.$c.'; cursor: pointer;" width=256 onMouseDown="'.$mouseDown.'">'.$a['Database'].'</td></tr>';
}
}
else {
if (strtoupper($_GET['getStuff']) == 'SHOW DATABASES') {
echo '<h2>Databases:</h2>';
echo ' <table border=0 style="border: 1px #000000 solid; background-color: #E0DFE3; padding: 0px; margin: 2px;">';
$r = mysql_query("SHOW DATABASES");
while ($a = mysql_fetch_array($r, MYSQL_ASSOC)) {
$c = ($c == '#CCC4E8') ? '#C4DDE8' : '#CCC4E8';
$mouseDown = "document.getElementById('db').value = '".$a['Database']."'; ";
$mouseDown .= "document.getElementById('tbls').value = 'Show tables in ".$a['Database']."'; ";
$mouseDown .= "document.getElementById('sql').value = 'SHOW TABLES'; ";
$mouseDown .= "getList('getStuff', 'sql', 'db', 'output', '".definedFileName."', 'GET');";
echo '<tr><td style="background-color: '.$c.'; cursor: pointer;" width=256 onMouseDown="'.$mouseDown.'">'.$a['Database'].'</td></tr>';
}
}
elseif (strtoupper($_GET['getStuff']) == 'SHOW TABLES') {
echo '<h2>Tables in '.$_GET['db'].':</h2>';
echo ' <table border=0 style="border: 1px #000000 solid; background-color: #E0DFE3; padding: 0px; margin: 2px;">';
$r = mysql_query("SHOW TABLES");
echo '<td style="background-color: #9D936B; font-color: #FFFFFF;"><b>Tables_in_'.$_GET['db'].'</b> </td>';
while ($a = mysql_fetch_array($r)) {
$c = ($c == '#CCC4E8') ? '#C4DDE8' : '#CCC4E8';
$mouseDown = "document.getElementById('sql').value = 'SELECT * FROM `".$a[0]."`'; ";
$mouseDown .= "getList('getStuff', 'sql', 'db', 'output', '".definedFileName."', 'GET');";
echo '<tr><td style="background-color: '.$c.'; cursor: pointer;" width=256 onMouseDown="'.$mouseDown.'">'.$a[0].'</td></tr>';
}
}
else {
$r = mysql_query(str_replace("\'", "'", $_GET['getStuff'])) or die ('<b>Error in query</b><br><pre>'.wordwrap(mysql_error(), 60).'</pre>');
if (mysql_num_rows($r) == 0) {
echo '<b>Empty table...</b>';
}
else {
$aOnce = mysql_fetch_array($r, MYSQL_ASSOC);
echo '<h2>SQL Output:</h2>';
echo ' <table border=0 style="border: 1px #000000 solid; background-color: #E0DFE3; padding: 0px; margin: 2px;"> <tr>';
foreach ($aOnce as $key => $value) {
echo '<td style="background-color: #9D936B; font-color: #FFFFFF;"><b>'.$key.'</b> </td>';
}
echo '</tr>';
$rMore = mysql_query(str_replace("\'", "'", $_GET['getStuff'])) or die ('<b>Error in query</b><br><pre>'.wordwrap(mysql_error(), 60).'</pre>');
while ($a = mysql_fetch_array($rMore, MYSQL_ASSOC)) {
echo '<tr>';
foreach ($a as $key => $value) {
$c = ($c == '#CCC4E8') ? '#C4DDE8' : '#CCC4E8';
echo '<td style="background-color: '.$c.';">'.$value.' </td>';
}
echo '</tr>';
}
}
}
}
exit;
}
?>
<html>
<head>
<title>AJAX MYSQL</title>
<style type="text/css">
<!--
a:link {
color: #000000;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #000000;
}
a:hover {
text-decoration: underline;
color: #666666;
}
a:active {
text-decoration: none;
color: #000000;
}
table.bodyTable {
border: 1px #000000 solid;
}
body {
background-color: #015BE1;
margin-left: 8px;
margin-top: 8px;
margin-right: 8px;
margin-bottom: 8px;
}
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #333333;
}
td.menuHeader {
background-color: #D5E5E0;
padding: 2px;
padding-bottom: 8px;
border-bottom: 1px #FFFFFF solid;
vertical-align: top;
}
td.center {
background-color: #DDE3E1;
padding: 2px;
padding-top: 8px;
vertical-align: top;
}
.output {
width: 100%;
background-color: #FFFFFF;
border: 1px #B1CBC2 dotted;
padding: 2px;
}
input {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 9px;
}
pre {
width: 512px;
max-width: 512px;
border: 1px #FF5519 solid;
color: #8D5541;
background-color: #EDE1DD;
}
-->
</style>
<script>
function getList(vari, theField, theField2, divv, file, method) {
// vari = $_GET waarde voor het server bestand
// theField = het veld waar hij de waardes uit gaat gebruiken
// theField2 = ,,
// divv = de div waar de output terecht komt (kan ook wat anders zijn, als het innerHTML maar ondersteund
// (zoals bijv. textarea's en span's).
// file = het bestand dat aangeroepen wordt
// method = GET of POST, GET is het meest gebruikelijke, aangezien je geen gegevens kunt posten,
// dan wordt de pagina vernieuwd namelijk...
var w3c = (typeof window.XMLHttpRequest != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');
if (w3c) {
var xmlHttp = new XMLHttpRequest();
}
else if (ie) {
var xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
return false;
}
var d = new Date();
var inp = document.getElementById(theField).value;
var inpDB = document.getElementById(theField2).value;
if (inp) {
var url = file+'?'+vari+'='+inp+'&db='+inpDB
var randomUrl = d.getFullYear()+(d.getMonth()+1)+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds()
xmlHttp.open(method, url+'&random='+randomUrl,true);
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
var content = xmlHttp.responseText;
document.getElementById(divv).innerHTML = content;
}
}
xmlHttp.send(null);
return (false);
}
}
</script>
</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" class="bodyTable">
<tr>
<td height="10" class="menuHeader">
<p>
<?
if (!$_SESSION['connected']) {
?>
<form method="post" action="">
<table align="center">
<tr>
<td width="128"><b>Host:</b></td>
<td><input name="host" type="text" value="localhost"></td>
</tr>
<tr>
<td><b>Username:</b></td>
<td><input name="user" type="text" value="root"></td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><input name="pass" type="password"></td>
</tr>
<tr>
<td> </td>
<td><input name="sbmitMysqlLog" type="submit" value="Connect"></td>
</tr>
</table>
</form>
<?
}
else {
$xhttpCmd = "getList('getStuff', 'sql', 'db', 'output', '".definedFileName."', 'GET');";
?>
SQL Instructie:
<input name="sql" type="text" id="sql" style="width: 512px;" onKeyUp="<?=$xhttpCmd;?>">
Database:
<input name="db" id="db" type="text" style="width: 128px;" readonly>
<input type="button" name="noname" value="Logout" onMouseDown="window.location='<?=definedFileName;?>?action=logout'">
<br>
<input type="button" name="noname" value="Clear DB" onMouseDown="document.getElementById('tbls').value = 'Show tables'; document.getElementById('db').value = ''; <?=$xhttpCmd;?>">
<input type="button" name="noname" value="Databases" onMouseDown="document.getElementById('sql').value = 'SHOW DATABASES'; <?=$xhttpCmd;?>">
<input type="button" name="noname" id="tbls" value="Show tables" onMouseDown="document.getElementById('sql').value = 'SHOW TABLES'; <?=$xhttpCmd;?>">
<?
}
?>
</p>
</td>
</tr>
<tr>
<td class="center">
<div id="output" class="output">
<?
if ($_GET['err'] == 'notlogged') {
echo 'Not logged in because the login settings are incorrect...';
}
elseif (!$_SESSION['connected']) {
echo 'Please login first...';
}
?>
</div>
</td>
</tr>
</table>
</body>
</html>
<?
$fileName = strrev ( $nArray [ 0 ] ) ; define ( 'definedFileName' , $fileName ) ;
if ( $_POST [ 'sbmitMysqlLog' ] ) {
if ( @ mysql_connect ( $_POST [ 'host' ] , $_POST [ 'user' ] , $_POST [ 'pass' ] ) ) { $_SESSION [ 'host' ] = $_POST [ 'host' ] ;
$_SESSION [ 'user' ] = $_POST [ 'user' ] ;
$_SESSION [ 'pass' ] = $_POST [ 'pass' ] ;
$_SESSION [ 'connected' ] = mktime ( ) ; header ( "location: " . definedFileName
) ; }
else {
header ( "location: " . definedFileName
. "?err=notlogged" ) ; }
}
if ( $_GET [ 'action' ] == 'logout' ) {
header ( "location: " . definedFileName
. "" ) ; }
if ( $_GET [ 'getStuff' ] ) {
mysql_connect ( $_SESSION [ 'host' ] , $_SESSION [ 'user' ] , $_SESSION [ 'pass' ] ) or
die ( '<b>Error!</b><br>No connection with mysql...' ) ; echo '<h2>Databases:</h2>' ; echo ' <table border=0 style="border: 1px #000000 solid; background-color: #E0DFE3; padding: 0px; margin: 2px;">' ; $c = ( $c == '#CCC4E8' ) ? '#C4DDE8' : '#CCC4E8' ;
$mouseDown = "document.getElementById('db').value = '" . $a [ 'Database' ] . "'; " ;
$mouseDown .= "document.getElementById('tbls').value = 'Show tables in " . $a [ 'Database' ] . "'; " ;
$mouseDown .= "document.getElementById('sql').value = 'SHOW TABLES'; " ;
$mouseDown .= "getList('getStuff', 'sql', 'db', 'output', '" . definedFileName. "', 'GET');" ;
echo '<tr><td style="background-color: ' . $c . '; cursor: pointer;" width=256 onMouseDown="' . $mouseDown . '">' . $a [ 'Database' ] . '</td></tr>' ; }
}
else {
if ( strtoupper ( $_GET [ 'getStuff' ] ) == 'SHOW DATABASES' ) { echo '<h2>Databases:</h2>' ; echo ' <table border=0 style="border: 1px #000000 solid; background-color: #E0DFE3; padding: 0px; margin: 2px;">' ; $c = ( $c == '#CCC4E8' ) ? '#C4DDE8' : '#CCC4E8' ;
$mouseDown = "document.getElementById('db').value = '" . $a [ 'Database' ] . "'; " ;
$mouseDown .= "document.getElementById('tbls').value = 'Show tables in " . $a [ 'Database' ] . "'; " ;
$mouseDown .= "document.getElementById('sql').value = 'SHOW TABLES'; " ;
$mouseDown .= "getList('getStuff', 'sql', 'db', 'output', '" . definedFileName. "', 'GET');" ;
echo '<tr><td style="background-color: ' . $c . '; cursor: pointer;" width=256 onMouseDown="' . $mouseDown . '">' . $a [ 'Database' ] . '</td></tr>' ; }
}
elseif ( strtoupper ( $_GET [ 'getStuff' ] ) == 'SHOW TABLES' ) { echo '<h2>Tables in ' . $_GET [ 'db' ] . ':</h2>' ; echo ' <table border=0 style="border: 1px #000000 solid; background-color: #E0DFE3; padding: 0px; margin: 2px;">' ; echo '<td style="background-color: #9D936B; font-color: #FFFFFF;"><b>Tables_in_' . $_GET [ 'db' ] . '</b> </td>' ; $c = ( $c == '#CCC4E8' ) ? '#C4DDE8' : '#CCC4E8' ;
$mouseDown = "document.getElementById('sql').value = 'SELECT * FROM `" . $a [ 0 ] . "`'; " ;
$mouseDown .= "getList('getStuff', 'sql', 'db', 'output', '" . definedFileName. "', 'GET');" ;
echo '<tr><td style="background-color: ' . $c . '; cursor: pointer;" width=256 onMouseDown="' . $mouseDown . '">' . $a [ 0 ] . '</td></tr>' ; }
}
else {
echo '<b>Empty table...</b>' ; }
else {
echo '<h2>SQL Output:</h2>' ; echo ' <table border=0 style="border: 1px #000000 solid; background-color: #E0DFE3; padding: 0px; margin: 2px;"> <tr>' ; foreach ( $aOnce as $key => $value ) {
echo '<td style="background-color: #9D936B; font-color: #FFFFFF;"><b>' . $key . '</b> </td>' ; }
foreach ( $a as $key => $value ) {
$c = ( $c == '#CCC4E8' ) ? '#C4DDE8' : '#CCC4E8' ;
echo '<td style="background-color: ' . $c . ';">' . $value . ' </td>' ; }
}
}
}
}
}
?>
<html>
<head>
<title>AJAX MYSQL</title>
<style type="text/css">
<!--
a:link {
color: #000000;
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #000000;
}
a:hover {
text-decoration: underline;
color: #666666;
}
a:active {
text-decoration: none;
color: #000000;
}
table.bodyTable {
border: 1px #000000 solid;
}
body {
background-color: #015BE1;
margin-left: 8px;
margin-top: 8px;
margin-right: 8px;
margin-bottom: 8px;
}
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #333333;
}
td.menuHeader {
background-color: #D5E5E0;
padding: 2px;
padding-bottom: 8px;
border-bottom: 1px #FFFFFF solid;
vertical-align: top;
}
td.center {
background-color: #DDE3E1;
padding: 2px;
padding-top: 8px;
vertical-align: top;
}
.output {
width: 100%;
background-color: #FFFFFF;
border: 1px #B1CBC2 dotted;
padding: 2px;
}
input {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 9px;
}
pre {
width: 512px;
max-width: 512px;
border: 1px #FF5519 solid;
color: #8D5541;
background-color: #EDE1DD;
}
-->
</style>
<script>
function getList(vari, theField, theField2, divv, file, method) {
// vari = $_GET waarde voor het server bestand
// theField = het veld waar hij de waardes uit gaat gebruiken
// theField2 = ,,
// divv = de div waar de output terecht komt (kan ook wat anders zijn, als het innerHTML maar ondersteund
// (zoals bijv. textarea's en span's).
// file = het bestand dat aangeroepen wordt
// method = GET of POST, GET is het meest gebruikelijke, aangezien je geen gegevens kunt posten,
// dan wordt de pagina vernieuwd namelijk...
var w3c = (typeof window.XMLHttpRequest != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');
if (w3c) {
var xmlHttp = new XMLHttpRequest();
}
else if (ie) {
var xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
return false;
}
var d = new Date();
var inp = document.getElementById(theField).value;
var inpDB = document.getElementById(theField2).value;
if (inp) {
var url = file+'?'+vari+'='+inp+'&db='+inpDB
var randomUrl = d.getFullYear()+(d.getMonth()+1)+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds()
xmlHttp.open(method, url+'&random='+randomUrl,true);
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4) {
var content = xmlHttp.responseText;
document.getElementById(divv).innerHTML = content;
}
}
xmlHttp.send(null);
return (false);
}
}
</script>
</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" class="bodyTable">
<tr>
<td height="10" class="menuHeader">
<p>
<?
if ( ! $_SESSION [ 'connected' ] ) {
?>
<form method="post" action="">
<table align="center">
<tr>
<td width="128"><b>Host:</b></td>
<td><input name="host" type="text" value="localhost"></td>
</tr>
<tr>
<td><b>Username:</b></td>
<td><input name="user" type="text" value="root"></td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><input name="pass" type="password"></td>
</tr>
<tr>
<td> </td>
<td><input name="sbmitMysqlLog" type="submit" value="Connect"></td>
</tr>
</table>
</form>
<?
}
else {
$xhttpCmd = "getList('getStuff', 'sql', 'db', 'output', '" . definedFileName. "', 'GET');" ;
?>
SQL Instructie:
<input name="sql" type="text" id="sql" style="width: 512px;" onKeyUp="<?= $xhttpCmd ; ?> ">
Database:
<input name="db" id="db" type="text" style="width: 128px;" readonly>
<input type="button" name="noname" value="Logout" onMouseDown="window.location='<? = definedFileName; ?> ?action=logout'">
<br>
<input type="button" name="noname" value="Clear DB" onMouseDown="document.getElementById('tbls').value = 'Show tables'; document.getElementById('db').value = ''; <?= $xhttpCmd ; ?> ">
<input type="button" name="noname" value="Databases" onMouseDown="document.getElementById('sql').value = 'SHOW DATABASES'; <?= $xhttpCmd ; ?> ">
<input type="button" name="noname" id="tbls" value="Show tables" onMouseDown="document.getElementById('sql').value = 'SHOW TABLES'; <?= $xhttpCmd ; ?> ">
<?
}
?>
</p>
</td>
</tr>
<tr>
<td class="center">
<div id="output" class="output">
<?
if ( $_GET [ 'err' ] == 'notlogged' ) {
echo 'Not logged in because the login settings are incorrect...' ; }
elseif ( ! $_SESSION [ 'connected' ] ) {
echo 'Please login first...' ; }
?>
</div>
</td>
</tr>
</table>
</body>
</html>
Download code (.txt)
Stemmen
Niet ingelogd.