简体   繁体   中英

How to insert data from a table to another table

I am using this auto complete form, that gets the data from 1 table,

now i am using that form to insert data from its table to another table.

here is my SQL for the inserting into the table "products"

$image = addslashes(file_get_contents($_FILES['prod_pic']['tmp_name']));
$sql="INSERT INTO `inventory` (`prod_brand`,`prod_name`,`prod_category`,`prod_price`,`prod_desc`,`prod_quantity`,`prod_pic`)
VALUES 
('$_POST[prod_brand]','".mysql_real_escape_string($_POST['prod_name'])."','$_POST[prod_category]' ,'$_POST[prod_price]',
    '".mysql_real_escape_string($_POST['prod_desc'])."','$_POST[prod_quantity]','{$image}')";

the prod_category is the column i need to fill. I have data from the table named "categories" with column name "categories"

so how do i input the data from categories to the column = prod_category in the products table?

Check example and try this way...may it's help you.


INSERT INTO student (s_id, s_name, s_email)
SELECT t_id, t_name, t_email FROM teacher
WHERE teaher.tid='25';

As i understand in POST['prod_category '] contain string, like you have text input. The best way to do what you need - is change category for select in html, like this

        <select>
    foreach(categories as $category){ 

    <option value='category->id' >category1->name </option>
}
</select>

Then you will get in post category id from the table named "categories" If you dont like it your should replace '$_POST[prod_category]' from youre query to subquery

select id from categories where categories = '$_POST[prod_category]'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM