MySQL Table Editor
A PHP 4 & 5 library to allow easy and user-friendly editing of MySQL tables. Note this is a library and not an application. It will be of no use unless you do some coding (not exactly a great deal though - see below). Note: This is not meant to be an alternative or replacement to phpMyAdmin. This is something you can provide to users to manage tables without them happily destroying your database. You can get the code here. Features
DemoThere's a demo here, and the second code listing below is the script that drives it.Example UsageThe following example is used in my admin area to allow administration of comments:
<?php
This is the demo script:
<?php
Public Method ListThe following are the public methods of the object which can be used to manipulate the gui:
Dependencies
ChangeLog16th June 2005 (1.0.6)
==============
o Fixed bug when not displaying primary key field.
o Fixed javascript bug in FireFox when selecting records.
o Added CSV download option. Can download either the selected records, the current
page or the entire table. Works well with searching too (ie. you can download the
current page even if it's a search result). There's a callback available to
override which escapes a single field of CSV data. The built in escape function
replaces carriage returns and line feeds with \r and \n respectively. It also
escapes commas with a backslash.
o Added advanced search capabilities
26th April 2005 (1.0.5)
===============
o Added support for join tables. Using these you can join the main table onto other
tables to retrieve columns from the "other" tables. When using join tables add
copy and delete functionality is automatically disabled, however edits can still
take place.
o Added two new methods, onlyDisplay() and onlyEdit(). As the names suggest these
methods set all fields to be hidden except those specified. Useful if you join
onto tables and end up having to hide lots of columns.
o Backported to PHP4. Be careful with references when using data validation callbacks.
19th April 2005 (1.0.4)
===============
o Added support for a "bitmask" type. Descriptive values need to be set using the
setValuesFrom*() methods. On the add/edit pages, this type is shown as a multiple
select. The corresponding type in MySQL is a SET, however you could just as easily
use an int.
o Added a "Set blank password" checkbox to the "password" pseudo input, to allow
setting of a blank password.
o Changed field specific errors on add/editcopy page to be next to their respective
fields. There's now a new method called setContextualError() to allow setting of
these errors from data validation callbacks.
17th April 2005 (1.0.3)
===============
o Fixed a bug which made multiple display filters on a column not work correctly.
o Search now highlights found terms. Does this by applying a display filter to each
searchable colum, which is applied *after* any user defined display filters.
15th April 2005 (1.0.2)
===============
o No longer classed as beta, (well, by me at least... ).
o When using the view button, fields with preset values are now shown
as they are on the main table page.
o Fixed a bug which meant edit was disabled when view was, and couldn't be
disabled by itself.
o Fixed redirection bug with urls.
o Fixed leading zeros being removed when inserting purely numeric values into
a text based field type.
o Added row copying functionality.
o Added data filters via addDataFilter() method to facilitate the
showing/viewing/editing/deleting of only certain rows
o Table structure is now no longer determined on each and every page refresh, but
is cached in the session.
o New method: setSearchableFields(). To be used instead of setConfig('searchableFields').
Supply one or more field names which are allowed to be searched.
o Added support for required fields when adding/editing/copying. If a required field is
not filled in, an error is displayed.
o Added support for pseudo input types:
o password This shows two password inputs on the add/edit page. If when
submitted the two do not match, an error is displayed. However,
if the two are left blank/empty, the field is not updated. This
is to allow updates to rows without changing the password field.
You can alter this behaviour by stipulating the field to be
required.
o email This shows a regular text input, however it automatically checks
whatever contents are supplied and ensures it matches the form of
an email address. If however the field is empty, no error is
raised. You can alter this behaviour by stipulating the field to
be required.
o Added support for data validation callbacks. These are run after functions, defaults,
required field checks and pseudo field validation checks. Using these you can add your
own validation checks before data is added/updated in the database. These callbacks
may also modify the data (eg. md5()ing a password). Now these are added, functions
default to off since along with default values functions are pretty much redundant.
o Added handling for magic_quotes_gpc
14th April 2005 (1.0.1-beta)
===============
o Changed aesthetics for shameful self promotion
o Added GPL licensing
o noDisplay() method now only hides a field from display, not editing. Use
noEdit() in combination to hide a field from editing too.
o Renamed defaultOrderby() to setDefaultOrderby()
o Fixed a number of E_NOTICE errors
o Added setDefaultValues(), which sets default values when adding rows. Can
be used with fields that aren't editable.
o Added add/edit/delete callback function support via addAdditionCallback(),
addEditCallback() and addDeleteCallback() methods. The callbacks are run when
each appropriate action is taken.
o Added ability to search fields which have their values preset with the
setValuesFrom* methods.
o Changed to using a single checkbox per row, and a single set of add/edit/delete
buttons. Can now delete multiple rows at once.
o Added View button, for viewing a row in full on it's own page.
o Added support for external header and footer files. Changing the header will lose
all CSS so you'll have to define your own.
o You can now disable any/all of the view/add/edit/delete buttons.
11th April 2005 (1.0.0-beta)
===============
o Initial release
TranslationsYou can get a German translation of the code here. I haven't tested this, so for all I know it may wipe your hard drive. Do a diff first to check. TODO ListMy current list of features to add:
Link to meIf you use any of the code on this site (and if you don't I guess) or it makes your life easier, I'd appreciate a link - http://www.phpguru.org. Thanks.
Author: Kolia
Posted: 14th April 2005 11:38 Found one trouble. I have to store in a DB phone codes which are integers starting with 0. When adding them to DB leading zero lost. In the DB they are stored as varchar.
Quote
Author: Kolia
Posted: 14th April 2005 11:42 fixed that by commenting "is_numeric" part in dbQuote.
Quote
Author: Richard Heyes
Posted: 14th April 2005 13:09 Kolia:
Quote> fixed that by commenting "is_numeric" part in > dbQuote. Great. Thanks!
Author: Richard@Home
Posted: 15th April 2005 17:13 Just a quick question to Kolia:
QuoteWhy are you storing telphone numebers as integers? Integers don't have leading zero's. Wouldn't it be better to store them as strings? That would allow you to have international telephone numbers too (+44)0114 xxxxxx (for example) Storing them as strings will also allow you to perform searches such as SELECT * FROM telephone WHERE number LIKE '(+44)%'
Author: Richard Heyes
Posted: 15th April 2005 17:54 Richard@Home:
Quote> Just a quick question to Kolia: > > Why are you storing telphone numebers as > integers? > > Integers don't have leading zero's. > > Wouldn't it be better to store them as > strings? > > That would allow you to have international > telephone numbers too (+44)0114 xxxxxx (for > example) > > Storing them as strings will also allow you to > perform searches such as > > SELECT * FROM telephone WHERE number LIKE > '(+44)%' > He probably wasn't. It was a valid bug in the code that occurred when the field type was textual.
Author: Kolia
Posted: 16th April 2005 19:36 Richard Heyes is right. i store them as varchar.
QuoteIt works without the is_numeric part in MySQL because MySQL understands numeric values in quotes. But I don't know will it work in other DBMS. And the question about other similar modules left unanswered...
Author: Richard Heyes
Posted: 16th April 2005 19:40 Kolia:
Quote> And the question about other similar modules > left unanswered... See the original article entitled "Table Editing". Or Google.
Author: ravi
Posted: 13th June 2005 11:43 This is a great module but do you think this will work for Postgresql as well?
QuoteSince you use pear that seems to be a good foundation. I have views on Postgres that I want to make editable and searchable. All the best.
Author: Richard Heyes
Posted: 13th June 2005 12:07 ravi:
Quote> This is a great module but do you think this > will work for Postgresql as well? > Since you use pear that seems to be a good > foundation. > > I have views on Postgres that I want to make > editable and searchable. Well, I guess it's possible. You'd have to separate out all the code which determines the table structure and field types, but after you've done that there shouldn't be all that much else to change.
Author: Tony
Posted: 14th June 2005 17:52 Hello.
QuoteI really admire the fact you submit classes to the php community that are so handy! The time a class like this saves when I'm working on the backend of PHP applications is amazing. I was looking around and actually found a very similar thing at: http://platon.sk/projects/main_page.php?project_id=5 . I haven't tried it yet, but it looks like more of a "program that does it all for you" as opposed to what your giving us. A class saves programmer's time, and at the same times requires programming knowledge and effort to get working. Just alot less time. :o) Ahh -- I hope you update this thing more! It's absolutely beautiful! Please don't abandon this thing! :o) - Tony P.S. Nifty bot-protection ^_^
Author: Richard Heyes
Posted: 14th June 2005 17:57 Tony:
Quote> Ahh -- I hope you update this thing more! It's > absolutely beautiful! Please don't abandon this > thing! :o) Glad you like it. I'm still developing it, CSV download is already added (selected rows, current page or entire table), and I'm half way through adding an advanced search facility.
Author: AlexIsWorkingForAChange
Posted: 29th June 2005 15:33 regarding using function setConfig($name, $value) to set a header, footer or both:
QuoteIf you've set up your include paths, and wish to include a header which is not in the same directory as the script which is including the tableeditor, its quite possible that file_exists($fileName) will return false even though the file is included quite happily. In this case, to avoid the spurious "Failed to find headerfile..." error, just comment out these 2 lines (253 - 254 on my copy): =========== $this->errors[] = "Failed to find $name: " . htmlspecialchars($value); return; =========== HTH someone..
Author: marcus
Posted: 8th July 2005 08:51 hi, i like that, but don't get it to run :-(
QuoteError: First argument is not a valid database connection! where is the $conn Var defined? i cant't find anything to write in my DB connection data... please help - i'm not so good in PHP greetings marcus
Author: Martin
Posted: 2nd August 2005 17:30 Marcus, connect like this:
Quoterequire_once('TableEditor.php'); $database = mysql_connect('localhost', 'user', 'passwd') or die('Could not connect: ' . mysql_error()); echo 'Connected successfully'; mysql_select_db('name-of-db') or die('Could not select database'); $editor = new TableEditor($database, 'table-name'); I just started using this great class and am considering porting it to use pear::db instead of raw mysql. Do you think it would fly or is it pear::db not specific or rubust enough to support TableEditor?
Author: Richard Heyes
Posted: 2nd August 2005 17:34 Martin:
Quote> I just started using this great class and am > considering porting it to use pear::db instead > of raw mysql. Do you think it would fly or is > it pear::db not specific or rubust enough to > support TableEditor? The problem you'll have is having to write code that gets the structure of the table that works cross database. I guess it's feasible.
Author: Martin
Posted: 4th August 2005 17:18 Ok,
QuoteI figured out how to do joins with addJoinTable() I am using a legacy database that seems to have the name of the key of every table named ID. To deal with this I would use: table1.id, table2.id... in my sql. Is there an easy way to get TableEditor to refer to fields by tablename.fieldname, or tell it to refer to specific fields with that nomenclature? Thanks for the help. Martin
Author: Martin
Posted: 4th August 2005 22:07 I tried a simple hack:
Quotechanging the $this>addField($row['Field'], ... to $this>addField($table.'.'.$row['Field'], ... in all the case statments of getStructure() This had some success, I am now able to join tables with duplicate field names, but now the table display seems to loose the first field of first joied field. For example if the first joined table was: table2.id, table2.name, table2.address; On the display page under table2.name would be the fields for table2.address
Author: Arsen
Posted: 26th August 2005 19:27 Hi,
QuoteI can't set default values for hidden fields. I get 'ERROR: Failed to find specified row in database' after pressing Apply. I hide the field in Display/Edit and set default value for it: noEdit('myFieldName'); setDefaultValues(array('myFieldName' => 'MyValue'); And DB still gets updated where 'myFieldName' value = NULL. Thanks
Author: Arsen
Posted: 26th August 2005 19:32 Forgot to mention that I also set
QuoteAddDataFilter('myFieldName=MyValue'); I get the error because of the data filter. Seems like setDefaultValues method does not work on Hidden fields.
Author: sam adi
Posted: 26th September 2005 01:05 how i can add two field and display ini one table,
Quoteand I Can't dropdowns choise to add data table. Please help me, I am newbee thank's before
Author: sam adi
Posted: 26th September 2005 01:18 sory my question wrong
Quotehow I Can sum data in two fields and display in another field and I Can't dropdowns choise to add data table. thank's Before
Author: Kibi
Posted: 14th October 2005 10:02 Thanks for TableEditor- it's very nice.
QuoteBut it's hard to understand from examples how to handle such SQL request: SELECT t1.field1, t2.field1, t2.filed2, t3.filed3 FROM table1 t1, table2 t2, table3 t3 WHERE t1.field1 = t2.field1 AND t2.field2 = t3.filed2 Is it possible ? Or should I use this library only for fetching data from one solid table? Thank you once more!
Author: james
Posted: 20th October 2005 14:51 Hi Richard, I've been trying to use the csv export. Everytime I go to export it defaults as a php file. Why doesnt it automatically save as a csv like the button would suggest?
QuoteIf you use your demo you'll see the same problem Thanks James
Author: Vince
Posted: 21st October 2005 00:22 I would also like to know how to make the CSV export 'save as' a file with .csv extension rather than php.
QuoteThe data is fine, and if you rename it while saving then ok, but it still give a couple of error messages to the users which then decide to click 'cancel' - I've watched them do it. It also means users cannot simply chose to open rather than save the file. Thanks for a great script.
Author: Vasily
Posted: 1st November 2005 15:59 Congratulation.... sincerely...
QuoteI'am at the begining of "journey" in php-scripting. You help me to change my vision about writing scripts. Whithout class-es is moore dificult to work, so ... Thank you once !
Author: oldfoot
Posted: 9th November 2005 15:01 sam adi:
Quote> sory my question wrong > > how I Can sum data in two fields and display in > another field > > and I Can't dropdowns choise to add data > table. > > thank's Before do it in your sql
Author: Tonu
Posted: 11th November 2005 18:26 hie,
Quotei like your code...would you plz complete one project for me which is alumni database...
Author: Rafia alotibi
Posted: 17th November 2005 14:09 Multiple Sort Fields now is Support
Quoteif Field Type is set inputType will Be checkbox also if Field Type is enum inputType Be will radio View Data list http://www.arabportal.net/TableEditor/Datalist.png View Add/Edit Record http://www.arabportal.net/TableEditor/EditAdd.png Example code http://www.arabportal.net/TableEditor/example.phps You can get the New code
Author: Rafia alotibi
Posted: 17th November 2005 14:11 Sorry
QuoteYou can get the New code http://www.arabportal.net/TableEditor/TableEditor_New.phps
Author: adi
Posted: 25th November 2005 12:59 how to resize coloum width in input field ?
Quotesorry if my english not good
Author: daniel
Posted: 28th December 2005 19:32 Thanks Richard for such a nice code. I started with TreeMenu, then Tree2 (the best of the like imo), Pager, and lately DynCalendar. I wish I had spotted TableEditor earlier, since I've brewed a similar one myself and I wish I could have used and eventually contributed to yours. I specially like your class interface, which is really concise.
QuoteCongrats and best luck !
Author: YOGESH G
Posted: 4th January 2006 08:17 Thanks a lot it's nice.. but you'd included some file in d code. how'll i get those files?
Quote
Author: Joseph
Posted: 11th January 2006 19:23 Everything works fine in IE, but in Firefox when you click the "Copy" button, the screen just refreshes. The problem is that - for some reason - the GET method is adding a "&" instead of the actual ampersand symbol into the URL. This happens in Firefox and Safari.
Quote
Author: Sam taneja
Posted: 2nd February 2006 03:10 Is it possible to declare some fields as richtext for some fields and use one of the html editors?
Quote
Author: Nick
Posted: 2nd February 2006 17:11 Ok you guys know what your talking about i wonder if you can solve this little issue i have.?
QuoteI use a varible $showhandsets in my coding which lists the handsets along in a line i.e TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT,TEXT, etc But i need to list the items in colums, with 20 handsets shown in a colum then it goes into another colum etc.. so instead of just having a long list horizontally, its something like: Start Continues Text Text Text Text Text Text Text Text Text Text Text Text Text Text Cuts off Im having problems.
Author: Mark Findlay
Posted: 15th February 2006 07:37 To set the filename to a CSV filename when the user clicks the CSV button (as opposed to the current .php filename it assigns), look for this code around line 1270 or so of of the TableEditor.php file:
Quote// Content type header('Content-Type: text/csv'); ...then add this directly beneath it: header('Content-Disposition: attachment; filename="' . $this->table . '.csv"'); That will set the download csv file to the name of the table, with the .csv extension.
Author: Mark Findlay
Posted: 23rd February 2006 02:37 Downloading the CSV file is broken in SSL mode.
QuoteTo fix: look for this code around line 1270 or so of of the TableEditor.php file: // Content type header('Content-Type: text/csv'); ...then add these 2 lines directly beneath it: header("Cache-Control: cache, must-revalidate"); header("Pragma: public"); Also, see my comment above to allow you to set a filename for the csv download.
Author: Michele
Posted: 11th March 2006 09:38 Hi. I have very little knowledge of php. I got it up and running. but now I have some errors. Do I have to "configure" or "format" the databese somehow first?
QuoteI was able to connect to the DB and open it, but now I am getting this: Warning: Invalid argument supplied for foreach() in /home/www/capurso.awardspace.com/table/TableEditor.php on line 636 Warning: Invalid argument supplied for foreach() in /home/www/capurso.awardspace.com/table/TableEditor.php on line 929 Warning: Invalid argument supplied for foreach() in /home/www/capurso.awardspace.com/table/TableEditor.php on line 823 ERROR: Unknown field: te_desc RROR: Error getting data. Database said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM TableEditorDemo WHERE 1 AND 1 AND (1) ORDER BY te_id LIMIT
Author: Richard Heyes
Posted: 11th March 2006 13:37 Michele:
Quote> Hi. I have very little knowledge of php. I got > it up and running. but now I have some errors. > Do I have to "configure" or "format" the > databese somehow first? > I was able to connect to the DB and open it, > but now I am getting this: Sounds like you've copied the example without either creating the table, or created the table but with missing fields.
Author: Richie
Posted: 14th March 2006 11:01 Your Class is exactly what i needed!!
QuotePerfect! I am modifying it to my needs just a little bit! p.e. i need to show to diffrent users diffrent content. So i seperate the content by group number. And the url gets &group=1 or 2 etc. Furthermore every group user has the possibility to add new content, i already managed to set the SETDEFAULTVALUE to the specific group. But everytime i create new content the group field is set to "0" and not to the DefaultValue i set! Do you have any idea what went wrong? thx Richie!
Author: Richard Heyes
Posted: 14th March 2006 22:10 Richie:
Quote> Your Class is exactly what i needed!! > Perfect! > I am modifying it to my needs just a little > bit! > p.e. i need to show to diffrent users diffrent > content. So i seperate the content by group > number. > And the url gets &group=1 or 2 etc. > Furthermore every group user has the > possibility to add new content, i already > managed to set the SETDEFAULTVALUE to the > specific group. But everytime i create new > content the group field is set to "0" and not > to the DefaultValue i set! > Do you have any idea what went wrong? Well, you'll need to set the default values with the setDefaultValues() method: $obj->setDefaultValues(array('fieldname' => 1)); And use the noDisplay() method to hide the field: $obj->noDisplay('fieldname'); HTH
Author: Andrew Sroka
Posted: 22nd March 2006 01:21 Warning: Invalid argument supplied for foreach() in /home/www/capurso.awardspace.com/table/TableEditor.php on line 636
QuoteWarning: Invalid argument supplied for foreach() in /home/www/capurso.awardspace.com/table/TableEditor.php on line 929 Warning: Invalid argument supplied for foreach() in /home/www/capurso.awardspace.com/table/TableEditor.php on line 823 ERROR: Unknown field: te_desc RROR: Error getting data. Database said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM TableEditorDemo WHERE 1 AND 1 AND (1) ORDER BY te_id LIMIT Someone has the same problem. I've downloaded main class, create required table in mySql, and.... error happen. I've look in to the source and it seems to build wrong query. I'm using PHP 5.05 and newest pear modules. Maybe the TableEditor.php publised on web contain some bug ?
Author: abc_tp
Posted: 23rd March 2006 08:54 It would be better if MySQL TableEditor Support Multilanguage .
Quote
Author: Emma
Posted: 30th March 2006 13:33 Sorry if my comment was a support type question, but after having done your fab class, I get the feeling that what I need to do isnt actually possible. So would be grateful for anyone's yeah or nay on this.
QuoteThanks Emma
Author: Stu
Posted: 31st March 2006 03:46 Great table editor!!! I'm having just one issue... I get this error when sorting rows or making an edit to a row or even canceling an edit...
QuoteCannot modify header information - headers already sent by .... I am running PHP 4.4.2 is this due to this new security feature of the header function.. "Note: Since PHP 4.4.2 and PHP 5.1.2 this function prevents more than one header to be sent at once as a protection against header injection attacks." Please help!!
Author: Stu
Posted: 31st March 2006 03:49 Emma -
QuoteYou can use the setValuesFromQuery(string field_name, string query) function to allow for drop down selections. Just store your selections in another table.
Author: Danny Engelman
Posted: 8th April 2006 16:31 After some testing I seem to be misunderstanding something.
QuoteCan TE add records into tables with an AutoIncrement ID field?
Author: hu
Posted: 11th April 2006 03:06 have a look at
Quotehttp://pear.php.net/package/Net_URL/download
Author: Doug Bergman
Posted: 22nd April 2006 06:08 This editor is awesome.
QuoteOne question, after selecting a record for Edit and makeing a change a blank page is displayed and the URL looks like this search=Romeo&pageID=2&edit=8395 how would I make it like this search=Romeo&pageID=2 This also occurs after a cancel from the edit page.
Author: Arik
Posted: 24th April 2006 09:52 Help to choose a videocamera. What standard to choose?
Quote
Author: Wendy Novianto
Posted: 25th April 2006 00:52 Hi,
QuoteI think the htmlspecialchars filter need to be applied directly on applyDisplayFilters function, instead of on other place, considering when I use callback function to generate certain html tag, it will escaped all the html generated tag from my callback function. [code] function applyDisplayFilters(&$row) { foreach ($row as $field => $value) { if (!empty($this->fields[$field]['filters'])) { $value = htmlspecialchars($value); foreach ($this->fields[$field]['filters'] as $f) { $value = call_user_func($f, $value); } $row[$field] = $value; } } } [/code] Notice that I add the htmlspecialchars on it, and I remove all occurance of htmlspecialchars for display, which I only found it on one place.
Author: Wendy Novianto
Posted: 25th April 2006 02:53 Found another small bugs.
QuoteCheck out around line line 900-950. Look for this code if (!empty($in)) { $in = implode(', ', $in); $searchClause[] = "$sf IN($in)"; } Replace the if statement into this if (!empty($in) && is_array($in)) { This will help the system not to break, which happen to me sometime, but I'm not sure whether that solve the real problem or not. Hope that helps.
Author: John
Posted: 28th April 2006 00:17 Great page, not too fancy but well balanced! Cheers!
Quote
Author: Serge
Posted: 1st May 2006 04:06 I have same error. No idea.
QuoteAndrew Sroka: > Warning: Invalid argument supplied for foreach() > in > /home/www/capurso.awardspace.com/table/TableEditor.php > on line 636 > > Warning: Invalid argument supplied for > foreach() in > /home/www/capurso.awardspace.com/table/TableEditor.php > on line 929 > > Warning: Invalid argument supplied for > foreach() in > /home/www/capurso.awardspace.com/table/TableEditor.php > on line 823 > > ERROR: Unknown field: te_desc > > RROR: Error getting data. Database said: You > have an error in your SQL syntax; check the > manual that corresponds to your MySQL server > version for the right syntax to use near 'FROM > TableEditorDemo WHERE 1 AND 1 AND (1) ORDER BY > te_id LIMIT > > > Someone has the same problem. I've downloaded > main class, create required table in mySql, > and.... error happen. I've look in to the > source and it seems to build wrong query. I'm > using PHP 5.05 and newest pear modules. Maybe > the TableEditor.php publised on web contain > some bug ? > |

Comments
Posted: 14th April 2005 08:33
You did a very useful module.
Btw, do you know any other such modules?
And my addition to your TODO list: would be great to make an option to show not all table but only rows that match some condition. Something like Search function, but not allowing to see not matching rows.