Hi,
Sorry if this was asked previously-- I did not see a similar question.
I am building a product entry system with PHP5 and MySQL where a user enters any of 3 types of data and gets a result. The result then needs to be able to be inserted into another table.
It needs to flow like this:
user searches > results shown > user clicks to insert it into database
Problem:
How do I get the values from the SELECT query to populate the INSERT query when it's outside of the IF statement?
... How do I get the values into the ELSE part of the statement?
Here's some Englishized code to make it easier to understand:
// $s = what type, $d = value, $i = inserting?
// $s and $d will be set if searching, $i alone if inserting:
if(isset['s'] && isset['d'] && !isset['i']) {
$query = "SELECT * FROM products WHERE $d LIKE '%$s%' LIMIT 0,3";
mysql_query($query) or DIE("whoops");
echo the column results...
}
// now to insert the selected product into the user's inventory
else if(!isset['s'] && !isset['d'] && isset['i']) {
$query = "INSERT INTO products (products_id,scanned_usr,scanned_dte,ndc_num,control_code,rxotc_code,package_size,units_pkg,unit_dosage_flag,dosage_code,unit_of_measure,product_desc,strength,mfg_num,wac_price,arcos_flg,upc,user_active,original_package_size,original_units_pkg,package_weight) VALUES";
(select * from products where products_id = ???)
. $scanned_usr
. $scanned_dte
etc. etc. etc.
}
** I know the query is rather vulnerable to injections. I'd love some feedback on how I could improve that too, but maybe another post...
Background (if interested):
I am having clients scan inventory of drugs. First the product has to exist, then it has to match and then finally, the user will specify quantity, condition, etc. The majority of the INSERT statement will be user-submitted but the key details need to come from the SELECT query (product id, etc.)