Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
LinkBack Thread Tools Display Modes
Share |
  #1 (permalink)  
Old 07-31-2011, 02:05 PM
kontrol's Avatar
Contributing Member
Latest Blog:
None

 
Join Date: 02-10-10
Posts: 212
iTrader: 0 / 0%
php multiple insert help

i have been trying to do this multiple insert but does not get it right

PHP Code:
<?php
$sql 
"SELECT * FROM table1 WHERE id IN ('$s_id')";
$result mysql_query($sql) or die (mysql_error());
$msg "hello world";
while(
$feed mysql_fetch_array($result));
      {
      
$id[] = $feed['id'];
      
$user_id[] = $feed['user_id'];
      
$notice "INSERT INTO table2 (user_id, msg) VALUES ('$user_id, '$msg')";
      
$notify mysql_query($notice) or die (mysql_error());
      }
i want get it in such a way that
if $user_id contains 1,2,3,4 then this should be how the data should be inserted
id user_id msg
1 1 hello world
2 2 hello world
3 3 hello world
4 4 hello world
__________________
this is the best forum i have ever been to!!!!
 
Reply With Quote
  #2 (permalink)  
Old 08-01-2011, 12:14 AM
kontrol's Avatar
Contributing Member
Latest Blog:
None

 
Join Date: 02-10-10
Posts: 212
iTrader: 0 / 0%
the user_id can not be in ascending order because it could be any user that is why i made an array to fetch the users' id. that one you saw there is just a scenario
__________________
this is the best forum i have ever been to!!!!
 
Reply With Quote
  #3 (permalink)  
Old 08-08-2011, 02:38 PM
Rukbat's Avatar
Contributing Member
Latest Blog:
None

 
Join Date: 08-08-11
Location: Long Island, NY, USA
Posts: 287
iTrader: 0 / 0%
I assume you set $s_id properly somewhere before this code. Tell us what is happening, not what's not happening. "does not get it right" isn't enough to tell us what's happening. (Are you using FirePHP to see what the code is doing, line by line?)
 
Reply With Quote
  #4 (permalink)  
Old 08-11-2011, 01:18 AM
JohnnyS's Avatar
Contributing Member
 
Join Date: 07-05-11
Location: philippines
Posts: 312
iTrader: 0 / 0%
Quote:
Originally Posted by kontrol View Post
i have been trying to do this multiple insert but does not get it right

PHP Code:
<?php
$sql 
"SELECT * FROM table1 WHERE id IN ('$s_id')";
$result mysql_query($sql) or die (mysql_error());
$msg "hello world";
while(
$feed mysql_fetch_array($result));
      {
      
$id[] = $feed['id'];
      
$user_id[] = $feed['user_id'];
      
$notice "INSERT INTO table2 (user_id, msg) VALUES ('$user_id, '$msg')";
      
$notify mysql_query($notice) or die (mysql_error());
      }
i want get it in such a way that
if $user_id contains 1,2,3,4 then this should be how the data should be inserted
id user_id msg
1 1 hello world
2 2 hello world
3 3 hello world
4 4 hello world

$user_id is an array, why are you using it as a value to insert..

try to echo each of your INSERT query.. that will help you know what is happening..
 
Reply With Quote
  #5 (permalink)  
Old 08-15-2011, 10:38 AM
Contributing Member
Latest Blog:
None

 
Join Date: 08-15-11
Posts: 50
iTrader: 0 / 0%
You're on the right track; you just need to use the raw user ID coming from the first mysql_fetch_array instead of using the $user_id variable, which will create an populate an array of those user ID's.

PHP Code:
<?php 
    $sql 
"SELECT * FROM table1 WHERE id IN ('$s_id')"
    
$result mysql_query($sql) or die (mysql_error()); 
    
$msg "hello world"
    while(
$feed mysql_fetch_array($result)) { 
      
$id[] = $feed['id']; 
      
$user_id[] = $feed['user_id']; 
      
$notice "INSERT INTO table2 (user_id, msg) VALUES ('{$feed['user_id']}', '$msg')"
      
$notify mysql_query($notice) or die (mysql_error()); 
    }
?>
You don't really need the $id or $user_id variables unless you're storing them for later use.
 
Reply With Quote
  #6 (permalink)  
Old 08-15-2011, 02:26 PM
kontrol's Avatar
Contributing Member
Latest Blog:
None

 
Join Date: 02-10-10
Posts: 212
iTrader: 0 / 0%
thank you my people. i used implode function and its cool.
__________________
this is the best forum i have ever been to!!!!
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
insert tag help please prorank Coding Forum 15 05-27-2011 03:44 PM
Multiple V7N Accounts Amnesty Week (Added: DO NOT CREATE MULTIPLE IDs) Cricket V7N Forum Support 4 02-06-2010 05:18 PM
insert multiple select into mysql janim Coding Forum 1 08-05-2007 04:08 PM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 07:16 AM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2014 Escalate Media




Search Engine Optimization by vBSEO 3.6.0 RC 2 ©2011, Crawlability, Inc.