izza56 Posted April 22, 2009 Share Posted April 22, 2009 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 More sharing options...
Zhadus Posted April 22, 2009 Share Posted April 22, 2009 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. Link to comment https://forums.phpfreaks.com/topic/155249-solved-what-is-wrong-with-my-logic/#findComment-816784 Share on other sites More sharing options...
Zhadus Posted April 22, 2009 Share Posted April 22, 2009 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"); } ?> Link to comment https://forums.phpfreaks.com/topic/155249-solved-what-is-wrong-with-my-logic/#findComment-816787 Share on other sites More sharing options...
izza56 Posted April 22, 2009 Author Share Posted April 22, 2009 Zhadus, you are an absolute star mate. Thats it sorted and I see from your explaination where the issue was. If you're ever in Dublin, theres a cold guinness with your name on it. Link to comment https://forums.phpfreaks.com/topic/155249-solved-what-is-wrong-with-my-logic/#findComment-816795 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.