i am trying to make this code work with no success. Can anyone help me?
`
$to = $_POST["credits"];
$messaging = $_POST["message"];
$sub = $_POST["subject"];

@mysql_query("INSERT INTO mp_creditmail (Id, message, subject, read) ('select Id from oto_members ORDER BY RAND() Limit $to;', '$messaging', '$sub', no");`

i am trying to insert several instanses using random id's from a query that grabs them randomly limited the the amount of credits used. so if using 50 credits, will grab 50 random id's then insert the message, sub, into the database for each of the grabbed id's...

Member Avatar for LastMitch

i am trying to make this code work with no success. Can anyone help me?

What is the issue when you ran the code?

This is your query:

@mysql_query("INSERT INTO mp_creditmail (Id, message, subject, read) ('select Id from oto_members ORDER BY RAND() Limit $to;', '$messaging', '$sub', no");`

My question is very simple does your table mp_creditmail and oto_members have these columns:

    mp_creditmail (table)                  oto_members (table)

id | message | subject |read           id | message | subject |read

You didn't mention any errors nor what is your table structure so the only thing base on your query is to used (it's not tested):

INSERT INTO mp_creditmail (Id, message, subject, read) 
SELECT DISTINCT ID FROM oto_members ORDER BY RAND()

INSERT INTO mp_creditmail (Id, message, subject, read) ('select Id from oto_members ORDER BY RAND() Limit $to;', '$messaging', '$sub', no

If that is supposed to be a subquery, the syntax is wrong - it is currently trying to insert a string value 'select Id from ...'

You are also missing a closing bracket and the VALUES keyword, see MySQL Insert Syntax (also what is the "no" supposed to be at the end?)

In order to do this as a subquery it should be as follows

INSERT INTO mp_creditmail (Id, message, subject, read) VALUES ((select Id from oto_members ORDER BY RAND() Limit 1), '$messaging', '$sub', false)

AFAIK there is no way to duplicate multiple entries in a single statement like you are trying to do. You can include multiple records (VALUES sections) in a single INSERT statement, but the looping still needs to be done in PHP and not intrinsicly in SQL.

As Mitch said, please provide any specific error messages you are getting in order to get more direct advice.

thanks guys, but i solved my problem by doing a clean random grab, and insert, then looping both queries equal to the amount of credits used.....

i appreciate your help though...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.