General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Iron Speed
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old June 10th, 2002, 11:13 AM
ozzie ozzie is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: London
Posts: 11 ozzie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Entering multiple users in DB

Hello Again....

I'm trying to enter multiple users into the database, but what I am trying to achieve is to enter all the contact details in one go then the next set of contact details...I'll post the code here to get more of an understanding...

Database structure...

PHP Code:
+----------+--------------+------+-----+---------+----------------+
Field    Type         Null Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
cid      int(11)      |      | PRI NULL    auto_increment |
name     text         |      |     |         |                |
title    varchar(255) |      |     |         |                |
email    varchar(255) |      |     |         |                |
street   varchar(255) |      |     |         |                |
street2  varchar(255) |      |     |         |                |
town     varchar(255) |      |     |         |                |
postcode varchar(255) |      |     |         |                |
number   int(11)      |      |     | 0       |                |
npid     int(11)      |      |     | 0       |                |
+----------+--------------+------+-----+---------+----------------+ 


PHP Code:
if ($submit=='Enter')
   {
     if ((
$email=='') || is_email($email))
     {
       echo 
'<br><center><b>Invalid Email Address</b></center>';
     }
     else
     {
       
# stuck on what to implment here...
       # insert values into DB to go here
         
      
}
 
     }
 
   } 


PHP Code:
<form action="contacts.php" method="post" ENCTYPE="multipart/form-data">
<
table cellpadding="2" cellspacing="2" border="1" width="100%">
<?
   for (
$n=1;$n<13;$n++)
   {
?>
<tr>
   <td><b>Name</b>:</td>
   <td><input type="text" name="name[<? echo $n ?>]" size="45"></td>
   <td><b>Job Title</b>:</td>
   <td><input type="text" name="job_title[<? echo $n ?>]" size="45"></td>
</tr>
   <td valign="top"><b>Email Address</b>:</td>
   <td colspan="3"><input type="text" name="email[<? echo $n ?>]" size="45"></td>
</tr>
<tr>
   <td><b>Address</b>:</td>
   <td colspan="3"><input type="text" name="street[<? echo $n ?>]" size="45"></td>
</tr>
<tr>
   <td>&nbsp;</td>
   <td colspan="3"><input type="text" name="street2[<? echo $n ?>]" size="45"></td>
</tr>
<tr>
   <td><b>Town/City</b></td>
   <td colspan="3"><input type="text" name="town[<? echo $n ?>]" size="45"></td>
</tr>
<tr>
   <td><b>Postcode</b></td>
   <td colspan="3"><input type="text" name="postcode[<? echo $n ?>]" size="45"></td>
</tr>
<tr>
   <td><b>Upload Picture</b></td>
   <td colspan="3"><input type="file" name="file<? echo $n?>" size="58"></td>
</tr>
<tr>
    <td colspan="4">&nbsp;</td>
</tr>
<?
 
   
}
 
?>
<tr>
   <td colspan="4" align="center"><input type="submit" name="submit" value="Enter"></td>
</tr>
</table>
</form> 


So basically I've got the contact form repeating 12 times, so they could enter upto 12 contacts for instance. When submit the form then they should go into the database.


I've placed the values into arrays, but I'm stuck on how to implment the way you insert into the database.

Any help greatly received.

Ozzie

Reply With Quote
  #2  
Old June 10th, 2002, 06:00 PM
mytch mytch is offline
Dev Articles Novice (500 - 999 posts)
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 589 mytch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Hey Ozzie,
Well personally I would use a for loop to get the values from the $_POST associative array and add them, something like this:

for($i = 0; $i < 12; $i++)
{
$query = "insert into myTable(name, age, sex) values(";
$query .= "'" . $_POST["name_$i"] . "', ";
$query .= $_POST["age_$i"] . ", "'" . $_POST["sex_$i"] . "')";
}

etc... you would call the form values name_1, age_1, sex_1, name_n, age_n, etc.... so each one has a number from 1-12 at the end...

Reply With Quote
  #3  
Old June 11th, 2002, 03:07 AM
ozzie ozzie is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: London
Posts: 11 ozzie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks

Hey Mytch...

Big thanks for that, never come across $_POST before, what does that mean?

Thanks


Ozzie

Reply With Quote
  #4  
Old June 11th, 2002, 05:02 AM
ozzie ozzie is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: London
Posts: 11 ozzie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Wink Another query

I was wondering this will insert into the database 12 times, but what happens if the user enters one contact for instance can this be achieved??

THanks

Ozzie

Reply With Quote
  #5  
Old June 12th, 2002, 06:09 PM
mytch mytch is offline
Dev Articles Novice (500 - 999 posts)
 
Join Date: Apr 2002
Location: Sydney, Australia
Posts: 589 mytch User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Ozzie, $_POST is the same as $HTTP_POST_VARS from older version of PHP. It contains name/value pairs for the details entered into your HTML form.

You can easily just add one, just use a check to see whether or not they are empty, something like this:

for($i = 0; $i < 12; $i++)
{
if($_POST["name_$i"] != "")
{
$query = "insert into myTable(name, age, sex) values(";
$query .= "'" . $_POST["name_$i"] . "', ";
$query .= $_POST["age_$i"] . ", "'" . $_POST["sex_$i"] . "')";
}
}


Reply With Quote
  #6  
Old June 13th, 2002, 04:05 AM
ozzie ozzie is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Location: London
Posts: 11 ozzie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks

Mytch....

Thanks for the code....but I done this, this way so to speak...see what you think...

PHP Code:
for ($n=1;$n<13;$n++)
{
   if (
$name[$n] != "")
   {
           
$query="insert into about_contacts (name,title,email,street,street2,town,postcode,num  ber,npid) values";
           
$query.="('".$name[$n]."','".$title[$n]."','".$email[$n]."','".$street[$n]."','".$town[$n]."','".$postcode[$n]."',$n,$DBnpid)";
           echo 
$query;
           
#mysql_query($query);
         
}
       } 


Works well indeed when I echo the query out, it displays the number of times the user has enter n contacts.

Thanks Mytch

Ozzie.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Entering multiple users in DB


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway