如何在已具有选择查询的文件中使用插入查询?

时间:2011-03-24 09:51:05

标签: php mysql

如何使用其他查询将question_text插入数据库表中,如下面的代码所示?

mysql_connect ("localhost", "root","") or die ('Error : '.mysql_error());
mysql_select_db("keyword");
$first_word = current(explode(' ', $_POST['question_text']));

$qStuff=mysql_query("SELECT c.field_name,t.category_name, d.domain_name FROM category_fields c, taxonomy_category t, taxonomy_domain d WHERE c.category_id = t.category_id AND t.domain_id = d.domain_id AND c.field_name = '" . mysql_real_escape_string($first_word) . "'");
$num_rows = mysql_num_rows($qStuff);

// some print codes here

2 个答案:

答案 0 :(得分:0)

只需输入:

mysql_query("insert into ... values ...");

但要注意可能的mysql注入!使用mysql_real_escape_string()并查看:http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php

答案 1 :(得分:0)

此insert语句使用select查询填充field1和field2的值:

    insert into table (field1, field2)
    select field1, field2 from table2