php - How to get add, edit, next & previous buttons on HTML form

284

Solution:

First of all, you should ensure that you have a column for the record ID that's anINTEGER, aPRIMARY KEY, and set toAUTOINCREMENT, let's call this `recordID`.

Let's take an example person table schema:

CREATE TABLE people (
    recordID INTEGER PRIMARY KEY AUTOINCREMENT,
    firstName VARCHAR(140) NOT NULL,
    middleNames VARCHAR(250),
    lastName VARCHAR(140) NOT NULL,
    dateOfBirth DATE NOT NULL
);

To query the first record, we can do:

SELECT * FROM people WHERE recordID = 1;

Now to edit the record, we can do:

UPDATE people SET firstName="NewName" WHERE recordID = 1;

Next we build an HTML form to display/edit this data in.

<form action="#" method="post">
    <input type="text" readonly="readonly" name="recordID" id="recordID" />
    <input type="text" name="firstName" id="firstName" />
    <input type="text" name="middleNames" id="middleNames" />
    <input type="text" name="lastName" id="lastName" />
    <input type="date" name="dateOfBirth" id="dateOfBirth" />
    <input type="submit" />
</form>

Last of all you create some next and previous buttons to traverse through the records and populate the input fields, and then an edit button that sends the data to the server for it to update the database.

If you're feeling extravagant, you could use SQL'sINSERT INTO ... ON DUPLICATE KEY UPDATE. E.g.:

INSERT INTO people (firstName, lastName, middleNames, dateOfBirth) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE firstName="?", lastName="?", middleNames="?", dateOfBirth="?";

In order to do an "in-place edit", you could add a variable to the querystring. For example, if you want to edit recordID = 3, you could have the URL as: http://yourserver.com/person/?id=3&edit.

On the serverside you can check for edit by usingisset($_GET['edit']). If that returns true, than run your edit code and populate the fields/enable the edit functionality.

People are also looking for solutions to the problem: Making Javascript array with PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.