You are here: Re: Input and Display Records With Same Script? « PHP Programming Language « IT news, forums, messages
Re: Input and Display Records With Same Script?

Posted by Justin Koivisto on 05/18/06 17:34

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
}
}
?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация