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:
Ajax Application Generator Generate database 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 13th, 2002, 06:39 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
Inserting Data into two tables

I've got this products page form and what I have is two tables to insert the data into, about_products,about_urls.

What I am trying to achieve is to insert info into about_products table and the URLs associated for that product. The thing is, is that I'm having trouble getting to insert the URLs for that product.

Database Structure:

PHP Code:
 desc about_products;
+----------+--------------+------+-----+---------+----------------+
Field    Type         Null Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
pid      int(11)      |      | PRI NULL    auto_increment |
title    varchar(255) |      |     |         |                |
intro    text         YES  |     | NULL    |                |
price    varchar(255) |      |     |         |                |
prodinfo text         YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

mysqldesc about_urls;
+-------+--------------+------+-----+---------+----------------+
Field Type         Null Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
uid   int(11)      |      | PRI NULL    auto_increment |
url   varchar(255) |      |     |         |                |
pid   int(11)      |      |     | 0       |                |
+-------+--------------+------+-----+---------+----------------+ 


As you can see I've used pid as a foreign key to about_products.

code:
PHP Code:
<form action="products.php" method="post" ENCTYPE="multipart/form-data">
<
table cellpadding="2" cellspacing="2" border="1" width="100%">
<?
 
   for (
$n=1;$n<9;$n++)
   {
 
?>
<tr>
   <td><b>Title of Product</b>:</td>
   <td><input type="text" name="title[<? echo $n ?>]" size="45"></td>
</tr>
<tr>
   <td><b>Price</b>:</td>
   <td><input type="text" name="price[<? echo $n ?>]" size="45"></td>
</tr>
<tr>
   <td valign="top"><b>Introductory text about the product</b>:</td>
   <td><textarea cols="50" rows="5" wrap="virtual" name="intro[<? echo $n ?>]"></textarea><p></td>
</tr>
<tr>
   <td valign="top"><b>Product Details</b>:</td>
   <td><textarea cols="50" rows="5" wrap="virtual" name="prodinfo[<? echo $n ?>]"></textarea><p></td>
</tr>
<tr>
   <td valign="top"><b>URLs</b>:<br>MUST BE IN FORMAT [url]http://www.manutd.com/whatever[/url]</td>
   <td>
<?
     
for ($x=0;$x<3;$x++)
     {
?>
       <input type="text" name="url[<? echo $n ?>][<?echo $x?>]" size="45"><br>
<?
     
}
?>
   </td>
</tr>
<tr>
   <td colspan="2">&nbsp;</td>
</tr>
<?
   
}
?>
<tr>
   <td colspan="2" align="center"><input type="submit" name="submit" value="Enter"></td>
</tr>
</table>
</form>

 if ($submit=='Enter')
   {
     $found=0;
     while (list($key1) = each($url))
     {
       while ((list($key2,$val)= each($url["$key1"])) && $found==0)
       {
         if ($val!='' && substr($val,0,7)!='http://')
         {
           $found=1;
           echo "You must start with [url]http://[/url]<br>\n";
         }
         else
           $urls[]=$val;
       }
     }
 
     foreach ($urls as $var=>$value)
       echo "$value<br>\n";
 
     $num=count($urls);
     if ($found==0)
     {
       $index=0;
       for ($n=1;$n<9;$n++)
       {
         if ($title[$n] !="")
         {
           $query="insert into about_products (title,intro,price,prodinfo) ";
           $query.="values ('".$title[$n]."','".$intro[$n]."','".$price[$n]."','".$prodinfo[$n]."')";
           echo "$query<br>\n";
           mysql_query($query);
           $pid=mysql_insert_id();
 
           echo "insert into about_urls (url,pid)
                 values ('".$urls[$x]."',$pid)<br>\n";
           #mysql_query("insert into about_urls (url,pid)
           #             values ('".$urls[$index++]."',$pid)");
         }
       }
     } 


So what I am trying to achieve here is to insert the product details into the database, get the last id then insert into about_urls table, the urls associated for that product. (which they can be upto 3 urls for that product).

at the moment its doing this....

http://www.manutd.com/
http://www.mancity.com/ (Product 1)

http://www.df.com/
http://www.msdf.com/ (Product 2)

insert into about_products (title,intro,price,prodinfo) values ('sd','sd','sd','d')
insert into about_urls (url,pid) values ('http://www.manutd.com/',91)
insert into about_products (title,intro,price,prodinfo) values ('ghj','h','gj','')
insert into about_urls (url,pid) values ('http://www.mancity.com/',92)

So really it should insert the first product into database with the two urls, then next product and the two urls for that...

I'm going astray here but don't know how to overcome this sticky problem.

Any help much appreciated...

Ozzie

Reply With Quote
  #2  
Old June 14th, 2002, 08:55 AM
PabstER PabstER is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2002
Posts: 26 PabstER User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Have you tried using mysql_insert_id() ?

Reply With Quote
  #3  
Old June 14th, 2002, 09:54 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
No, I haven't, in what context do you mean??

Thanks


Ozzie

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Inserting Data into two tables


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 3 hosted by Hostway