Jump to content


insert statement based off of select statement results??

  • Please log in to reply
2 replies to this topic

#1 ivalea

  • Members
  • PipPip
  • Member
  • 24 posts

Posted 13 October 2006 - 11:35 PM

Hi, I'm trying to grab the results of a select statement and only if the result is equal to a certain string use an insert to add to a different table.  I looks like my select is okay - but now I'm wondering how I can run the insert... here is my code:
$a = mysql_query("select * from orders_products as op, customers as c where op.orders_id = '000099' and op.products_id = '29' and c.customers_id = '14'");
while($row = mysql_fetch_assoc($a)){
$id = $row['products_model'];
$email = $row['customers_email_address'];
$custid = $row['customers_id'];
if ($id == 'CBT-0100'){
echo 'Now run the insert query using $email, $custid, and $id';
$z = mysql_query("insert into students (students_id, customers_id, students_pmodel, students_email, students_mod1, students_mod2, students_mod3, students_manual, students_cert) values ('', '$custid', '$id', '$email', '', '', '', '', ''");
echo 'something here';

Desperately need help with this - project is due today and this is the last piece that I need to lauch!  Thanks! :)

#2 fenway

  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 14 October 2006 - 05:38 PM

I'm not sure I understand what the problem is... though why not do this with an INSERT INTO... SELECT WHERE?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 ivalea

  • Members
  • PipPip
  • Member
  • 24 posts

Posted 14 October 2006 - 07:17 PM

Not sure - just that I need the "if" in there to make sure that the order contains that specific product model.

So, I first need to pull the order information and then select the model number purchased.  Only if the model number purchased is equal to a specific string, then insert into the students table. 

I'll look into doing an INSERT INTO .... SELECT though - thanks! :)

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users