Computer Webmaster Gaming Console Graphics Forum

Welcome to the Computer Webmaster Gaming Console Graphics Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

MK PitStop Main Earn $25 Earn Money Posting Extras Members Blogs Image Hosting User Pages
Go Back   Computer Webmaster Gaming Console Graphics Forum > Webmaster Forum > Website Coding > Database
Register FAQ/Rules Become A V.I.P. Member Search Today's Posts Mark Forums Read

Database Database problems or need to ask a question? maybe something to do with sql injections or a database software question. Database topics cover MySQL, PostgreSQL, Oracle, SQL Server or anything else related to databases.

Google
Closed Thread
 
LinkBack Thread Tools Display Modes
Old 07-01-2007, 9:35 PM   #1
Bob Sanderson
 
Bob Sanderson's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default 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.

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Advertisements
Old 07-01-2007, 9:35 PM   #2
Paul Lautman
 
Paul Lautman's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default Input and Display Records With Same Script?

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.


 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 9:35 PM   #3
Justin Koivisto
 
Justin Koivisto's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default Input and Display Records With Same Script?

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/
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 9:35 PM   #4
Justin Koivisto
 
Justin Koivisto's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default Input and Display Records With Same Script?

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
}
}
?>
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Featured Websites
Free Space
Free Space
Free Space Free Space
Closed Thread
Tags: , , , ,




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
phantom records TB Database 2 07-01-2007 7:32 PM
How do you display users total records based on user level ? M.E. PHP 1 07-01-2007 3:18 PM
How NOT to Insert Duplicate Records? Antithesis PHP 2 07-01-2007 3:18 PM
Display top N records with a group Ted Database 0 06-10-2007 12:25 AM
how to get records from stored procedure using ASP Bill Database 0 06-10-2007 12:19 AM


Featured Websites




All times are GMT +1. The time now is 12:19 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0
Cheap Computers
MK PitStop Copyright 2005 - 2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98