Problems inserting tabular data into DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

Problems inserting tabular data into DB

Post by amir »

I have 3 columns and 5 rows of textboxes, so total of 15 textboxes inside a table that i need to fill up. After hotiing submit, every one of the 5 rows has to submit as a seperate record with a new primary key everytime. So it would fill lets say starting from record 90 to 94. Do i have to use a loop? How do i do this? Wheat if one of my 3 comuns here is a redio button (y/n)?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Got any examples of what you have already tried?
obiron
Forum Newbie
Posts: 15
Joined: Fri Nov 10, 2006 4:50 am

Post by obiron »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Heres what I do.  I'm still learning so its chunky code, but it does work.  It is in use on a live website for adding players to a poker league

Build your form with a row identifier on it:-

Code: Select all

print "<FORM action = $PHP_SELF name = thirdpart method = post><TABLE><TR><TD>#</TD><TD>First Name</TD><TD>Surname</TD><TD>NickName</TD><TD>Email</TD><TD>UserName</TD><TD>Password</TD><TD>MembershipNo</TD></TR>";
   for ($i = 1; $i <= $_POST[NumberNewPlayers]; $i++)
     {
       $PrintString = "<TR><TD>$i</TD><TD><input type = text name=$i" . "Fname></TD>";
       $PrintString = $PrintString . "<TD><input type = text name=$i" . "Sname length = 20></TD>";
       $PrintString = $PrintString . "<TD><input type = text name=$i" . "Nname length = 20></TD>";
       $PrintString = $PrintString . "<TD><input type = text name=$i" . "Email length = 40></TD>";
       $PrintString = $PrintString . "<TD><input type = text name=$i" . "Uname length = 10></TD>";
       $PrintString = $PrintString . "<TD><input type = text name=$i" . "Pword length = 10></TD>";
       $PrintString = $PrintString . "<TD><input type = text name=$i" . "MembNo length = 6></TD></TR>";
       print $PrintString;
     } // END for $i loop
print "</TABLE></FORM>";

After submission and validation, if anything is wrong, repost the form with the values

Code: Select all

print "<FORM FORM action = $PHP_SELF name = thirdpart method = post><TABLE><TR><TD>First Name</TD><TD>Surname</TD><TD>NickName</TD><TD>Email</TD><TD>UserName</TD><TD>Password</TD></TR>";
  for ($i = 1; $i <= $_POST[NumberNewPlayers]; $i++)
    {
     $PrintString =            "<TR><TD>$i</TD><TD><input type = text name=$i" . "Fname value = " . $_POST[$i . Fname]."></TD>";
     $PrintString = $PrintString . "<TD><input type = text name=$i" . "Sname value = " . $_POST[$i . Sname]."></TD>";
     $PrintString = $PrintString . "<TD><input type = text name=$i" . "Nname value = " . $_POST[$i . Nname]."></TD>";
     $PrintString = $PrintString . "<TD><input type = text name=$i" . "Email value = " . $_POST[$i . Email]."></TD>";
     $PrintString = $PrintString . "<TD><input type = text name=$i" . "Uname value = " . $_POST[$i . Uname]."></TD>";
     $PrintString = $PrintString . "<TD><input type = text name=$i" . "Pword value = " . $_POST[$i . Pword]."></TD>";
     $PrintString = $PrintString . "<TD><input type = text name=$i" . "MembNo value = " . $_POST[$i . MembNo]."></TD></TR>";
     print $PrintString;
    } // END for $i loop
  print "</TABLE></FORM>";
on submission and validation passed, process the records:

Code: Select all

for ($i = 1;$i <=$_POST[NumberNewPlayers]; $i++)
   {$Fields = "MembershipNo,FName, Sname, Nname, Email, Uname, Pword,HomeLeague,IsPlayer";
       //  The values inserted into the database need to be cleaned before they are used
       $Fname = $_POST[$i . Fname];
       $Sname = $_POST[$i . Sname];
       $Nname = $_POST[$i . Nname];
       $Email = $_POST[$i . Email];
       $Uname = $_POST[$i . Uname];
       $Pword = $_POST[$i . Pword];
       $MembNo = $_POST[$i . MembNo];
       $Fname = fn_CleanMe($Fname,20,NoSpaces,NoQuotes);
       $Sname = fn_CleanMe($Sname,30,NoSpaces,Single);
       $Nname = fn_CleanMe($Nname,15,Spaces,NoQuotes);
       $Uname = fn_CleanMe($Uname,20,NoSpaces,NoQuotes);
       $Pword = fn_CleanMe($Pword,10,NoSpaces,NoQuotes);
       $Values = "$MembNo,'$Fname', '$Sname', '$Nname', '$Email', '$Uname', '$Pword',$_POST[HomeLeague],1";
       $SQL_INSERT = "INSERT into User ($Fields) VALUES ($Values)";
}
obviously my fields are all text and you will need to change the input types to select statements with their relevant options and you will also need to pass the [NumberNewPlayers] through the forms in a hidden field if the number of lines is not fixed. Rather than prefixing the field names with a counter you could build arrays and I am sure somone will suggest building the insert statements as a single query string rather than separate submissions each time but the above does work.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply