Ajax Inline Editor with PHP and MySQL
Friday, March 30th, 2007I found lots of scripts that do inline editing but I couldnt find one that updated the database. This little beauty will, thanks to the Javascript from the link below.
My code is fairly sloppy but you get the idea from it.
Ok so using the js from the page below:
http://www.yvoschaap.com/index.php/weblo
assuming you have a table called tblInLineEdit with fields id, name,descr
**ajaxtest.php**
<?
$db_user = "";
$db_pass = ""; // password here
$db = "";
$host = "";
$link = mysql_connect($host,$db_user,$db_pass) or die("Database connection broken");
mysql_select_db($db,$link) or die("Database connection unavailable - ".mysql_error() );
$result = mysql_query("SELECT * FROM tblInLineEdit");
//$row = mysql_fetch_assoc($result);
?>
<script type="text/javascript" src="instantedit.js"></script>
<table>
<? while ($row = mysql_fetch_assoc($result)) { ?>
<tr>
<td><span
id="name-|||-<?php echo $row[’id’]; ?>" class="editText"><?
echo $row[’name’]; ?></span></td>
<td><span
id="descr-|||-<?php echo $row[’id’]; ?>"
class="editText"><? echo $row[’descr’];
?></span></td>
</tr>
<?php } ?>
</table>
**update.php**
<?
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
$db_user = "";
$db_pass = "";
$db = "";
$host = "";
$link = mysql_connect($host,$db_user,$db_pass) or die("Database connection broken");
mysql_select_db($db,$link) or die("Database connection unavailable - ".mysql_error() );
$content = $_GET[’content’];
list($fieldname, $id) = explode("-|||-",$_GET[’fieldname’]);
mysql_query("UPDATE tblInLineEdit SET $fieldname = ‘$content’ WHERE id = $id ") or die("blah failure - ".mysql_error() );
$result = mysql_query("SELECT * FROM tblInLineEdit WHERE id = $id");
$row = mysql_fetch_assoc($result);
echo $row["{$fieldname}"];
?>
