Domanda

I'm a newbie to PHP and MySQL. I know there are lots of similar threads but I just can't seem to find one with my issue.

At the moment I have a table named 'movies' in my database. I have a search form to search for movies in that table. I'm using the code below to get the information from my database and display it.

$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
}

What I want to do is: If $urlid does not match 'titleid' then I want to add it to the database. At the moment I'm using the code below to add an entry:

$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);

How could I combine them so if the record does not exist it adds it and then displays the information it has added?

Thanks Lee

È stato utile?

Soluzione 3

You can insert and then select again, like this code:

$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
}

Also, I noticed that your INSERT statement may not be correct. Missing $year in values part.

Altri suggerimenti

$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
}
}

This will add it if there are no rows and otherwise show the results.

Only a supplement: If your values are same order as in the 'movies' table, then you can use the shortest way:

$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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top