Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在1个查询中将数据插入3个表_Php_Mysql_Select_Insert - Fatal编程技术网

Php 在1个查询中将数据插入3个表

Php 在1个查询中将数据插入3个表,php,mysql,select,insert,Php,Mysql,Select,Insert,基本上我有一个站点,它有一个类似于这个站点的标签系统,我的表结构如下 TOPICS TOPIC_TAGS TAGS topic_id tag_id tag_id topic_data topic_id tags 现在我要做的是运行一系列查询,以便在发布主题时,将相关主题数据输入主题表(当前工作),将标记放入标记表(当前工作),然后将标记表中的标记id放入带有链接主题id的主题标记表中(当前不工作) 现在,它并不像在tags表中那样将tag_ID输

基本上我有一个站点,它有一个类似于这个站点的标签系统,我的表结构如下

TOPICS      TOPIC_TAGS    TAGS
topic_id    tag_id        tag_id
topic_data  topic_id      tags
现在我要做的是运行一系列查询,以便在发布主题时,将相关主题数据输入主题表(当前工作),将标记放入标记表(当前工作),然后将标记表中的标记id放入带有链接主题id的主题标记表中(当前不工作)

现在,它并不像在tags表中那样将tag_ID输入到topic_标记中,它只是将最后的tags信息输入到行中,而不是存储相关的topic_ID

这是我的密码

$tags = isset($_POST['tags']) ? $_POST['tags'] : null;

if (is_array($tags)) {
foreach ($tags as $t) {
    // Checking duplicate
     $sql_d = "SELECT * from tags where tags='$t'"; 
      $res=mysql_query($sql_d);
      $res = mysql_num_rows($res);
    if($res<1)
    {
    // escape the $t before inserting in DB
    $sql = "INSERT INTO tags (tags) VALUES('$t')";
    mysql_query($sql);
    }
 }
} else {
echo 'Invalid tag';
}

$tag_id = isset($_POST['tag_id']) ? $_POST['tag_id'] : null;

if (is_array($tag_id)) {
foreach ($tag_id as $tid) {

    // escape the $t before inserting in DB
    $sql = "INSERT INTO topic_tags (tag_id) VALUES('$tid')";
    mysql_query($sql);
    }
 }

$sql="INSERT INTO topic_tags (tag_id)VALUES(LAST_INSERT_ID())";
$result=mysql_query($sql);


$topic_data= htmlentities($_POST['topic_data']);
$posted_by = $_SESSION['user_id'];
$posted = "date_add(now(),INTERVAL 2 HOUR)";
$invisipost = isset($_POST['invisipost']) ? $_POST['invisipost'] : 0 ;

if (($topic_data=="")) 
echo "<h2>Opps...</h2><p>You did not fill out all the required fields.</p>";

else 
$sql="INSERT INTO topics(topic_data, posted_by, posted, invisipost)VALUES('$topic_data', '$posted_by', $posted, $invisipost)";
$result=mysql_query($sql);

if($result){

$sql="INSERT INTO topic_tags (topic_id)VALUES(LAST_INSERT_ID()) WHERE topic_tags.tag_id='". $_GET['tags'] ."'";
$result=mysql_query($sql);
$tags=isset($\u POST['tags'])$_POST['tags']:空;
if(is_数组($tags)){
foreach($t标记){
//检查副本
$sql_d=“从标记中选择*,其中标记='$t';
$res=mysql\u查询($sql\u d);
$res=mysql\u num\u行($res);

如果($res我认为您应该在“foreach”循环中的一个步骤中插入
主题标签

$sql="INSERT INTO topic_tags (topic_id)VALUES(LAST_INSERT_ID()) WHERE topic_tags.tag_id='". $_GET['tags'] ."'"
既不是插入也不是更新,这肯定是错误的

(编辑) 因此,最有可能的情况是,您需要以下内容:

foreach ($tag_id as $tid) {

    // escape the $t before inserting in DB
    $sql = "INSERT INTO topic_tags (topic_id,tag_id) VALUES($topic_id,$tid)";
    mysql_query($sql);
}

我对mysql/php还是很陌生,所以我的代码可能看起来像crud,我不确定我做错了什么,希望有一些方向,谢谢你的输入。你正在处理主题和标记之间的多对多关系。更新后,我看不到代码中提取了主题id,你需要在这个上下文中使用主题id来创建ent对于topic_tags table中的每个标记,我删除了“where”部分,将最后一个关键字ID插入topic_标记,并将topic_ID放在一个单独的行中。topic_ID tag_ID 0 3 1 0“警告:为foreach($tag_ID as$tid)中的foreach()提供的参数无效{在我的公开文章中添加了额外的问题。