Jump to content

Help with a db query and if statements


flexxall

Recommended Posts

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

  }

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.