login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > Overige > AJAX > AJAX MySQL DBMS

AJAX MySQL DBMS

Auteur: barry - 17 maart 2006 - 13:32 - Gekeurd door: nemesiskoen - Hits: 4421 - 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
  1. <?
  2.  
  3. $nArray = explode('/', strrev($_SERVER['SCRIPT_NAME']));
  4. $fileName = strrev($nArray[0]);
  5. define ('definedFileName', $fileName);
  6.  
  7. if ($_POST['sbmitMysqlLog']) {
  8. if (@mysql_connect($_POST['host'], $_POST['user'], $_POST['pass'])) {
  9. $_SESSION['host'] = $_POST['host'];
  10. $_SESSION['user'] = $_POST['user'];
  11. $_SESSION['pass'] = $_POST['pass'];
  12. $_SESSION['connected'] = mktime();
  13. header("location: ".definedFileName);
  14. }
  15. else {
  16. header("location: ".definedFileName."?err=notlogged");
  17. }
  18. }
  19.  
  20. if ($_GET['action'] == 'logout') {
  21. header("location: ".definedFileName."");
  22. }
  23.  
  24. if ($_GET['getStuff']) {
  25. mysql_connect($_SESSION['host'], $_SESSION['user'], $_SESSION['pass']) or die ('<b>Error!</b><br>No connection with mysql...');
  26. if (!mysql_select_db($_GET['db'])) {
  27. echo '<h2>Databases:</h2>';
  28. echo ' <table border=0 style="border: 1px #000000 solid; background-color: #E0DFE3; padding: 0px; margin: 2px;">';
  29. $r = mysql_query("SHOW DATABASES");
  30. while ($a = mysql_fetch_array($r, MYSQL_ASSOC)) {
  31. $c = ($c == '#CCC4E8') ? '#C4DDE8' : '#CCC4E8';
  32. $mouseDown = "document.getElementById('db').value = '".$a['Database']."'; ";
  33. $mouseDown .= "document.getElementById('tbls').value = 'Show tables in ".$a['Database']."'; ";
  34. $mouseDown .= "document.getElementById('sql').value = 'SHOW TABLES'; ";
  35. $mouseDown .= "getList('getStuff', 'sql', 'db', 'output', '".definedFileName."', 'GET');";
  36. echo '<tr><td style="background-color: '.$c.'; cursor: pointer;" width=256 onMouseDown="'.$mouseDown.'">'.$a['Database'].'</td></tr>';
  37. }
  38.  
  39. }
  40. else {
  41. if (strtoupper($_GET['getStuff']) == 'SHOW DATABASES') {
  42. echo '<h2>Databases:</h2>';
  43. echo ' <table border=0 style="border: 1px #000000 solid; background-color: #E0DFE3; padding: 0px; margin: 2px;">';
  44. $r = mysql_query("SHOW DATABASES");
  45. while ($a = mysql_fetch_array($r, MYSQL_ASSOC)) {
  46. $c = ($c == '#CCC4E8') ? '#C4DDE8' : '#CCC4E8';
  47. $mouseDown = "document.getElementById('db').value = '".$a['Database']."'; ";
  48. $mouseDown .= "document.getElementById('tbls').value = 'Show tables in ".$a['Database']."'; ";
  49. $mouseDown .= "document.getElementById('sql').value = 'SHOW TABLES'; ";
  50. $mouseDown .= "getList('getStuff', 'sql', 'db', 'output', '".definedFileName."', 'GET');";
  51. echo '<tr><td style="background-color: '.$c.'; cursor: pointer;" width=256 onMouseDown="'.$mouseDown.'">'.$a['Database'].'</td></tr>';
  52. }
  53. }
  54.  
  55. elseif (strtoupper($_GET['getStuff']) == 'SHOW TABLES') {
  56. echo '<h2>Tables in '.$_GET['db'].':</h2>';
  57. echo ' <table border=0 style="border: 1px #000000 solid; background-color: #E0DFE3; padding: 0px; margin: 2px;">';
  58. $r = mysql_query("SHOW TABLES");
  59. echo '<td style="background-color: #9D936B; font-color: #FFFFFF;"><b>Tables_in_'.$_GET['db'].'</b>&nbsp;</td>';
  60. while ($a = mysql_fetch_array($r)) {
  61. $c = ($c == '#CCC4E8') ? '#C4DDE8' : '#CCC4E8';
  62. $mouseDown = "document.getElementById('sql').value = 'SELECT * FROM `".$a[0]."`'; ";
  63. $mouseDown .= "getList('getStuff', 'sql', 'db', 'output', '".definedFileName."', 'GET');";
  64. echo '<tr><td style="background-color: '.$c.'; cursor: pointer;" width=256 onMouseDown="'.$mouseDown.'">'.$a[0].'</td></tr>';
  65. }
  66. }
  67.  
  68. else {
  69. $r = mysql_query(str_replace("\'", "'", $_GET['getStuff'])) or die ('<b>Error in query</b><br><pre>'.wordwrap(mysql_error(), 60).'</pre>');
  70. if (mysql_num_rows($r) == 0) {
  71. echo '<b>Empty table...</b>';
  72. }
  73. else {
  74. $aOnce = mysql_fetch_array($r, MYSQL_ASSOC);
  75. echo '<h2>SQL Output:</h2>';
  76. echo ' <table border=0 style="border: 1px #000000 solid; background-color: #E0DFE3; padding: 0px; margin: 2px;"> <tr>';
  77. foreach ($aOnce as $key => $value) {
  78. echo '<td style="background-color: #9D936B; font-color: #FFFFFF;"><b>'.$key.'</b>&nbsp;</td>';
  79. }
  80. echo '</tr>';
  81. $rMore = mysql_query(str_replace("\'", "'", $_GET['getStuff'])) or die ('<b>Error in query</b><br><pre>'.wordwrap(mysql_error(), 60).'</pre>');
  82. while ($a = mysql_fetch_array($rMore, MYSQL_ASSOC)) {
  83. echo '<tr>';
  84. foreach ($a as $key => $value) {
  85. $c = ($c == '#CCC4E8') ? '#C4DDE8' : '#CCC4E8';
  86. echo '<td style="background-color: '.$c.';">'.$value.'&nbsp;</td>';
  87. }
  88. echo '</tr>';
  89. }
  90. }
  91. }
  92. }
  93.  
  94. }
  95. ?>
  96.  
  97. <html>
  98. <head>
  99. <title>AJAX MYSQL</title>
  100. <style type="text/css">
  101. <!--
  102. a:link {
  103. color: #000000;
  104. text-decoration: none;
  105. }
  106. a:visited {
  107. text-decoration: none;
  108. color: #000000;
  109. }
  110. a:hover {
  111. text-decoration: underline;
  112. color: #666666;
  113. }
  114. a:active {
  115. text-decoration: none;
  116. color: #000000;
  117. }
  118.  
  119. table.bodyTable {
  120. border: 1px #000000 solid;
  121. }
  122.  
  123. body {
  124. background-color: #015BE1;
  125. margin-left: 8px;
  126. margin-top: 8px;
  127. margin-right: 8px;
  128. margin-bottom: 8px;
  129. }
  130.  
  131. body,td,th {
  132. font-family: Arial, Helvetica, sans-serif;
  133. font-size: 12px;
  134. color: #333333;
  135. }
  136.  
  137. td.menuHeader {
  138. background-color: #D5E5E0;
  139. padding: 2px;
  140. padding-bottom: 8px;
  141. border-bottom: 1px #FFFFFF solid;
  142. vertical-align: top;
  143. }
  144.  
  145. td.center {
  146. background-color: #DDE3E1;
  147. padding: 2px;
  148. padding-top: 8px;
  149. vertical-align: top;
  150. }
  151.  
  152. .output {
  153. width: 100%;
  154. background-color: #FFFFFF;
  155. border: 1px #B1CBC2 dotted;
  156. padding: 2px;
  157. }
  158.  
  159. input {
  160. font-family: Verdana, Arial, Helvetica, sans-serif;
  161. font-size: 9px;
  162. }
  163.  
  164. pre {
  165. width: 512px;
  166. max-width: 512px;
  167. border: 1px #FF5519 solid;
  168. color: #8D5541;
  169. background-color: #EDE1DD;
  170. }
  171. -->
  172. </style>
  173.  
  174. <script>
  175. function getList(vari, theField, theField2, divv, file, method) {
  176.  
  177. // vari = $_GET waarde voor het server bestand
  178. // theField = het veld waar hij de waardes uit gaat gebruiken
  179. // theField2 = ,,
  180. // divv = de div waar de output terecht komt (kan ook wat anders zijn, als het innerHTML maar ondersteund
  181. // (zoals bijv. textarea's en span's).
  182. // file = het bestand dat aangeroepen wordt
  183. // method = GET of POST, GET is het meest gebruikelijke, aangezien je geen gegevens kunt posten,
  184. // dan wordt de pagina vernieuwd namelijk...
  185.  
  186.  
  187. var w3c = (typeof window.XMLHttpRequest != 'undefined');
  188. var ie = (typeof window.ActiveXObject != 'undefined');
  189. if (w3c) {
  190. var xmlHttp = new XMLHttpRequest();
  191. }
  192. else if (ie) {
  193. var xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  194. }
  195. else {
  196. return false;
  197. }
  198.  
  199. var d = new Date();
  200. var inp = document.getElementById(theField).value;
  201. var inpDB = document.getElementById(theField2).value;
  202.  
  203. if (inp) {
  204. var url = file+'?'+vari+'='+inp+'&db='+inpDB
  205. var randomUrl = d.getFullYear()+(d.getMonth()+1)+d.getDate()+d.getHours()+d.getMinutes()+d.getSeconds()
  206. xmlHttp.open(method, url+'&random='+randomUrl,true);
  207.  
  208. xmlHttp.onreadystatechange = function() {
  209. if (xmlHttp.readyState == 4) {
  210. var content = xmlHttp.responseText;
  211. document.getElementById(divv).innerHTML = content;
  212. }
  213. }
  214.  
  215. xmlHttp.send(null);
  216. return (false);
  217. }
  218. }
  219. </script>
  220.  
  221.  
  222. </head>
  223. <body>
  224. <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0" class="bodyTable">
  225. <tr>
  226. <td height="10" class="menuHeader">
  227. <p>
  228. <?
  229. if (!$_SESSION['connected']) {
  230. ?>
  231. <form method="post" action="">
  232. <table align="center">
  233. <tr>
  234. <td width="128"><b>Host:</b></td>
  235. <td><input name="host" type="text" value="localhost"></td>
  236. </tr>
  237. <tr>
  238. <td><b>Username:</b></td>
  239. <td><input name="user" type="text" value="root"></td>
  240. </tr>
  241. <tr>
  242. <td><b>Password:</b></td>
  243. <td><input name="pass" type="password"></td>
  244. </tr>
  245. <tr>
  246. <td>&nbsp;</td>
  247. <td><input name="sbmitMysqlLog" type="submit" value="Connect"></td>
  248. </tr>
  249. </table>
  250. </form>
  251. <?
  252. }
  253. else {
  254. $xhttpCmd = "getList('getStuff', 'sql', 'db', 'output', '".definedFileName."', 'GET');";
  255. ?>
  256. SQL Instructie:
  257. <input name="sql" type="text" id="sql" style="width: 512px;" onKeyUp="<?=$xhttpCmd;?>">
  258. Database:
  259. <input name="db" id="db" type="text" style="width: 128px;" readonly>
  260. <input type="button" name="noname" value="Logout" onMouseDown="window.location='<?=definedFileName;?>?action=logout'">
  261. <br>
  262. <input type="button" name="noname" value="Clear DB" onMouseDown="document.getElementById('tbls').value = 'Show tables'; document.getElementById('db').value = ''; <?=$xhttpCmd;?>">
  263. <input type="button" name="noname" value="Databases" onMouseDown="document.getElementById('sql').value = 'SHOW DATABASES'; <?=$xhttpCmd;?>">
  264. <input type="button" name="noname" id="tbls" value="Show tables" onMouseDown="document.getElementById('sql').value = 'SHOW TABLES'; <?=$xhttpCmd;?>">
  265. <?
  266. }
  267. ?>
  268. </p>
  269. </td>
  270. </tr>
  271. <tr>
  272. <td class="center">
  273. <div id="output" class="output">
  274. <?
  275. if ($_GET['err'] == 'notlogged') {
  276. echo 'Not logged in because the login settings are incorrect...';
  277. }
  278. elseif (!$_SESSION['connected']) {
  279. echo 'Please login first...';
  280. }
  281. ?>
  282. </div>
  283. </td>
  284. </tr>
  285. </table>
  286. </body>
  287. </html>
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (5)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.047s