我试图将我的文章连接到不同的团队,具体取决于它包含哪些团队。目前我有以下3个表:
teams(id, name, shortname)
news(id, title, text, url)
contain(team_id, news_id)
我的问题是在跟随插入查询
之后mysqli_query($con, "INSERT INTO news (`title`, `url`, `image_url`, `news_text`, `referer_img`) VALUES ('$title', '$link', '$img', '$full_text_strip', 'test')");
我想检查变量$ full_text_strip包含其中一个团队名称或短名称,如果是,它应该在包含刚刚创建的news_id和team_id的表中创建一条新记录。最简单的方法是什么?
答案 0 :(得分:0)
这是一个未经测试的SQL解决方案:
<?php
$query = "select name, shortname, id from teams";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$ids[$row['id'][] = $row['name'];
$ids[$row['id']][] = $row['shortname'];
}
foreach($ids as $id => $teams) {
foreach($teams as $team) {
if(strpos($full_text_strip, $team) !== false) {
//mysqli_query($con, "INSERT INTO news (`title`, `url`, `image_url`, `news_text`, `referer_img`) VALUES ('$title', '$link', '$img', '$full_text_strip', 'test')");
echo 'We are inserting here because ' . $team . ' is present.';
}
}
}
?>
经过测试的代码:
<?php
$title = 'this is input blue jays for the testing';
$ids = array(1 => array('blue jay', 'blues'), 2 => array('red sox', 'reds'), 3 => array('white sox', 'whites'));
foreach($ids as $id => $teams) {
foreach($teams as $team) {
if(strpos($title, $team) !== false) {
echo 'Insert ' . $team . 'with id ' . $id;
}
}
}