PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingPHP Development

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 May 7th, 2012, 10:05 PM
SB2 SB2 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 6 SB2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 29 m 51 sec
Reputation Power: 0
Update if exists, else insert into

Apparently I got a bit cocky, where is this going wrong.
what its' supposed to do:

New customer:
New customer information input with INSERT INTO, then the $cust_id will be brought into the ORDER TABLE by an INSERT INTO (this all works)

Existing Customer:
IF Existing customer is in DB, then Update Address Information only use the existing Customers "Cust_id" to bring into the order table.

What is happening here is, The customer information is being updated correctly, and not adding a new ID.
the order is auto incrementing, BUT....the existing Customer ID is not being brought over, it puts a "0" in the table for cust ID on the existing customers new order.

Any help would be greatly appreciated, I've spent 3 days non stop on this. thank You...


$sql1 ="SELECT * FROM customer WHERE ln = '$ln' and fn = '$fn'";
$result1 = mysql_query($sql1) or die(mysql_error());
if(mysql_num_rows($result1) > 0){

$sql2 = "UPDATE customer
SET addr='$addr', addr2='$addr2', city='$city', st='$st', zip='$zip'
WHERE fn='$fn' AND ln='$ln'";
$result2 = mysql_query($sql2) or die(mysql_error());
print ("<tr><td>$cust_id</td></tr>");

}else{

$sql3 ="INSERT INTO customer (cust_id, fn, ln, addr, addr2, city, st, zip)
VALUES ('', '$fn', '$ln', '$addr', '$addr2', '$city', '$st', '$zip')";
$result3 = mysql_query($sql3,$connection) or die(mysql_error());
$cust_id = mysql_insert_id();
print ("<tr><td>$cust_id</td></tr>");
}


$sql4 = "insert into orders (order_num, order_dt, cust_id, shipcost, pymthd) values
('', NOW(''),'$cust_id','$shipcost', '$paymthd')";
$result4 = mysql_query($sql4,$connection) or die(mysql_error());
$order_num = mysql_insert_id();

Last edited by SB2 : May 9th, 2012 at 08:03 PM. Reason: not solved thought it was...but one minor detail missed

Reply With Quote
  #2  
Old May 8th, 2012, 09:36 PM
Pauline224 Pauline224 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 4 Pauline224 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 39 sec
Reputation Power: 0
URLURLURLURL

Reply With Quote
  #3  
Old May 9th, 2012, 09:13 PM
SB2 SB2 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 6 SB2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 29 m 51 sec
Reputation Power: 0
Not solved see above

I missed the detail of it not bringing over the customer id to the order table when a record is existing in the customer table

Reply With Quote
  #4  
Old May 9th, 2012, 09:58 PM
SB2 SB2 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 6 SB2 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 29 m 51 sec
Reputation Power: 0
NOW it is solved

$sql10 ="SELECT * FROM customer WHERE ln = '$ln' and fn = '$fn'";
$result = mysql_query($sql10, $connection)
or die(mysql_error());

{



while ($record = mysql_fetch_array($result))
{
$cust_id = isset($record['cust_id']) ? htmlentities($record['cust_id']) : '';
}
}
if(mysql_num_rows($result) > 0){
$sql30 = "UPDATE customer
SET addr='$addr', addr2='$addr2', city='$city', st='$st', zip='$zip'
WHERE fn='$fn' AND ln='$ln'";
$result30 = mysql_query($sql30) or die(mysql_error());
print ("<tr><td>$cust_id</td></tr>");
}else{
$sql20 ="INSERT INTO customer (cust_id, fn, ln, addr, addr2, city, st, zip)
VALUES ('', '$fn', '$ln', '$addr', '$addr2', '$city', '$st', '$zip')";
$result20 = mysql_query($sql20,$connection) or die(mysql_error());
$cust_id = mysql_insert_id();
print ("<tr><td>$cust_id</td></tr>");
}

hopefully this can help some newbies at least a little with similar situations.

Reply With Quote
  #5  
Old May 23rd, 2012, 08:32 AM
williamxaviers williamxaviers is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 5 williamxaviers User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 39 sec
Reputation Power: 0
What was the actual problem ? Was it related to the database connectivity or was something problem related to the database table ? Actually I am asking because I am also about to get into the topics of JDBC and ODBC very soon.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Update if exists, else insert into (or vice versa if not exists help)


Developer Shed Advertisers and Affiliates


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

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap