Jump to content

[SOLVED] What is wrong with my logic?


izza56

Recommended Posts

Hi Folks,

 

I have a bit of php here that lists registered users on a site and you can follow (twitter like) a user.  If a user clicks 'follow' the code does a quick check to see if they are already being followed, if they are then state it, otherwise insert into the database the follow details.  However, when i try to add a user that is already being followed the message appears ok, but the code then continues to add the user to the database.  So, when you want to report on who you are following the same user appears multiple times?  Can you see why this is happening below?

 

<?php

require('dbUtils.php');

$connection = dbConnect();

 

$checkiffollow = "SELECT * FROM follow

WHERE userfollowid IN (

SELECT user.userid FROM user

WHERE username = '$followinguser')

AND userid IN (

SELECT user.userid FROM user

WHERE username = '$username')";

 

$insertfollow = "INSERT INTO follow (userid, userfollowid, followdate)

VALUES ((SELECT userid FROM user WHERE username = '$username'),

(SELECT userid FROM user WHERE username = '$followinguser'),

NOW())";

 

$result = mysql_query($checkiffollow, $connection);

$insertrow = mysql_query($insertfollow, $connection);

 

if(!$result) {

 

die("could not query database");

}

 

$count = mysql_num_rows($result);

 

if ($count != 0 ) {

echo("Sorry you are already subscribed to this user");

}

 

else {

$insertrow;

header("location:http://127.0.0.1/mysubs.php");

}

?>

 

 

 

Many thanks.

Link to comment
https://forums.phpfreaks.com/topic/155249-solved-what-is-wrong-with-my-logic/
Share on other sites

You have this line:

 

$insertrow = mysql_query($insertfollow, $connection);

 

Your logic is that it doesn't actually execute that line of code until you run the line that states

$insertrow;

 

That logic is false because it isn't a function :)

 

Where you have the $insertrow by itself on a line where it fails, execute this instead:

mysql_query($insertfollow, $connection) or die(mysql_error());

 

And remove all of the $insertrow variable lines and it should solve it.

I apologize, that was a little confusing, here is everything fixed:

 

<?php
require('dbUtils.php');
$connection = dbConnect();

$checkiffollow =    "SELECT * FROM follow
               WHERE userfollowid IN (
                  SELECT user.userid FROM user
                  WHERE username = '$followinguser')
               AND userid IN (
                  SELECT user.userid FROM user
                  WHERE username = '$username')";
                  
$insertfollow =    "INSERT INTO follow (userid, userfollowid, followdate)
               VALUES ((SELECT userid FROM user WHERE username = '$username'),
               (SELECT userid FROM user WHERE username = '$followinguser'),
               NOW())";
                  
$result = mysql_query($checkiffollow, $connection);
   
   if(!$result) {
   
      die("could not query database");
   }
   
   $count = mysql_num_rows($result);
   
   if ($count != 0 ) {
      echo("Sorry you are already subscribed to this user");
   }
   
   else {      
      mysql_query($insertfollow, $connection) or die(mysql_error());
      header("location:http://127.0.0.1/mysubs.php");
   }
?>

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.