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
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?

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

$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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.