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
https://forums.phpfreaks.com/topic/273410-help-with-a-db-query-and-if-statements/
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;

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.