473,574 Members | 2,914 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1770
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_recor d" 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_recor d" 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($error s)){
// 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=2 4
// 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
2047
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 with message "access disallowed from scripts at XXXX to documents at another domain"). 1) I have a frameset http://host/PATH/pinco,html in whose...
8
5266
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 write a query to pull records that match a variable input. In Oracle, I'd use a substitution variable like this... SELECT * FROM jobs WHERE jobid...
13
9602
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 assigned to a variable and output using document.write. (Note, there is no submit button or other form elements. Basically
15
4738
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 button will verify the information that has been entered and updates the data base if the data is correct. Update will throw an exception if the data is...
11
8011
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 display. If they put a value in the text box in the second row then display the third row etc. etc. etc. to 10 rows. I'm pretty new to javascript,...
3
3116
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 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...
3
3987
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 with a createElement function to add additional entries. The table has the first row of input text boxes already in it. You have to click a button to...
10
2036
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 id="form1", action="testing.htm" method="post"> Your name: <input type="text" name="name"/> </form>
2
3618
by: Abneo | last post by:
Hi all, I am very new to Javascript and I am in some need of some help. I am creating a site that has a madlib. I got the madlib code but now I want the results of the madlib to display in another frame. The user input will be in one from and the output should be in seperate frame. I have written the code for the madlib but when I hit the...
5
8102
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 to input (only once) a date. Is this something that I program into the report? Also, this is related but may require a separate posting, but can I...
0
7805
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7726
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8237
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7817
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8098
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6454
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1060
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.