Link to home
Start Free TrialLog in
Avatar of Scott Walker
Scott WalkerFlag for United States of America

asked on

How to inline edit a MySQL table

Hello Everyone,

I have setup a MySQL database that I use as a changelog, and I was wondering if there is any way to edit the table on a php page instead of having to go into PHPMyAdmin. Thanks!
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

As long as the php page has the queries on the backend to acquire the data and process a form with updates, it is just an ordinary operation.

PHPmyadmin is just a web interface to the mysql API.

Cd&
Yes.  The general design is to SELECT the contents of the table you want to edit, and render the column names into the NAME= attributes of HTML form input tags.  You would also render the contents into the VALUE= attributes of the input tags.  When the form is submitted, the old (or new) names and values will be in the $POST array.  From this you would construct a query to INSERT or UPDATE the table.  Lather, rinse, repeat.
Avatar of Scott Walker

ASKER

Thanks for the quick response! Sorry I wasn't clear, but I'm looking for some form of PHP code to use that will allow me to do inline editing on a page.
What's wrong with using phpMyAdmin?  From a functional perspective, you'll just be duplicating its abilities, minus a lot of the broader capabilities.

Are you new to PHP?  If so, this article may be able to help with some of the "deep background" and good learning resources.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Also, there is this ;-)
http://www.ee-stuff.com/Newsletter-old/061913newsletter.htm
Edit what?  If you are updating the database then there are three steps.

Acquire the original date from the db and put it in a form on a page.
Apply whatever changes are necessary and return the form to the server
Update the database.

What additional option do you think exists?

Cd&
Avatar of Dave Baldwin
What do you mean by 'inline editing'?  If you mean like editing a text file, that can't happen with a MySQL table because it is not organized 'inline' like that.  No SQL tables are like that.
Yes I am a newbie when it comes to PHP, and when I mean inline editing I'm talking about having an "edit" button at the end of each row that would allow me to edit that row. The reason why I don't want to use PHPMyAdmin is because both me and my dad are using this and it would be much more convenient. Thanks!
I keep thinking I should write an article about this... But maybe you can beat me to it.

If you want to post the CREATE TABLE statements, we might be able to help you get started.  Don't be in too much of a rush -- there are a lot of moving parts!

You will need to understand about how PHP processes HTML forms.  On the PHP.net web site they have an introductory tutorial.  See especially the part about Dealing With Forms.
http://php.net/tut.php

You will need to understand client/server protocols.  This article can help with that part of things.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html

You will need to understand how the MySQL data base works.  This article can help with that part of it.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
How does having two people use it affect what you want?

Since you would have to duplicate some of the code in phpMyAdmin, I'm not sure where the 'more convenient' part comes into it.  You can write fairly simple code to edit a single table.  But it stops being simple when you want to edit 'any' table because you have to get not only the data but the table structure and several other things to make it 'generic'.  And then you have to create the dynamic displays and forms to edit the data.

There are quite a few examples on the web for editing a single table.  I don't want to recommend anything since I don't know what you are familiar with.
AND... It still comes down to three steps whether you edit a row at a time in a table structure with buttons on the rows.

So for some strange reason related to more than one person has to have access you want to reinvent the wheel, and perhaps even get the wheel round, and you are prepared to maintain the wheel to keep up with cross-version changes to mysql; changes in the database; and changes in requirements.  Yes it makes perfect sense to not use a proven available method, when you can spend a substantial amount of time getting some duplication of the functionality to work.

Cd&
People seem to be confused, so I am going to explain it as best as I can. I want to view a SINGLE table's data and be able to edit a row by clicking an "edit" button or link on the end of the row, so that I can edit THAT ROW's data in the database. I want to do this instead of PHPMyAdmin because it takes time to sign into PHPMyAdmin and navigate to the editor. Hope this clears things up
Please create and post the SSCCE so we can get a clear understanding of what you want, thanks. ~Ray
No, we're not confused.  We've these things many times, many ways.  Now we're trying to figure out which one you're talking about.  I'm also curious where this is going to be hosted if it isn't going to require a login to keep the creepies out.
I think we understand what you want to do.  It's one of the "PHP 101" exercises I give my students at the end of the first week of class.  It's not hard at all, but it has several moving parts that require a level of understanding and integration.  To help you get started with PHP and MySQL, please read this article, then go back and read the articles I recommended earlier.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

After reading the articles, please create the SSCCE that will show us where you're having trouble with your code.  Once we see your starting point, it will be easy for us to help you.
???? If I am doing a lot of direct editing using phpmyadmin, I just keep it in an open tab.  Click the tab, there is the table I am working with, click on the field I want to edit, make the change. Done... go back to another tab... ???

Even with older versions of phpmyadmin where I have to click inline edit first there is no delay, I get instant access and instant update.

Cd&
As for a starting point, I have a php page that displays the data in the table, and I would like to be able to edit on this page as well: (note that I didn't create this code; I found an example online and modified it)
<?php
$con=mysqli_connect("192.168.0.9","plugins","","changelog");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM log");

echo "<table border='1'>
<tr>
<th>Who Did It?</th>
<th>When Was It Done?</th>
<th>What Site Was It Done On?</th>
<th>What Was Done?</th>
<th>What Pages Were Involved?</th>
<th>Who Tested It?</th>
<th>GA Annotation?</th>

</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['who'] . "</td>";
  echo "<td>" . $row['date'] . "</td>";
  echo "<td>" . $row['site'] . "</td>";
  echo "<td>" . $row['activity'] . "</td>";
  echo "<td>" . $row['pages_involved'] . "</td>";
  echo "<td>" . $row['tested_by'] . "</td>";
  echo "<td>" . $row['GA_annotation'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>

Open in new window

Editing requires you to get the info from the database and put it in a form on a page.  That form must submit to a PHP page that checks the data and does an UPDATE to the right row in the database.  That sounds pretty simple but there are a lot of details to getting it done right.  It can be done all on one page but so far you just have the 'getting the data' part.
That's exactly what I am looking for.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I need to find a book on round things so I can invent a wheel. ;^)

Cd&
@COBOLdinosaur: Or, "I want to build a car, but I don't know what all that metal stuff is under the hood."

@hdw632: We're not picking on you and there is no harm in ignorance - we were all ignorant and new to this stuff once. It just takes a while to learn enough of the fundamentals to use the PHP language effectively.  The same is true of human languages.  If you've never spoken Italian, you can certainly learn some Italian in a few weeks of immersive study.  But if you want to write Italian poetry, you need to be a little more patient with yourself, gathering knowledge and building up a vocabulary of words and phrases.  The same is true of programming languages.  That's why we're pointing you in the direction of learning resources (books online courses, etc).  And you will be much less of a newbie in just a few months of study.