Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
different way of "INSERT INTO"..?
Old 03-11-2009, 11:29 PM different way of "INSERT INTO"..?
Novice Talker

Posts: 6
Trades: 0
I need to be able to INSERT values into a db in the form of:

Code:
fieldx='$valuex', fieldy='$valuey'
instead of the standard:
Code:
(fieldx, fieldy) VALUES ('$valuex', '$valuey')
i swear i have done it before but cannot find anywhere that is has been done this way. does anyone know if it is possible or am i dreaming?

thanks alot!
ryan556 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-11-2009, 11:53 PM Re: different way of "INSERT INTO"..?
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I don't see anything in the mysql documentation about inserting with any syntax other than the standard form and I've never seen it done like that (doesn't mean it can't be done). Why is it that you need to be able to insert in the form you mentioned?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-12-2009, 12:10 AM Re: different way of "INSERT INTO"..?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
You can simply omit the fields list, but then you have to specify the value of every fields in the right order.
table:
Code:
create table demo(
  ikey integer not null,
  xcomment varchar(100),
  xuser varchar(100) not null,
  hpass varchar(255) not null,
  constraint pk_demo primary key (ikey)
);
and then:
Code:
insert into demo
(ikey, xcomment, xuser, hpass)
values
(1, null, 'tripy', 'ha350s');
is the same than
Code:
insert into demo
values
(1, null, 'tripy', 'ha350s');
Now, depending of your DB ,(I assume mysql here) I don't know how an auto_increment field would be handled.
Maybe you'd need to give a NULL value to respect the fields orders, I don't know.
I see that mysql allows the use of a keyword named DEFAULT, and it's probably what you'll need.
Given that ikey is with auto_increment
Code:
insert into demo
values
(DEFAULT, null, 'tripy', 'ha350s');
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 03-12-2009, 12:11 AM Re: different way of "INSERT INTO"..?
Novice Talker

Posts: 6
Trades: 0
i have a form that lasts over about 4 pages, and i have been collecting the previous pages data into a single hidden input field in the form of field='value'. so it accumulated through the form resulting in the last one having all the info in it..

im not sure of any 'easier' way to do it, but then again, this way hasnt turned out as 'easy' as i had hoped.. lol
ryan556 is offline
Reply With Quote
View Public Profile
 
Old 03-12-2009, 12:15 AM Re: different way of "INSERT INTO"..?
Novice Talker

Posts: 6
Trades: 0
thanks tripy, thats a good idea, but you got any quick ideas of a way to strip the data block (which IS in order) of field names and '=' leaving only values?

ie turn this:
Code:
field1='asdfsd',field2='qwer',firld3='yuiop'
into this:
Code:
'asdfsd','qwer','yuiop'
ryan556 is offline
Reply With Quote
View Public Profile
 
Old 03-12-2009, 12:58 AM Re: different way of "INSERT INTO"..?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
PHP Code:
<?php 
$str
="field1='asdfsd',field2='qwer',firld3='yuiop'"
$aryV=explode(',',$str); 
foreach(
$aryV as $k=>$v){ 
  list(
$f$val)=explode('=',$v); 
  print 
"The value for field $f is $val\n"
}
Output of this script:
Code:
tmo@devp4:~/$ php 1.php 
The value for field field1 is 'asdfsd'
The value for field field2 is 'qwer'
The value for field firld3 is 'yuiop'
tmo@devp4:~/$
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 03-12-2009, 01:10 AM Re: different way of "INSERT INTO"..?
Novice Talker

Posts: 6
Trades: 0
that looks good. ill give it a go now and let you know how it goes. cheers!
ryan556 is offline
Reply With Quote
View Public Profile
 
Old 03-12-2009, 02:02 AM Re: different way of "INSERT INTO"..?
Novice Talker

Posts: 6
Trades: 0
works great. thansk alot
ryan556 is offline
Reply With Quote
View Public Profile
 
Old 03-12-2009, 03:18 AM Re: different way of "INSERT INTO"..?
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
You can actually use the syntax you were first looking for when inserting. And yes, it's in the MySQL manual. You do it like this:
Code:
INSERTO INTO your_table SET
   field1='value1',
   field2='value2',
   field3='value3',
   ...
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-12-2009, 03:52 AM Re: different way of "INSERT INTO"..?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Interesting...
Not sql92 compliant, but interesting....
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to different way of "INSERT INTO"..?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.55002 seconds with 12 queries