TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 12-09-2007, 09:33 PM   #1 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default Insert data into two tables with one query

Hey,

Can you insert data into two tables using only one query? I know you can SELECT and UPDATE two tables with one query, which is why I assumed you could do it with INSERT, but upon trying and looking at sites provided by Google, I came up with nothing... which is where I come to ask you lot :P

I tried this:
PHP Code:
//Insert vote into database
    
mysql_query(sprintf("
        INSERT INTO poll_vote, poll SET
          poll_vote.poll_id = '%d',
          poll_vote.option_id = '%d',
          poll_vote.voter_id = '%d',
          poll.who_voted = '%s'"
,
            
$_POST['poll_id'],
            
$_POST['poll_choice'],
            
$_COOKIE['userID'],
            
$serial_who_voted
    
)) or die(mysql_error()); 
But naa.

Any ideas? Cheers!
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 12-09-2007, 09:58 PM   #2 (permalink)
The Gregarious
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 566
Thanks: 15
Village Idiot is on a distinguished road
Default

Just use mysqli_query and have two INSERT commands.
__________________
There are two ways to write bug-free code, only the third one works.
Village Idiot is offline  
Reply With Quote
Old 12-09-2007, 09:58 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,547
Thanks: 72
Wildhoney is on a distinguished road
Default

I don't believe you can do it for an INSERT statement, but you can put them in a TRIGGER which can then be executed using one call to it. If you were looking for an easy solution then I think you're out of luck, I'm afraid.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-10-2007, 01:53 AM   #4 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

Ah well, nevermind. Thanks anyway.

I've not read much into MySQLi, but I probably should.
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 12-10-2007, 10:19 AM   #5 (permalink)
The Contributor
 
Join Date: Apr 2005
Posts: 29
Thanks: 0
Dr John is on a distinguished road
Default

From the manual
"INSERT inserts new rows into an existing table. "
notice the use of the singular table.
__________________
www.kidneydialysis.org.uk
Dr John is offline  
Reply With Quote
Old 12-10-2007, 10:46 AM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,547
Thanks: 72
Wildhoney is on a distinguished road
Default

Yeh, try the multi insert function in MySQLi Just be careful though as it allows anyone to add some pretty nasty queries if they can somehow introduce a semi-colon into your query.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:51 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0