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:
Free Web 2.0 Code Generator! Generate data entry 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 December 8th, 2003, 07:47 PM
an-ath an-ath is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 4 an-ath User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
need help with php db function

hi all,

i'm an asp developer, but as a favor to friends i'm developing a content management sytem for their bookstore in php. i'm a newbie to this programming language so i hope someone will help me. basically i have a functions in asp which performs a sql inserts and updates regardless of which form submitted the information. i'm hoping i could get some direction on how to convert these functions to php. i stumbling on the $http_post_vars which i know is the rough equivalent to asp's request.form. however, i can't find any documentation on the equivalent of the full request.form collection, ie request.form.count, request.form.key, request.form.item. i've posted the asp update function below, any advice would be greatly appreciated.


Code:
function sqlUpdate(table_name,column_name,condition)
	sqlUpdate = "UPDATE " & table_name & " SET "
	for x = 1 to request.form.count
		fieldname = request.form.key(x)
		sqlUpdate = sqlUpdate & fieldname & " = "
		fieldvalue = request.form.item(x)
		fieldvalue = "'" & replace(replace(replace(replace(replace(replace _
			(fieldvalue,"!","!"),"%","%"), _
			";",";"),"<","<"),">",">" ),"'","'") & "'" & ", "			
		sqlUpdate = sqlUpdate & fieldvalue
	next
	
	sqlUpdate = StrReverse(sqlUpdate)
	sqlUpdate = StrReverse(Replace(sqlUpdate, ",", " ",1,1))
		
	sqlUpdate = sqlUpdate & " WHERE " & column_name & " = " & condition & ""
end function
sTablename = "entries"
sColumnname = "entry_id"
sCondition = id
	
sqlEdit = sqlUpdate(sTablename, sColumnname, sCondition)
set rsEdit = cnnDataConn.execute(sqlEdit)



thanx

Reply With Quote
  #2  
Old December 9th, 2003, 11:15 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Try something like the following:

PHP Code:
while(list($key,$val)=each($_POST)){
    
//do junk to the keys and vals.



The list($k,$v)=each($array) bit iterates through the array and sets $k and $v to the key and the value for each iteration. It should be pretty easy to find equivalent logic in PHP to do the rest of what you're doing.

Reply With Quote
  #3  
Old December 9th, 2003, 05:32 PM
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 4 m 48 sec
Reputation Power: 8
There's also my favourite:

PHP Code:
foreach ($_POST as $key => $value)
{
    
//do junk to the keys and vals.



PHP Manual Reference:
http://www.php.net/foreach

Reply With Quote
  #4  
Old December 9th, 2003, 08:18 PM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
an-ath, please post back whether the solutions worked, did not work, or if you need more clarification.

Thanks,
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
  #5  
Old December 9th, 2003, 10:15 PM
an-ath an-ath is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 4 an-ath User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hi again,

sorry i didn't post back sooner. the solutions posted do work, and I thank everyone for their assistance. i was just a little embarassed to post back because i'm getting a sql error. here is the code i constructed for test purposes:


PHP Code:
 $id $_GET["id"];
$sqlInput "UPDATE cds SET ";
foreach (
$_POST as $key => $value){
    
$sqlInput .= $key " = '" $value "', ";
}
$sqlInput .= " WHERE id = '" $id ."'";

if (!(
$connection = @ mysql_connect($hostname$username$password)))
    
showerror();
if (!
mysql_select_db($databasename$connection))
    
showerror();

echo 
$sqlInput "<br />";
if (!(@ 
mysql_query ($sqlInput$connection)))
    
showerror();
mysql_close($connection



and here is the output:


Code:
UPDATE cds SET title = 'Kind of Blue', artist = 'Miles Davis Quintet', year = '1956', WHERE id = '8'

Error 1064 : You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server 
version for the right syntax to use near 'WHERE id = '8'' at line 1



after doing a google search, i've run the query with and without $id single quoted, with and without the single quotes escaped. the same error persists. can't believe i'm this close and i can't make it work.

Reply With Quote
  #6  
Old December 9th, 2003, 10:32 PM
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 4 m 48 sec
Reputation Power: 8
the problem relates to how you're concatonating the comma onto the different fields...

if you notice in your SQL string, there's a comma before the WHERE clause...

One of the ways I've dealt with this in the past is by switching thigns around a bit...

PHP Code:
 $ctr 0;
$sqlInput "UPDATE cds SET "
foreach (
$_POST as $key => $value){ 
    if(
$ctr != 0) {
        
$sqlInput .= ", "
    }
    
$ctr++;
    
$sqlInput .= $key " = '" $value "'"

$sqlInput .= " WHERE id = '" $id ."'"


I haven't tested this code exactly (just typing it out on the fly), but I believe it should work.

Reply With Quote
  #7  
Old December 10th, 2003, 08:31 AM
an-ath an-ath is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 4 an-ath User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
doh, the comma! you just can't put a value on another pair of eyes looking at the code.
your solution works great. thanks again for your help, for everyone's help.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > need help with php db function


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