General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
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:
  #1  
Old June 10th, 2002, 12:13 PM
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, 07: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: 8
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, 04: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, 06: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, 07: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: 8
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, 05: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!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

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




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek