-1
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO cinema (name, genre, year, oscar, watched) VALUES(?,?,?,?,?)";
$q = $pdo->prepare($sql);
$q->execute(array($name, $genre, $year, $oscar, $seen));
Database::disconnect();
10
  • 3
    And your question is? Commented Mar 15, 2015 at 16:19
  • 1
    @kingkero Maybe it's an easter egg, you have to find it :D Commented Mar 15, 2015 at 16:20
  • 1
    @Rizier123 Which is coming up soon, woohoo! Hop hop Commented Mar 15, 2015 at 16:21
  • "check mysql db before inserting values using pdo in php" - Query before to see if it exists. Oh, and check FOR what?. Commented Mar 15, 2015 at 16:22
  • 2
    @Rizier123 Mhm .. I am quite certain the table is badly configured (eg missing UNQIUE constraints), but the question is, what should happen after the table is queried before the INSERT. Either give an error (so the first query is useless anyway) or do an UPDATE (which has been asked&answered a ton of times as well) Commented Mar 15, 2015 at 16:23

1 Answer 1

0

Just do another query before you do the INSERT:

SELECT COUNT(*) as c FROM cinema WHERE name = ? and year = ?

Do the usual prepare/bind/fetch stuff and if ($row['c'] > 0) then you output your error message - otherwise you continue to your INSERT.

Sign up to request clarification or add additional context in comments.

1 Comment

can you plz share the prepare/bind/fetch stuff

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.