flexxall Posted January 21, 2013 Share Posted January 21, 2013 I seem to be having an issue with this script. Im pretty sure its in my if statement but i cant figure out what is wrong. Any one see anything obviously wrong ? $ip_address = $_SERVER['REMOTE_ADDR']; $user_name = $_SESSION['user_name']; $url = $_SERVER['REQUEST_URI']; $tokens = explode('/', $url); $image_id = $tokens[sizeof($tokens)-1]; //Connect to the database include 'db_connect.php'; $sql_1 = "SELECT * FROM tracking_table"; $statement = $db->prepare($sql_1); $statement->execute(array()); while($row = $statement->fetch(PDO::FETCH_ASSOC)) { $compare_id = $row['image_id']; $count = $row['count']; } if ($compare_id != $image_id) { $sql_2 = "INSERT INTO tracking_table (image_id, user_name, ip_address, count) VALUES (?, ?, ?, ?)"; $statement = $db->prepare($sql_2); $statement->execute(array($image_id, $user_name, $ip_address, 0)); } else if ($compare_id == $image_id){ $count = $count++; $sql_3 = "INSERT INTO tracking_table (count) VALUES (?)"; $statement = $db->prepare($sql_3); $statement->execute($count); } Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 21, 2013 Share Posted January 21, 2013 Read the post in my signature about asking good questions. As well as debugging SQL. Quote Link to comment Share on other sites More sharing options...
flexxall Posted January 21, 2013 Author Share Posted January 21, 2013 So if anyone needs something similar I resolved with this ... <?php session_start(); $ip_address = $_SERVER['REMOTE_ADDR']; $user_name = $_SESSION['user_name']; $url = $_SERVER['REQUEST_URI']; $tokens = explode('/', $url); $image_id = $tokens[sizeof($tokens)-1]; include 'db_connect.php'; $sql_1 = "SELECT * FROM tracking_table WHERE image_id = '$image_id'"; $statement = $db->prepare($sql_1); $statement->execute(array()); $row = $statement->fetch(); $compare_id = $row['image_id']; $count = $row['count']; if ($compare_id != $image_id) : $sql_2 = "INSERT INTO tracking_table (image_id, user_name, ip_address, count) VALUES (?, ?, ?, ?)"; $statement = $db->prepare($sql_2); $statement->execute(array($image_id, $user_name, $ip_address, 0)); elseif ($compare_id == $image_id) : $count++; $sql_3 = "UPDATE tracking_table SET count = '$count' WHERE image_id = '$compare_id'"; $statement = $db->prepare($sql_3); $statement->execute(array($sql_3)); endif; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.