I am trying to get inline editing of mysql data working on my php page.
I am making use of an example script found:here
All my code is shown below this question.
On browsing to ajaxtest.php, the table data is displayed correctly. When clicking on the table data, I am allowed to edit it. but when clicking to save the data changes to ‘false’
Any ideas why this is hapening? Does it have to do with the headers on update.php?
Here is my code for the mysql table:
I have created a table as per below:
CREATE TABLE IF NOT EXISTS `tblInLineEdit` (
`id` int(6) NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`descr` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
--
-- Dumping data for table `tblInLineEdit`
--
INSERT INTO `tblInLineEdit` (`id`, `name`, `descr`) VALUES
(1, 'carl', 'developer'),
(2, 'angela', 'administration');
I have then created the two pages, ajaxtest.php and update.php. Code is listed below:
ajaxtest.php
<?
$db_user = "username";
$db_pass = "password"; // password here
$db = "database";
$host = "localhost";
$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="jquery.js"></script>
<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 2014 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 = "user";
$db_pass = "password"; // password here
$db = "database";
$host = "localhost";
$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}"];
?>
Any ideas as to why it returns false?
Thanks for the assistance in advance.
Alternatively can anyone recommend a lightweight inline editor for mysql that is easy to impliment?
Thanks as always.
Try debugging your client-side script. Use Firebug or Chrome’s developer tools to see the bugs produced. Your link produces the following error on page load in my browser:
It seems that the page does not exist.
I’m not sure what you are trying to accomplish. But if this is an early stage of an actual project, I recommend considering a little more research. Perhaps some study on Service Oriented Architecture can help you a lot. One of the best practices you can come up with would be using Zend Framework’s JSON server. I, myself, have worked on a framework which can be useful to you if your project’s scale grows bigger. It’s called Pomegranate, maybe you want to take a look at it.