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 March 14th, 2005, 12:06 AM
aron aron is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: brisbane
Posts: 42 aron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 9 m 35 sec
Reputation Power: 5
update sql help please

hi all. need help manulipilating this script to produce a update sql query

PHP Code:
 $INTEGER_FIELDS = array('property_id','development'); // as many as you need
    
$insert = array();
    foreach(
$_REQUEST as $key => $value){  
        if(
$value == '') continue;
        if(
$value == 'Submit') continue;
        if(
$value == '685388') continue;
        if(
$value == '12446606') continue; 
        if(
$value == '685388:12446606') continue; 


        if(
in_array($key$INTEGER_FIELDS)){
            
// Error checking
            
if(is_int($value)){
                die(
'Error ... ['.$key.'] field does not contain an integer.');
            }
            
$insert[$key] = $value;
        } else {
            
$insert[$key] = "'".$value."'";
        }
    }

    
// Error checking - maybe someone just clicked the submit button.
    
if(count($insert)){
        
$sql "INSERT into property (".join(','array_keys($insert)).",date) VALUES (".join(','array_values($insert)).",'tdate')"
        echo 
$sql
        
mysql_query($sql) or die("Invalid query: " mysql_error());
    } 


currently that builds the insert query (that works well)

now i want to do the same thing but build a update query.

cheers aron.

Reply With Quote
  #2  
Old March 14th, 2005, 02:48 AM
aron aron is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: brisbane
Posts: 42 aron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 9 m 35 sec
Reputation Power: 5
trailing , comma help

ok got it pretty much there.

PHP Code:
<?php
$INTEGER_FIELDS 
= array('property_id'); // as many as you need
$insert = array();
foreach(
$_REQUEST as $key => $value){  
    if(
$value == '') continue;
    if(
$value == 'Submit') continue;
    if(
$value == '0') continue;
    if(
$value == '685388') continue;
    if(
$value == '12446606') continue; 
    if(
$value == '685388:12446606') continue; 


    if(
in_array($key$INTEGER_FIELDS)){
        
// Error checking
        
if(is_int($value)){
            die(
'Error ... ['.$key.'] field does not contain an integer.');
        }
        
//$insert[$key] = $value;
        
$sql_2 .= ($key.'='.$value.', ');
    } else {
        
//$insert[$key] = "'".$value."'";
        
$sql_2 .= ($key."='".$value."', ");
    }
}
$sql "UPDATE listings SET $sql_2 WHERE id=$id" or die("Invalid query: " mysql_error());
echo 
$sql;
?>


produces

UPDATE listings SET property_id=236846, category='commercial', office_area='22', vparks='covered', type='All', additional_areas='loft', hot_water='solar_electric', tiled_roof='1', building_style='other', WHERE id=23

but how do i get rid of the trailing , comma

Reply With Quote
  #3  
Old March 14th, 2005, 08:02 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Instead of:
$sql_2 .= ($key.'='.$value.', ');

I would recommend something like this:
if(!empty($where)) $where.=',';
$where .= ($key.'='.$value);


Then outside to loop do this:
$sql_2.=$where;

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > update sql help please


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT