Doggonit
08-26-2005, 06:09 PM
How can I make the data that I get from the database in the browser editable? So make it that the data is just filled into forms and you can just change the content and press a button that updates the data on the MySQL server and shows the new values? Or maybe a regular output field but with a button next to each row that brings up a popup (for example) and enables you to edit, and update the database. Then, after changing the data, I would also like the page to refresh itself.
A simple example will more than suffice, so don't spend your time making it all fancy and whatnot.
Thanks a lot in advance,
Doggonit
musher
08-26-2005, 08:30 PM
Doggonit here's a simple Maint (Add/Modify/Delete/Display) app I wrote
Table: employee fields:First, Last, Address, Position
<?php
require_once('DataBaseCon.php');
mysql_select_db($database_Con,$db);
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
$_var_message = "Select a Record or Add a New Record";
// process form - SQL to Insert a new record
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form3")) {
$db = mysql_connect("localhost", "root", "mycroft");
mysql_select_db("test",$db);
$sql = sprintf("INSERT INTO employees (first,last,address,position) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['first'], "text"),
GetSQLValueString($_POST['last'], "text"),
GetSQLValueString($_POST['address'], "text"),
GetSQLValueString($_POST['position'], "text"));
$result = mysql_query($sql);
$_var_message = "Thank you! Information has been Added.";
}
// process form - SQL to Update a record
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
$sql = sprintf("UPDATE employees SET first=%s, last=%s, address=%s, position=%s WHERE id=%s",
GetSQLValueString($_POST['first'], "text"),
GetSQLValueString($_POST['last'], "text"),
GetSQLValueString($_POST['address'], "text"),
GetSQLValueString($_POST['position'], "text"),
GetSQLValueString($_POST['id'], "int"));
$result = mysql_query($sql);
$_var_message = "Thank you! Information has been Updated.";
}
// process form - SQL to Delete a record
if ((isset($_REQUEST["delete"])) && ($_REQUEST["delete"] == "yes")) {
// delete a record
$id=$_GET['recordID'];
$sql = "DELETE FROM employees WHERE id=$id";
$result = mysql_query($sql);
$_var_message = "Thank you! Information has been Deleted.";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="mssmarttagspreventparsing" content="true" />
<meta name="Description" content=" " />
<meta name="Keywords" content=" " />
<meta name="distribution" content="global" />
<meta name="resource-type" content="document" />
<meta name="robots" content="all" />
<meta http-equiv="pragma" content="no-cache" />
<meta name="Author" content="Jim Miller" />
<meta name="Copyright" content="2000, Ridley Inc." />
<meta name="Revised" content="Jim Miller, 01/07/2005" />
<meta name="Expires" content="never" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>// SITE NAME // HOME PAGE. //</title>
<link rel="stylesheet" type="text/css" href="stylesheets/default.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
<style type="text/css">
<!--
body {
margin: 20px;
min-width:590px;
text-align: center;
}
#main {
width:590px;
background:#fff;
text-align: left;
border: 1px dashed #778899;
}
.style1 {color: #993300}
-->
</style>
</head>
<body>
<div id="main">
<div align="center"><h1>Record Maint Section</h1></div>
<table width="100%" border="1" align="center">
<?php
// Edit Record
if ((isset($_REQUEST["edit"])) && ($_REQUEST["edit"] == "yes")) {
// query the DB
$id=$_GET['recordID'];
$sql = "SELECT * FROM employees WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$_var_message = "Edit Record";
echo '<tr><td colspan="2" class="style1">'.$_var_message.'</td></tr>';
echo '<form method="post" name="form2" action="'.$_SERVER['PHP_SELF'].'">';
echo '<tr><td>First name: </td><td><input type="Text" name="first" value="'.$myrow['first'].'"></td></tr>';
echo '<tr><td>Last name: </td><td><input type="Text" name="last" value="'.$myrow['last'].'"></td></tr>';
echo '<tr><td>Address: </td><td><input type="Text" name="address" value="'.$myrow['address'].'"></td></tr>';
echo '<tr><td>Position: </td><td><input type="Text" name="position" value="'.$myrow['position'].'"></td></tr>';
echo '<tr><td colspan="2"> </td></tr>';
echo '<tr><td><input type="submit" name="submit" value="Submit"></td>';
echo '<td><input type="button" value="Return to Select" onclick="location.href=\'' .$_SERVER['PHP_SELF']. '\'" /></td></tr>';
echo '<input type=hidden name="id" value="'.$myrow['id'].'">';
echo '<input type="hidden" name="MM_update" value="form2">';
echo '</form>';
// Add Record
} elseif ((isset($_REQUEST["add"])) && ($_REQUEST["add"] == "yes")) {
$_var_message = "Add a New Record";
echo '<tr><td colspan="2" class="style1">'.$_var_message.'</td></tr>';
echo '<form method="post" name="form3" action="'.$_SERVER['PHP_SELF'].'">';
echo '<tr><td>First name: </td><td><input type="Text" name="first"></td></tr>';
echo '<tr><td>Last name: </td><td><input type="Text" name="last"></td></tr>';
echo '<tr><td>Address: </td><td><input type="Text" name="address"></td></tr>';
echo '<tr><td>Position: </td><td><input type="Text" name="position"></td></tr>';
echo '<tr><td colspan="2"> </td></tr>';
echo '<tr><td><input type="submit" name="submit" value="Submit"></td>';
echo '<td><input type="button" value="Return to Select" onclick="location.href=\'' .$_SERVER['PHP_SELF']. '\'" /></td></tr>';
echo '<input type="hidden" name="MM_insert" value="form3">';
echo '</form>';
// Display Record
} elseif ((isset($_REQUEST["display"])) && ($_REQUEST["display"] == "yes")) {
$_var_message = "Record Display";
$id = $_GET['recordID'];
$result = mysql_query("SELECT * FROM employees WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
echo '<tr><td colspan="2" class="style1">'.$_var_message.'</td></tr>';
echo '<form>';
echo '<tr><td>First Name: </td><td>'.$myrow['first'].'</td></tr>';
echo '<tr><td>Last Name: </td><td>'.$myrow['last'].'</td></tr>';
echo '<tr><td>Address: </td><td>'.$myrow['address'].'</td></tr>';
echo '<tr><td>Position: </td><td>'.$myrow['position'].'</td></tr>';
echo '<tr><td colspan="2"> </td></tr>';
echo '<tr>';
echo '<td><input type="button" value="Edit Record" onclick="location.href=\''.$_SERVER['PHP_SELF'].'?recordID='.$myrow['id'].'&edit=yes\'" /></td>';
echo '<td><input type="button" value="Return to Select" onclick="location.href=\'' .$_SERVER['PHP_SELF']. '\'" /></td>';
echo '</tr>';
echo '</form>';
// Show Record List (Selecttion Page)
} else {
$result = mysql_query("SELECT * FROM employees",$db);
if ($myrow = mysql_fetch_array($result)) {
// display list if there are records to display
echo '<tr><td colspan="6" class="style1">'.$_var_message.'</td></tr>';
echo '<tr>';
echo '<td colspan="3">';
echo '<div align="center"><input type="button" value="ADD New Record" onclick="location.href=\'' . $_SERVER['PHP_SELF'] . '?add=yes\'" /></div>';
echo '</td>';
echo '<td>First Name</td>';
echo '<td>Last Name</td>';
echo '<td>Record ID</td>';
echo '</tr>';
do {
echo '<tr>';
echo '<form name="form9" action="" method="post">';
echo '<td><input type="button" value="Edit" onclick="location.href=\''.$_SERVER['PHP_SELF'].'?recordID='.$myrow['id'].'&edit=yes\'" /></td>';
echo '<td><input type="submit" onclick="this.form.action=\''.$_SERVER['PHP_SELF'].'?recordID='.$myrow['id'].'&delete=yes\';return confirm(\'Are you sure you want to delete this record\')" value="Delete" name="del" /></td>';
echo '<td><input type="button" value="Display" onclick="location.href=\''.$_SERVER['PHP_SELF'].'?recordID='.$myrow['id'].'&display=yes\'" /></td>';
echo '<td>'.$myrow['first'].'</td>';
echo '<td>'.$myrow['last'].'</td>';
echo '<td>'.$myrow['id'].'</td>';
echo '</tr>';
} while ($myrow = mysql_fetch_array($result));
echo '</form>';
} else {
// no records to display
echo "Sorry, no records were found!";
}
}
?>
</table>
</div>
</body>
</html>
<?php
mysql_close($db);
?>
<?php
# FileName="DataBaseCon.php"
# Type="MYSQL"
$hostname_Con = "localhost";
$database_Con = "MYSQL DB NAME HERE";
$username_Con = "USERID HERE";
$password_Con = "PASSWORD HERE";
$db = mysql_connect($hostname_Con, $username_Con, $password_Con) or die(mysql_error());
?>
Doggonit
08-26-2005, 11:15 PM
Thanks. I will get back to you as soon as I can with my version just for reference here.
vBulletin® v3.8.2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.