Scripts
Maak beheer pagina
Ik werd er moe van om steeds een MySQL beheer pagina te moeten schrijven (een soort PhpMyAdmin, maar dan op je site zelf, dat vindt ik persoonlijk fijner werken), dus het toevoegen, wijzigen, bekijken en verwijderen van MySQL records. Daarom schreef ik een script dat die pagina voor mij schrijft. Dit is de 2e versie, de geperfectioneerde zeg maar. Al heb je het script zelf niet nodig, kun je het altijd nog gebruiken om die paginas voor jou te maken.
maak-beheer-pagina
///////////////////////////////////////////////////////////////
/////////////////////// index.php ///////////////////////////
///////////////////////////////////////////////////////////////
<?
if($_POST['maak']){
echo "<script language='JavaScript'> document.location = 'index2.php?f=".$HTTP_POST_VARS['velden']."'; </script>";
} else {
echo "<form method='post' target='_self'>";
echo "Aantal velden <input type='text' name='velden'><br>";
echo "<input type='submit' name='maak' value='Naar stap 2'>";
echo "</form>";
}
?>
///////////////////////////////////////////////////////////////
/////////////////////// index2.php //////////////////////////
///////////////////////////////////////////////////////////////
<?
if($_POST['maak']){
?>
<textarea cols='100' rows='30'>
<html>
<head>
<style type='text/css'>
body {
background-image: url('doc/background.gif');
background-color: #FFFFFF;
color: #000000;
}
A {
color: #000000;
text-decoration:none;
}
A:hover {
color: #000000;
text-decoration:none;
}
A:active{
color: #000000;
text-decoration:none;
}
A:visited{
color: #000000;
text-decoration:none;
}
table { font-size: 10px; font-family: Verdana, Arial; }
td.main {
background-color: #FFFFFF;
color: #000000;
padding-left: 0;
padding-right: 0;
padding-top: 0;
padding-bottom: 0;
border-style: solid;
border-color: #000000;
border-top-width: 1;
border-left-width: 1;
border-right-width: 1;
border-bottom-width: 1;
}
td.none {
background-color: #FFFFFF;
color: #000000;
padding-left: 0;
padding-right: 0;
padding-top: 0;
padding-bottom: 0;
border-style: solid;
border-color: #000000;
border-top-width: 0;
border-left-width: 0;
border-right-width: 0;
border-bottom-width: 0;
}
td.main_r {
background-color: #FFFFFF;
color: #000000;
padding-left: 0;
padding-right: 0;
padding-top: 0;
padding-bottom: 0;
border-style: solid;
border-color: #000000;
border-top-width: 1;
border-left-width: 0;
border-right-width: 1;
border-bottom-width: 1;
}
hr {
background-color:#000000;
color: #000000;
border-style: solid;
border-width: 0px;
border-color:#000000;
background-image: none;
height: 1pt;
}
</style>
</head>
<body>
<table><tr><td>
<?
$host = <? echo $HTTP_POST_VARS['host']; ?>;
$user = <? echo $HTTP_POST_VARS['user']; ?>;
$ww = <? echo $HTTP_POST_VARS['pass']; ?>;
$db = <? echo $HTTP_POST_VARS['db']; ?>;
mysql_connect("$host","$user","$ww");
mysql_select_db("$db")or die (mysql_error());
function afkorten($string, $lenght, $end = '<font color=red>...</font>')
{
if (strlen($string) > $lenght)
{
$lenght -= strlen($end);
$last = strrpos(substr($string, 0, $lenght + 1), ' ');
return substr($string, 0, (!$last)? $lenght: $last) . $end;
}
return $string;
}
$functie = $_GET['f'];
if($functie == ""){ $functie = "b"; }
if($functie == "b"){
echo "<hr>";
echo "Bekijken";
echo "<hr>";
echo "<table width='100%' cellspacing='5'>";
echo "<tr>";
echo "<td class='none'><b>ID</b></td>";
<? $teller = 1; while($teller <= $_GET['f']){ ?>
echo "<td class='none'><b><? echo $HTTP_POST_VARS[$teller]; ?></b></td>";
<? if($HTTP_POST_VARS['sorteer'] == 0){ $sorteer = "id"; } else if($teller == $HTTP_POST_VARS['sorteer']){ $sorteer = $HTTP_POST_VARS[$teller]; } ?>
<? $teller++; } ?>
echo "<td class='none'><b>Verwijderen</b></td>";
echo "<td class='none'><b>Wijzigen</b></td>";
<? if($HTTP_POST_VARS['desk'] == 1){ ?>
$select = mysql_query("SELECT * FROM <? echo $HTTP_POST_VARS['tabel']; ?> ORDER BY <? echo $sorteer; ?> DESC") or die(mysql_error());
<? } else { ?>
$select = mysql_query("SELECT * FROM <? echo $HTTP_POST_VARS['tabel']; ?> ORDER BY <? echo $sorteer; ?>") or die(mysql_error());
<? } ?>
while($row = mysql_fetch_array($select)){
echo "<tr>";
echo "<td class='none'>".$row['id']."</td>";
<? $teller = 1; while($teller <= $_GET['f']){ ?>
echo "<td class='none'>".afkorten($row['<? echo $HTTP_POST_VARS[$teller]; ?>'],'<? echo $HTTP_POST_VARS['afkorten']; ?>')."</td>";
<? $teller++; } ?>
echo "<td class='none' align='right'><a href='<? echo $HTTP_POST_VARS['p_url']; ?>?f=v&fid=".$row['id']."'><font color='red'><b>V</b></font></a></td>";
echo "<td class='none' align='right'><a href='<? echo $HTTP_POST_VARS['p_url']; ?>?f=w&fid=".$row['id']."'><font color='darkorange'><b>W</b></font></a></td>";
echo "</tr>";
}
echo "</table>";
echo "<hr>";
echo "<a href='<? echo $HTTP_POST_VARS['p_url']; ?>?f=t'>Voeg een record toe</a>";
echo "<hr>";
} else if($functie == "v"){
$verwijder_id = $_GET['fid'];
$delete = mysql_query("DELETE FROM <? echo $HTTP_POST_VARS['tabel']; ?> WHERE id = '$verwijder_id'") or die(mysql_error());
echo "Record #".$verwijder_id." is verwijderd.<br>";
<? if($HTTP_POST_VARS['terug'] == 1){ ?>
echo "<script language='JavaScript'> document.location = '<? echo $HTTP_POST_VARS['p_url']; ?>'; </script>";
<? } else { ?>
echo "<a href='<? echo $HTTP_POST_VARS['p_url']; ?>'>Terug</a>";
<? } ?>
} else if($functie == "t"){
if($_POST['toevoegen']){
<? $teller = 1; while($teller <= $_GET['f']){ ?>
$<? echo $HTTP_POST_VARS[$teller]; ?> = $HTTP_POST_VARS['<? echo $HTTP_POST_VARS[$teller]; ?>'];
<? $teller++; } ?>
$toevoegen = "INSERT INTO <? echo $HTTP_POST_VARS['tabel']; ?> (
<? $teller = 1; while($teller <= $_GET['f']){
if($teller != 1){
echo ",";
}
echo $HTTP_POST_VARS[$teller];
$teller++; } ?>
) VALUES (
<? $teller = 1; while($teller <= $_GET['f']){
if($teller != 1){
echo ",";
}
echo "'$".$HTTP_POST_VARS[$teller]."'";
$teller++; } ?>
)";
mysql_query($toevoegen) or die(mysql_error());
echo "Record is toegevoegd.<br>";
<? if($HTTP_POST_VARS['terug'] == 1){ ?>
echo "<script language='JavaScript'> document.location = '<? echo $HTTP_POST_VARS['p_url']; ?>'; </script>";
<? } else { ?>
echo "<a href='<? echo $HTTP_POST_VARS['p_url']; ?>'>Terug</a>";
<? } ?>
} else {
echo "<hr>";
echo "<b>Voeg een record toe</b>:";
echo "<hr>";
echo "<form method='post' target='_self'>";
echo "<table>";
<? $teller = 1; while($teller <= $_GET['f']){ ?>
echo "<tr>";
echo "<td class='none'><b><? echo $HTTP_POST_VARS[$teller]; ?></b></td>";
echo "<td class='none'>";
<? $type_teller = "type_".$teller;
if($HTTP_POST_VARS[$type_teller] == "textarea"){ ?>
echo "<textarea cols='30' rows='7' name='<? echo $HTTP_POST_VARS[$teller]; ?>'></textarea>";
<? } else { ?>
echo "<input type='text' name='<? echo $HTTP_POST_VARS[$teller]; ?>'>";
<? } ?>
echo "</td>";
echo "</tr>";
<? $teller++; } ?>
echo "<tr>";
echo "<td class='none'><input type='submit' name='toevoegen' value='Voeg record toe'><br>";
echo "<a href='<? echo $HTTP_POST_VARS['p_url']; ?>'>Terug</a></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
} else if($functie == "w"){
$wijzig_id = $_GET['fid'];
if($_POST['wijzigen']){
<? $teller = 1; while($teller <= $_GET['f']){ ?>
$<? echo $HTTP_POST_VARS[$teller]; ?> = $HTTP_POST_VARS['<? echo $HTTP_POST_VARS[$teller]; ?>'];
<? $teller++; } ?>
$wijzigen = mysql_query("UPDATE <? echo $HTTP_POST_VARS['tabel']; ?> SET
<? $teller = 1; while($teller <= $_GET['f']){
if($teller != 1){
echo " , ";
}
echo $HTTP_POST_VARS[$teller]; ?> = '$<? echo $HTTP_POST_VARS[$teller]; ?>'<?
$teller++; } ?>
WHERE id = '$wijzig_id' LIMIT 1") or die(mysql_error());
echo "Record is gewijzigd.<br>";
<? if($HTTP_POST_VARS['terug'] == 1){ ?>
echo "<script language='JavaScript'> document.location = '<? echo $HTTP_POST_VARS['p_url']; ?>'; </script>";
<? } else { ?>
echo "<a href='<? echo $HTTP_POST_VARS['p_url']; ?>'>Terug</a>";
<? } ?>
} else {
echo "<hr>";
echo "<b>Wijzig een record</b>:";
echo "<hr>";
$record_select = mysql_query("SELECT * FROM <? echo $HTTP_POST_VARS['tabel'] ?> WHERE id = '$wijzig_id' LIMIT 1");
while($row = mysql_fetch_array($record_select)){
echo "<form method='post' target='_self'>";
echo "<table>";
<? $teller = 1; while($teller <= $_GET['f']){ ?>
echo "<tr>";
echo "<td class='none'><b><? echo $HTTP_POST_VARS[$teller]; ?></b></td>";
echo "<td class='none'>";
<? $type_teller = "type_".$teller;
if($HTTP_POST_VARS[$type_teller] == "textarea"){ ?>
echo "<textarea name='<? echo $HTTP_POST_VARS[$teller]; ?>' cols='30' rows='7'>".$row['<? echo $HTTP_POST_VARS[$teller]; ?>']."</textarea>";
<? } else { ?>
echo "<input type='text' name='<? echo $HTTP_POST_VARS[$teller]; ?>' value='".$row['<? echo $HTTP_POST_VARS[$teller]; ?>']."'>";
<? } ?>
echo "</td>";
echo "</tr>";
<? $teller++; } ?>
echo "<tr>";
echo "<td class='none'><input type='submit' name='wijzigen' value='Wijzig dit record'></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
}
}
?>
</td></tr></table>
</body>
</html>
</textarea>
<?
} else {
$fields = $_GET['f'];
$teller = 1;
echo "<form method='post' target='_self'>";
echo "Tabel <input type='text' name='tabel'><br>";
echo " <br>";
echo "Host <input type='text' name='host' value='localhost'><br>";
echo "DB - naam <input type='text' name='db'><br>";
echo "Gebruiker <input type='text' name='user'><br>";
echo "Wachtwoord <input type='password' name='pass'><br>";
echo " <br>";
echo "Url pagina <input type='text' name='p_url' value='index.php'><br>";
echo " <br>";
echo "Maximale lengte te weergeven text <input type='text' name='afkorten' value='125'><br>";
echo " <br>";
echo "Na toevoegen direct terug (anders eerst melding) <input type='checkbox' name='terug' value='1'><br>";
echo " <br>";
echo "Sorteren omdraaien <input type='checkbox' value='1' name='desk'><br>";
echo " <br>";
echo "<table>";
echo "<tr>";
echo "<td><b>#</b></td>";
echo "<td><b>Naam</b></td>";
echo "<td><b>Sorteer</b></td>";
echo "<td><b>Type</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td>-</td>";
echo "<td>ID</td>";
echo "<td><input type='radio' name='sorteer' value='0' CHECKED></td>";
echo "<td>-</td>";
echo "</tr>";
while($teller <= $fields){
echo "<tr>";
echo "<td>".$teller."</td>";
echo "<td><input type='text' name='".$teller."'></td>";
echo "<td><input type='radio' name='sorteer' value='".$teller."'></td>";
echo "<td>";
echo "<input type='radio' name='type_".$teller."' value='input' CHECKED>Input ";
echo "<input type='radio' name='type_".$teller."' value='textarea'>Textarea";
echo "</td>";
echo "</tr>";
$teller ++;
}
echo "</table>";
echo " <br>";
echo "<input type='submit' name='maak' value='Maak pagina'>";
echo "</form>";
}
?>
///////////////////////////////////////////////////////////////
/////// voorbeeld van een gegenereerde code //////////
///////////////////////////////////////////////////////////////
<html>
<head>
<style type='text/css'>
body {
background-image: url('doc/background.gif');
background-color: #FFFFFF;
color: #000000;
}
A {
color: #000000;
text-decoration:none;
}
A:hover {
color: #000000;
text-decoration:none;
}
A:active{
color: #000000;
text-decoration:none;
}
A:visited{
color: #000000;
text-decoration:none;
}
table { font-size: 10px; font-family: Verdana, Arial; }
td.main {
background-color: #FFFFFF;
color: #000000;
padding-left: 0;
padding-right: 0;
padding-top: 0;
padding-bottom: 0;
border-style: solid;
border-color: #000000;
border-top-width: 1;
border-left-width: 1;
border-right-width: 1;
border-bottom-width: 1;
}
td.none {
background-color: #FFFFFF;
color: #000000;
padding-left: 0;
padding-right: 0;
padding-top: 0;
padding-bottom: 0;
border-style: solid;
border-color: #000000;
border-top-width: 0;
border-left-width: 0;
border-right-width: 0;
border-bottom-width: 0;
}
td.main_r {
background-color: #FFFFFF;
color: #000000;
padding-left: 0;
padding-right: 0;
padding-top: 0;
padding-bottom: 0;
border-style: solid;
border-color: #000000;
border-top-width: 1;
border-left-width: 0;
border-right-width: 1;
border-bottom-width: 1;
}
hr {
background-color:#000000;
color: #000000;
border-style: solid;
border-width: 0px;
border-color:#000000;
background-image: none;
height: 1pt;
}
</style>
</head>
<body>
<table><tr><td>
<?
$host = localhost;
$user = netersel_koen;
$ww = koen;
$db = netersel_koen;
mysql_connect("$host","$user","$ww");
mysql_select_db("$db")or die (mysql_error());
function afkorten($string, $lenght, $end = '<font color=red>...</font>')
{
if (strlen($string) > $lenght)
{
$lenght -= strlen($end);
$last = strrpos(substr($string, 0, $lenght + 1), ' ');
return substr($string, 0, (!$last)? $lenght: $last) . $end;
}
return $string;
}
$functie = $_GET['f'];
if($functie == ""){ $functie = "b"; }
if($functie == "b"){
echo "<hr>";
echo "Bekijken";
echo "<hr>";
echo "<table width='100%' cellspacing='5'>";
echo "<tr>";
echo "<td class='none'><b>ID</b></td>";
echo "<td class='none'><b>type</b></td>";
echo "<td class='none'><b>naam</b></td>";
echo "<td class='none'><b>url</b></td>";
echo "<td class='none'><b>info</b></td>";
echo "<td class='none'><b>Verwijderen</b></td>";
echo "<td class='none'><b>Wijzigen</b></td>";
$select = mysql_query("SELECT * FROM domeininfo ORDER BY id DESC") or die(mysql_error());
while($row = mysql_fetch_array($select)){
echo "<tr>";
echo "<td class='none'>".$row['id']."</td>";
echo "<td class='none'>".afkorten($row['type'],'125')."</td>";
echo "<td class='none'>".afkorten($row['naam'],'125')."</td>";
echo "<td class='none'>".afkorten($row['url'],'125')."</td>";
echo "<td class='none'>".afkorten($row['info'],'125')."</td>";
echo "<td class='none' align='right'><a href='test.php?f=v&fid=".$row['id']."'><font color='red'><b>V</b></font></a></td>";
echo "<td class='none' align='right'><a href='test.php?f=w&fid=".$row['id']."'><font color='darkorange'><b>W</b></font></a></td>";
echo "</tr>";
}
echo "</table>";
echo "<hr>";
echo "<a href='test.php?f=t'>Voeg een record toe</a>";
echo "<hr>";
} else if($functie == "v"){
$verwijder_id = $_GET['fid'];
$delete = mysql_query("DELETE FROM domeininfo WHERE id = '$verwijder_id'") or die(mysql_error());
echo "Record #".$verwijder_id." is verwijderd.<br>";
echo "<script language='JavaScript'> document.location = 'test.php'; </script>";
} else if($functie == "t"){
if($_POST['toevoegen']){
$type = $HTTP_POST_VARS['type'];
$naam = $HTTP_POST_VARS['naam'];
$url = $HTTP_POST_VARS['url'];
$info = $HTTP_POST_VARS['info'];
$toevoegen = "INSERT INTO domeininfo (
type,naam,url,info
) VALUES (
'$type','$naam','$url','$info'
)";
mysql_query($toevoegen) or die(mysql_error());
echo "Record is toegevoegd.<br>";
echo "<script language='JavaScript'> document.location = 'test.php'; </script>";
} else {
echo "<hr>";
echo "<b>Voeg een record toe</b>:";
echo "<hr>";
echo "<form method='post' target='_self'>";
echo "<table>";
echo "<tr>";
echo "<td class='none'><b>type</b></td>";
echo "<td class='none'>";
echo "<input type='text' name='type'>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='none'><b>naam</b></td>";
echo "<td class='none'>";
echo "<input type='text' name='naam'>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='none'><b>url</b></td>";
echo "<td class='none'>";
echo "<input type='text' name='url'>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='none'><b>info</b></td>";
echo "<td class='none'>";
echo "<textarea cols='30' rows='7' name='info'></textarea>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='none'><input type='submit' name='toevoegen' value='Voeg record toe'><br>";
echo "<a href='test.php'>Terug</a></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
} else if($functie == "w"){
$wijzig_id = $_GET['fid'];
if($_POST['wijzigen']){
$type = $HTTP_POST_VARS['type'];
$naam = $HTTP_POST_VARS['naam'];
$url = $HTTP_POST_VARS['url'];
$info = $HTTP_POST_VARS['info'];
$wijzigen = mysql_query("UPDATE domeininfo SET
type = '$type' , naam = '$naam' , url = '$url' , info = '$info'
WHERE id = '$wijzig_id' LIMIT 1") or die(mysql_error());
echo "Record is gewijzigd.<br>";
echo "<script language='JavaScript'> document.location = 'test.php'; </script>";
} else {
echo "<hr>";
echo "<b>Wijzig een record</b>:";
echo "<hr>";
$record_select = mysql_query("SELECT * FROM domeininfo WHERE id = '$wijzig_id' LIMIT 1");
while($row = mysql_fetch_array($record_select)){
echo "<form method='post' target='_self'>";
echo "<table>";
echo "<tr>";
echo "<td class='none'><b>type</b></td>";
echo "<td class='none'>";
echo "<input type='text' name='type' value='".$row['type']."'>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='none'><b>naam</b></td>";
echo "<td class='none'>";
echo "<input type='text' name='naam' value='".$row['naam']."'>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='none'><b>url</b></td>";
echo "<td class='none'>";
echo "<input type='text' name='url' value='".$row['url']."'>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='none'><b>info</b></td>";
echo "<td class='none'>";
echo "<textarea name='info' cols='30' rows='7'>".$row['info']."</textarea>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='none'><input type='submit' name='wijzigen' value='Wijzig dit record'></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
}
}
?>
</td></tr></table>
</body>
</html>
Reacties
0