I’m trying to do a mysql insert through pdo. the insert works fine, but i’m in need of some if else rules and cant wrap my head around it since pdo is unfamiliar to me.
Here’s the insert code:
$qry = $dbh->prepare(
'INSERT INTO statistics (page, who, ip) VALUES (?, ?, ?)');
$qry->execute(array($pdotitle, $name, $ip));
This does what it says correctly, but i need to make a rule that selects rows from the statistics and checks if a row with the users ip already exists.
something like this:
$ip = $_SERVER['remote_addr']
$qry = $dbh->prepare(
'SELECT * FROM statistics WHERE page = :pdotitle);
$qry->execute(array($pdotitle)');
if (ip == '$ip') { /**do nothing**/ } else { /**do insert**/ }
How do I do this?
You probably thought of this:
What you should do is using SQL correctly though. Put a
UNIQUEconstraint on theipcolumn and you can do