PHP form code maintainance - php

I am working on a database in mysql and I need to make a user facing page that allows me to enter text for each field and then submit the record. I have been able to accomplish this easily, however it is getting quit annoying having to update the input.html and save.php every time I decide to add/remove a field.
It really seems like there should be some sort of program that can auto-maintain the code for me and allow me to just focus on the database structure. Does anyone know of something that does this? I feel like I am doing it all wrong.
Thanks in advance.
P.S. I realize that I could just use phpmyadmin, but I do not want to give full DB access to my data entry people; plus they are not technical types, I don't want to intimidate them.

In the end, I decided to make my own script, using INFORMATION_SCHEMA to get the field names, and then made a recursive loop to add each record. It wasn't hard, but it seems like there should still be a better way.

Related

Mysql query backup. What would be considered best practices?

I've been doing some googling but I don't know if my search terms could be better.
I am making a database that will keep customer information. I have a few reps that will have access to that data using the php website front end and I would like to put something in place to allow me to undo a mistake that could occur.
I initially thought to make a trigger for all update and/or insert queries in which it would save the query used and would make a backup of the contents of the table before the query was executed.
I wasn't sure if that would be considered best practice or if there was a better option I could be overlooking.
I would appreciate any input on if this route seems best or should I be looking towards a different option.
Thanks!

Recreate a database using existing php code

So I have an old website which was coded over an extended period of time but has been inactive for 3 or so years. I have the full PHP source to the site, but the problem is I do not have a backup of the database any longer. I'm wondering what the best solution to recreating the database would be? It is a large site so manually going through each PHP file and trying to keep track of which tables are referenced is no small task. I've tried googling for the answer but have had no luck. Does anyone know of any tools that are available to help extract this information from the PHP and at least give me the basis of a database skeleton? Otherwise, has anyone ever had to do this? Any tips to help me along and possibly speed up the process? It is a mySQL database I'm trying to use.
The way I would do it:
Write a subset of SQLi or whatever interface was used to access the DB to intercept all DB accesses.
Replace all DB accesses with the dummy version of yours.
The basic idea is to emulate the DB so that the PHP code runs long enough to activate the various DB accesses, which in turn will allow you to analyze the way the DB is built and used.
From within these dummy functions:
print the SQL code used
regenerate just enough dummy results to let the rest of the code run, based on the tables and fields mentioned in the query parameters and the PHP code that retrieves them (you won't learn much from a SELECT *, but you can see what fields the PHP code expects to get from it)
once you have understood enough of the DB structure, recreate the tables and let the original code work on them little by little
have the previous designer flogged to death for not having provided a way to recreate the DB programatically
There are currently two answers based on the information you provided.
1) you can't do this
PHP is a typeless language. you could check you sql statements for finding field and table names. but it will not complete. if there is a select * from table, you can't see the fields. so you need to check there php accesses the fields. maybe by name or by index. you could be happy if this is done by name, because you can extract the name of the fields. finally the data types will missing. also missing: where are is an index on, what are primary keys, constrains etc.
2) easy, yes you can!
because your php is using a modern framework with contains a orm. this created the database for you. a meta information are included in the php classes/design.
just check the manual how to recreate the database.

Staged form and "going back" (clicking "back" in browser)

I have a very complicated form which has multiple "stages" in it.
Each stage has forms where the user adds indefinite numbers of rows. Each time the user clicks "continue" these forms save to the database (each "stage" has its own table in the database) and the next stage is shown.
I'm worried about users pressing "back" in order to get the the previous form (if they've made a mistake or something).
The only three ways I can think to solve this problem is:
Disallow people to press back. If they do send them straight to the first form to start again.
Check the database when the form page is requested. If there are rows already then delete those rows and start the user again on the form they're on.
[potential - This is going to be harder because I'm using codeigniter's form_validation library] Check the database when users press back. Get any current values out and put into the form as default values. Then on submit, remove any rows and replace with form values.
How do you guys do this kind of form? Its getting really ridiculous the amount of logic just for some forms! haha, is there a better way to do this? I suspect that number 3 above is the best option?
Thanks
I have used the third option on various occasions, though depending on the size of the form, this can certainly get overwhelming, not to mention the fact that I haven't used CodeIgniter's validation before.
Many issues arise from the first two options that should be avoided if possible [Including, but not limited to: Mistakes that aren't able to be corrected, hairy database logic, etc.], but the reward of having happy users is invaluable.
Hope this helps!
Mason
To give a visual summary of what users may think if they can't go back, I have included this image.
http://momentumshift.com/wp-content/uploads/2011/05/senior_woman_using_computer_mon142089.jpg
1 and 2 are both very bad for UX. 3 is your best bet, and depending on your db structure you might be able to update instead of delete and re-insert.
If you have a key, which i assume you do, in order to differentiate multiple form submissions, you can use something like insert on duplicate key update depending on you db engine.

A way to edit multiple rows of a table with different values at once

So the thing is that i want a way to edit multiple rows of a table by providing a excel sheet kind of feel.
The user should be able to enter values for different rows in the form, and once the save/submit is clicked all entries in the table must be updated.
I have a problem with figuring out how to implement this in my project.
can anyone please tell me how i can go about having this interface in code?
There are lot of JS frameworks (dojo, extJS, etc) which can help. As for me, i like DHTMLx one
One idea would be to map the row data structure (i.e. the fields of each row) to a value object. Then each row that is edited will be saved to an array. Then for each element, or object, in the array, you will build an update query and execute it.
This historical essay can help you get started:
http://www.atariarchives.org/deli/spreadsheet_program.php
One thing that I didn't see covered is that you have to look out for circular references, the simplest kind being A1=A2, and A2=A1. Graph theory gives you tools to manage these errors.

Put mysql data into html table with edit buttons

I'm attempting to make a ticket system for out smaller company. I've made a submission form that saves to a mySQL database. I can save the data into the table and pull the date into a html table. I've been searching all day, however, to figure out how to put in a cell that will allow me to click a button, link, or whatever to change that row to completed. I have a field in the database for it, but I don't know how to get that cell into the table or how to get the link to understand which row I'm talking about. I've been searching Google for awhile and only getting how to make a html table with mySQL data or how to INSERT into a mySQL table.
You'll need 3 things: an UPDATE statement, the field in the row you want to update, and some unique way of identifying which row (entry) you want to update.
You'll probably want to create a link something similar to:
<a href='/path/to/update.php?id=myUniqueIdentifier'>complete</a>
and on the php page you'll want to use the $_GET['id'] passed (make sure to use msyql_real_escape_string()) to UPDATE your row and set your completed flag to whatever you want.
Lynda.com has a solid MySQL essentials video that has something very similar to what you are looking for. It would definitely be a good starting point for building your own custom solution. Or I'm sure you could just use their user interface to interact with mysql inside the browser.
I have no affiliation with Lynda.com - I just used their site to learn almost everything i know about programming.

Categories