我是PHP和MySQL的新手。我知道有很多类似的主题,但我似乎无法找到一个问题。
目前我有一张名为'电影'在我的数据库中。我有一个搜索表单来搜索该表中的电影。我使用下面的代码从我的数据库中获取信息并显示它。
$query = "SELECT * FROM movies WHERE titleid='$urlid'";
$result = $con->query($query);
if( !$result ) {
die('Query failed!<br>'.$con->error);
}
if( $result->num_rows == 0 ) {
}
while( $row = $result->fetch_assoc() ) {
echo $row['id'];
echo $row['title']; //this is not all of the columns just an example
}
我想做的是: 如果$ urlid与titleid&#39;不匹配然后我想将它添加到数据库中。目前我正在使用以下代码添加条目:
$query = "INSERT IGNORE INTO movies (id, aka, ratio, budget, cast, fullcast, linkcast, castcharacter, linkcastcharacter, company, linkcompany, description, directors, linkdirectors, genre, mpaa, synopsis, poster, rating, releasedate, runtime, sound, officialsites, tagline, title, trailerlink, titleid, year)
VALUES (NULL, '$aka', '$ratio', '$budget', '$cast', '$fullcast', '$linkcast', '$castcharacter', '$linkcastcharacter', '$company', '$linkcompany', '$description', '$directors', '$linkdirectors', '$genre', '$mpaa', '$synopsis', '$poster', '$rating', '$releasedate', '$runtime', '$sound', '$officialsites', '$tagline', '$title', '$trailerlink', '$urlid')";
mysqli_query($con, $query);
如何将它们组合起来如果记录不存在则添加它然后显示它添加的信息?
由于 利
答案 0 :(得分:0)
$query = "SELECT * FROM movies WHERE titleid='$urlid'";
$result = $con->query($query);
if( !$result ) {
die('Query failed!<br>'.$con->error);
}
if( $result->num_rows == 0 ) {
$addQuery = "*your query here*";
$con->query($query);
echo "Movie added."
} else {
while( $row = $result->fetch_assoc() ) {
echo $row['id'];
echo $row['title']; //this is not all of the columns just an example
}
}
如果没有行,则会添加它,否则显示结果。
答案 1 :(得分:0)
仅补充: 如果您的值与“电影”表中的值相同,那么您可以使用最短的方式:
$query = "INSERT IGNORE INTO movies VALUES (
NULL, '$aka', '$ratio', '$budget', '$cast', '$fullcast', '$linkcast', '$castcharacter', '$linkcastcharacter', '$company',
'$linkcompany', '$description', '$directors', '$linkdirectors', '$genre', '$mpaa', '$synopsis', '$poster', '$rating',
'$releasedate', '$runtime', '$sound', '$officialsites', '$tagline', '$title', '$trailerlink', '$urlid')";
mysqli_query($con, $query);
答案 2 :(得分:-1)
您可以插入然后再次选择,如下代码:
$query = "SELECT * FROM movies WHERE titleid='$urlid'";
$result = $con->query($query);
if( !$result ) {
die('Query failed!<br>'.$con->error);
}
if( $result->num_rows == 0 ) {
$insert_query = "INSERT IGNORE INTO movies (id, aka, ratio, budget, cast, fullcast, linkcast, castcharacter, linkcastcharacter, company, linkcompany, description, directors, linkdirectors, genre, mpaa, synopsis, poster, rating, releasedate, runtime, sound, officialsites, tagline, title, trailerlink, titleid, year)
VALUES (NULL, '$aka', '$ratio', '$budget', '$cast', '$fullcast', '$linkcast', '$castcharacter', '$linkcastcharacter', '$company', '$linkcompany', '$description', '$directors', '$linkdirectors', '$genre', '$mpaa', '$synopsis', '$poster', '$rating', '$releasedate', '$runtime', '$sound', '$officialsites', '$tagline', '$title', '$trailerlink', '$urlid')";
mysqli_query($con, $insert_query);
$result = $con->query($query);
if( !$result ) {
die('Query failed!<br>'.$con->error);
}
}
while( $row = $result->fetch_assoc() ) {
echo $row['id'];
echo $row['title']; //this is not all of the columns just an example
}
另外,我注意到您的INSERT语句可能不正确。缺少$ year的价值部分。