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 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!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

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




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