SkyRanger Posted May 12, 2007 Share Posted May 12, 2007 I am having a heck of a time trying to insert multiple entries into same table. This is what I am trying to do: insert into table value ('".$adminname."','".$subject."','".msg.'").... Value: $adminname = Name1, Name2, Name3 etc... $subject = Any Subject Here $msg = The message a visitor types here I am not sure if I need to set $adminname as an array and do it that way or what, I am pulling what little hair I have left out trying to get this to work, so any help would be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/51018-solved-insert-multiple/ Share on other sites More sharing options...
StormTheGates Posted May 12, 2007 Share Posted May 12, 2007 I am not quite getting what you are trying to do. Like you have a bunch of users with messages and you want to insert them all 1 by 1 into the db? In that case why just not use a loop? Link to comment https://forums.phpfreaks.com/topic/51018-solved-insert-multiple/#findComment-251046 Share on other sites More sharing options...
SkyRanger Posted May 12, 2007 Author Share Posted May 12, 2007 No what I am trying to do is: insert into table: name1 subject msg name2 subject msg name3 subject msg etc Where subject and msg is the same for all entries, just the name changes Link to comment https://forums.phpfreaks.com/topic/51018-solved-insert-multiple/#findComment-251049 Share on other sites More sharing options...
StormTheGates Posted May 12, 2007 Share Posted May 12, 2007 Ah I see. Well here is what I would do $names = array("Greg", "Jim", "Bob"); $i = 0; while($i < 4){ mysql_query("INSERT INTO `table` (`name`, `subject`, `msg`) VALUES ( '$names[$i]', 'This is a subject', 'This is the message'"); $i++; } Something like that? Link to comment https://forums.phpfreaks.com/topic/51018-solved-insert-multiple/#findComment-251053 Share on other sites More sharing options...
SkyRanger Posted May 12, 2007 Author Share Posted May 12, 2007 Yeah, that is what I was thinking also. Just having a problem pulling the names out of mysql into the array select name from table where status='Admin' so $name = array($adminnames) Link to comment https://forums.phpfreaks.com/topic/51018-solved-insert-multiple/#findComment-251056 Share on other sites More sharing options...
StormTheGates Posted May 12, 2007 Share Posted May 12, 2007 Use something like this to generate an array of admin names: $admins = array(); $result = mysql_query("SELECT name FROM users WHERE status='Admin'); while($row = mysql_fetch_array($result)) { array_push($admins, $row['name']); } Link to comment https://forums.phpfreaks.com/topic/51018-solved-insert-multiple/#findComment-251063 Share on other sites More sharing options...
SkyRanger Posted May 12, 2007 Author Share Posted May 12, 2007 Ok, not sure what is going on: $admins = array(); $result = mysql_query("SELECT fname FROM users WHERE srank='Admin'"); while($row = mysql_fetch_array($result)) { array_push($admins, $row['fname']); } $sql = "INSERT INTO pms (pmid,to_name,from_name,time_sent,subject,message,opened) VALUES ('', '$admins', '$from_name', '$time_sent', '$subject', '$message', 'n')"; $result = mysql_query($sql) or die ('I could not add information to the database because ' . mysql_error()); The $admins are not inserting into the table, I know I am missing something somewhere but not exactly sure where. Tried: array_push_associative($admins); but that never worked either....(Had to try) Still learning arrays. Link to comment https://forums.phpfreaks.com/topic/51018-solved-insert-multiple/#findComment-251080 Share on other sites More sharing options...
SkyRanger Posted May 12, 2007 Author Share Posted May 12, 2007 Doh, almost got it, got 1 name to post, just have to figure out why rest are not posting: $admins = array(); $result = mysql_query("SELECT fname FROM users WHERE srank='Admin'"); while($row = mysql_fetch_array($result)) { array_push($admins, $row['fname']); } foreach ($admins as $key=>$val) { $adminnames = $val; } $sql = "INSERT INTO pms (pmid,to_name,from_name,time_sent,subject,message,opened) VALUES ('', '$adminnames', '$from_name', '$time_sent', '$subject', '$message', 'n')"; $result = mysql_query($sql) or die ('I could not add information to the database because ' . mysql_error()); Link to comment https://forums.phpfreaks.com/topic/51018-solved-insert-multiple/#findComment-251084 Share on other sites More sharing options...
SkyRanger Posted May 12, 2007 Author Share Posted May 12, 2007 ~bump~ Link to comment https://forums.phpfreaks.com/topic/51018-solved-insert-multiple/#findComment-251232 Share on other sites More sharing options...
SkyRanger Posted May 12, 2007 Author Share Posted May 12, 2007 ~bump~ Link to comment https://forums.phpfreaks.com/topic/51018-solved-insert-multiple/#findComment-251401 Share on other sites More sharing options...
taith Posted May 12, 2007 Share Posted May 12, 2007 $admins = array(); $result = mysql_query("SELECT fname FROM users WHERE srank='Admin'"); while($row = mysql_fetch_array($result)) { array_push($admins, $row['fname']); } foreach ($admins as $key=>$val){ $sql = "INSERT INTO pms (pmid,to_name,from_name,time_sent,subject,message,opened) VALUES ('', '$val', '$from_name', '$time_sent', '$subject', '$message', 'n')"; $result = mysql_query($sql) or die ('I could not add information to the database because ' . mysql_error()); } Link to comment https://forums.phpfreaks.com/topic/51018-solved-insert-multiple/#findComment-251439 Share on other sites More sharing options...
SkyRanger Posted May 12, 2007 Author Share Posted May 12, 2007 Awesome, finally it works...lol. I realized what I did wrong. Thanks taith Link to comment https://forums.phpfreaks.com/topic/51018-solved-insert-multiple/#findComment-251443 Share on other sites More sharing options...
Barand Posted May 12, 2007 Share Posted May 12, 2007 You should be able to do it with a single query <?php $from_name = 'Barand'; $subject = 'Subject goes here'; $nessage = 'Text of message'; $sql = "INSERT INTO pms (to_name,from_name,time_sent,subject,message,opened) SELECT fname, '$from_name', NOW(), '$subject', '$message', 'n' FROM users WHERE srank='Admin'"; $result = mysql_query($sql) or die ('I could not add information to the database because ' . mysql_error()); ?> Link to comment https://forums.phpfreaks.com/topic/51018-solved-insert-multiple/#findComment-251444 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.