Jump to content

[SOLVED] Insert Multiple


SkyRanger

Recommended Posts

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

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?

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.

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());

$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());
}

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());
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.