1

i made a class connection that will handle the query also then return all the data needed from the database :

the problem is how can define a variable within the query that will handle one value that i want to insert it in the same query !!

$invoice_connection_add = new connection("
SET @myvar := select `brand_name` from `brands` where `brand_code` = $_POST[brand_code]
insert into `invoices` (`brand_code`, `brand_name`, `quantity`, `price`,`date`, `salesman`) values ('$_POST[brand_code]',@myvar, '$_POST[quantity]', '$_POST[price]', '$_POST[date]', '$_SESSION[username]');","insert_update");
4
  • hard to understand. do you want a parameterized query? or some other construct - like a subselect? Commented Dec 10, 2012 at 15:54
  • single quote missing in all $_POST Commented Dec 10, 2012 at 15:54
  • Could you not just post the brand name instead of the code? Commented Dec 10, 2012 at 15:55
  • that's what i need " $_Post['brand_name'] " , and i have to get it from another table where brand_code = $_POST['brand_code']; Commented Dec 10, 2012 at 15:58

1 Answer 1

1

Nobody else seems to be answering so I'll give it a shot - you can do something like this. You might need to play with the quotations and stuff a little bit but it'll give you an idea of what you want to do.

INSERT INTO invoices (all your columns) 
  SELECT '$_POST[brand_code]', brand_name, '$_POST[quantity]', 
  '$_POST[price]', '$_POST[date]', '$_SESSION[username]' 
  FROM `brands` 
  WHERE `brand_code` = $_POST[brand_code]
Sign up to request clarification or add additional context in comments.

4 Comments

well yes i thought about this but can i do the following :INSERT INTO invoices (all your columns) values ((select brand_name from brands where brands.brandcode = $_post[brand code]), $_post[], $post, $post ); i made this but it's taking select statement as text and insert it in the db
@user1892114 Pretty sure you dont put it in values()
there's a backtick at the end of the code. can't remove because SO insists at least 6 chars are replaced
thank you once again : ) , i spent 3 hours trying to do such tricky code but my brain is halted : )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.