473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Input and Display Records With Same Script?

I am trying to create a form for a MySQL database similar to a spreadsheet.
The idea is to display a list of records, with the last line of the list
being an input form. When the user enters data in the form and hits the
submit button, the data is entered and the form is reloaded with the new
data displayed and another input form becomes the last line.

Example ---

Before entering new data

Record 1
Record 2
Record 3
Input form Submit button
After entering new data

Record 1
Record 2
Record 3
Record 4
Input form Submit button

It seems is what I think the approach should be:

1. Display all current records - the last line is an input form
2. User adds data to the form and hits the Submit button
3. The form action calls the same script
4. The new data is entered into the database
5. Back to step one

Does this make sense or is there a better way? How do I structure the
queries to accomplish this?

Thanks in advance.

May 18 '06 #1
3 3096
Bob Sanderson wrote:
I am trying to create a form for a MySQL database similar to a
spreadsheet. The idea is to display a list of records, with the last
line of the list being an input form. When the user enters data in
the form and hits the submit button, the data is entered and the form
is reloaded with the new data displayed and another input form
becomes the last line.

Example ---

Before entering new data

Record 1
Record 2
Record 3
Input form Submit button
After entering new data

Record 1
Record 2
Record 3
Record 4
Input form Submit button

It seems is what I think the approach should be:

1. Display all current records - the last line is an input form
2. User adds data to the form and hits the Submit button
3. The form action calls the same script
4. The new data is entered into the database
5. Back to step one

Does this make sense or is there a better way? How do I structure the
queries to accomplish this?

Thanks in advance.


Funny you should ask, I just did a similar thing, except my input was at the
top with the records displayed in descending order.

1) Check if form has been submitted
1a) if form has been submitted, INSERT data into database
2) Query database to get all records
3) Output all records
4) Output Form elements

That's it. You may want to edit-check the data and send an error message if
the data doesn't pass muster.
May 18 '06 #2
Bob Sanderson wrote:
I am trying to create a form for a MySQL database similar to a spreadsheet.
The idea is to display a list of records, with the last line of the list
being an input form. When the user enters data in the form and hits the
submit button, the data is entered and the form is reloaded with the new
data displayed and another input form becomes the last line.

It seems is what I think the approach should be:

1. Display all current records - the last line is an input form
2. User adds data to the form and hits the Submit button
3. The form action calls the same script
4. The new data is entered into the database
5. Back to step one

Does this make sense or is there a better way? How do I structure the
queries to accomplish this?


That would work fine:

<?php
if(isset($_POST['add_record'])){
// verify POST fields for type, etc.
$q='INSERT INTO table1 (field2, field3, field4) VALUES ('.
'\''.mysql_real_escape_string($_POST['field2']).'\''.
'\''.mysql_real_escape_string($_POST['field3']).'\''.
'\''.mysql_real_escape_string($_POST['field4']).'\''.
')';
// where there is an auto_increment field for PK (field1)
// fields 2-4 are text or varchar in example

// execute query, send message, etc.
}

$q='SELECT field1, field2, field3 field4 FROM table1';

// execute query, loop, display list

// add form to end of list with a submit button at end like
// <input type="submit" name="add_record" value="Add Record' />
?>

If you want to be able to edit each record, make them all form fields
with the values filled in. Use fields like:
<?php
echo "<input type='text' name='field1[$recID]' value='$value' />";
?>

For the last row, have it be the same format, just use 0 as the id
value. That way, when you post, you'll have something like the following
as your POST array structure:

$_POST = array (
[field1] = array (
[0] = New Rec value
[1] = value1
[2] = value2
[24] = value24
)
[field2] = array (
[0] = New Rec value2
[1] = value1-2
[2] = value2-2
[24] = value24-2
)
)
If there wasn't anything new added to the form, the structure would be
the same, but the new values would be empty.

HTH

--
Justin Koivisto, ZCE
http://koivi.com/
May 18 '06 #3
Justin Koivisto wrote:
Bob Sanderson wrote:
I am trying to create a form for a MySQL database similar to a
spreadsheet. The idea is to display a list of records, with the last
line of the list being an input form. When the user enters data in the
form and hits the submit button, the data is entered and the form is
reloaded with the new data displayed and another input form becomes
the last line.

It seems is what I think the approach should be:

1. Display all current records - the last line is an input form
2. User adds data to the form and hits the Submit button
3. The form action calls the same script 4. The new data is entered
into the database
5. Back to step one

Does this make sense or is there a better way? How do I structure the
queries to accomplish this?


That would work fine:

<?php
if(isset($_POST['add_record'])){
// verify POST fields for type, etc.
$q='INSERT INTO table1 (field2, field3, field4) VALUES ('.
'\''.mysql_real_escape_string($_POST['field2']).'\''.
'\''.mysql_real_escape_string($_POST['field3']).'\''.
'\''.mysql_real_escape_string($_POST['field4']).'\''.
')';
// where there is an auto_increment field for PK (field1)
// fields 2-4 are text or varchar in example

// execute query, send message, etc.
}

$q='SELECT field1, field2, field3 field4 FROM table1';

// execute query, loop, display list

// add form to end of list with a submit button at end like
// <input type="submit" name="add_record" value="Add Record' />
?>

If you want to be able to edit each record, make them all form fields
with the values filled in. Use fields like:
<?php
echo "<input type='text' name='field1[$recID]' value='$value' />";
?>

For the last row, have it be the same format, just use 0 as the id
value. That way, when you post, you'll have something like the following
as your POST array structure:

$_POST = array (
[field1] = array (
[0] = New Rec value
[1] = value1
[2] = value2
[24] = value24
)
[field2] = array (
[0] = New Rec value2
[1] = value1-2
[2] = value2-2
[24] = value24-2
)
)
If there wasn't anything new added to the form, the structure would be
the same, but the new values would be empty.


BTW - if you will have a lot of records, instead of having a huge form
like this, you will be better off having separate forms for editing via
a link in the row of the list. (I like using Edit and Delete links.)
Then in the code you'd have something more like:

<?php
if(isset($_POST['add_record'])){
// insert stuff
}else if(isset($_POST['edit_record'])){
// update stuff
}else if(isset($_POST['delete_record'])){
// delete stuff
}

if(isset($errors)){
// I store error messages in an array so I can output them all at once
// for the user to review. You may want to do other things here as well
}else{

if(isset($_GET['edit'])){
// show edit form. I like to use URIs like:
// page.php?edit=24
// when I want to edit record 24
}else if(isset($_GET['delete'])){
// show delete confirm form
// I use the same type of URI as the edit for this as well
}else{
// no action request, show list with links
}
}
?>
May 18 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: LC's No-Spam Newsreading account | last post by:
I have the following arrangement working under Netscape 3 / Unix, IE6 / Win and Konqueror / Linux, but NOT under Netscape 7 Unix or Mozilla Linux (silently fails) nor under Netscape 4 Unix (fails...
8
by: rgonzale6 | last post by:
Hello. I'm using SQL query analyzer to run some queries against an SQL2000 DB. Unfortunately, my previous SQL experience is some Oralce SQL I took in school. To put it simply, I'm trying to...
13
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be...
15
by: Nathan | last post by:
I have an aspx page with a data grid, some textboxes, and an update button. This page also has one html input element with type=file (not inside the data grid and runat=server). The update...
11
by: jimstruckster | last post by:
I have a table with 10 rows, I want all rows except for the first to be hidden when the page first opens up. If the user puts a value in a text box in the first row then I want the second row to...
3
by: Bob Sanderson | last post by:
I am trying to create a form for a MySQL database similar to a spreadsheet. The idea is to display a list of records, with the last line of the list being an input form. When the user enters data...
3
by: acecraig100 | last post by:
I am fairly new to Javascript. I have a form that users fill out to enter an animal to exhibit at a fair. Because we have no way of knowing, how many animals a user may enter, I created a table...
10
by: patelxxx | last post by:
Problem: I am trying to input my name in a text box and I click on transfer and I want the name to appear on the same page (i.e. in another form). How is this done? <BODY> <HTML> <form...
5
by: Brett | last post by:
Hello, Is it possible to have just one criteria and have it apply to a group of queries? I am trying to create a report with the separate results of 4 queries based on a prompt for the user...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.