SitePoint Sponsor

User Tag List

Results 1 to 24 of 24
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Struggling With 'Social Network' Script

    Hi,

    I am trying to create a script for a 'social network' system. The idea is that USER1 (follow_user_id) can follow USER 2 (user_id)

    The table is 'follow' but I cant seem to get it to work. Does anyone have any suggestions on what I can try. All I can currently do is echo the ID of the person who is logged in from the first line of code.


    Code:
    <?php 
    $id = $_SESSION['userID']; 
    if (($_GET['do'] == 'follow') && !empty($_GET['id'])) { 
             // check if user is logged in 
             if (($_SESSION['auth']) && !empty($_SESSION['current_user_id'])) { // whatever your $_SESSION variable is for logged in users 
                             if ($_SESSION['current_user_id'] == $_GET['current_user_id']) { 
                                             // other checks here to determine various ID's are numeric, etc. 
                                             $sql = "INSERT INTO `follow` (`user_id`, `follow_user_id`) VALUES (". (int)$_SESSION['current_user_id'] .", ". (int)$_GET['id'] .")"; 
                                             if (!mysql_query($sql)) { 
                                                             if (mysql_errno($link_identifier) == 1062) { //$link_identifier is necessary to avoid conflicting 
    error notices due to multiple openning/closing SQL connections 
                                                                             // duplicate attempt to follow 
                                                                             // handle accordingly 
                                                             } 
                                             } 
                             } 
             } 
    } 
    ?> 
    </div> 
    <div class="forminputcell"> 
             <div class="datainput"> 
                             <div class="forminputleft"> 
                                             Follow: 
                             </div> 
                             <div class="followbutton"> 
                                             <a href="<?php echo $_SERVER['PHP_SELF']; ?>?do=follow&id=354"><img src="/images/follow.png" class="submit-button"/></a> 
                             </div> 
             </div> 
    </div> 
    </div> 
    <?php echo $id; ?> <br> 
    <?php echo $current_user_id; ?> <br> 
     
     
    </div>

  2. #2
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    I can tell you that nested if statements are a very quick and easy way to make your code unreadable and guarantee the long term failiure of your project.

  3. #3
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    So what is the alternative method to using if statements?

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    So what is the alternative method to using if statements?
    Rather than nest the ifs, test for failure and send away (use header("Location:"))

    PHP Code:
    if (($_GET['do'] !== 'follow') || (int)$_GET['id']===) {
    // just send away
    }

    // from now on id is an integer at least, and the action is follow

    // now get on with logic. 
    Also, consider creating a function or class to handle all that session stuff. e.g.

    PHP Code:
    if(!isValidUser($_GET['current_user_id']) ){
      
    // just send away
    }

    // from now on the user can be taken as being valid 

    Now, var_dump() the variables you are testing for prior to making a conditional check, this usually turns up where you are going wrong.

  5. #5
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Well, I'd recommend using objects and taking advantage of polymorphism as a general guide to getting rid of nasty if statements.

    http://net.tutsplus.com/tutorials/ph...rphism-in-php/

    Edit: here's a google clean code talk about this very thing (replacing messy if statements with much cleaner polymorphism, as well as testing too):

    http://www.youtube.com/watch?hl=en-G...2VULWFvc&gl=GB

  6. #6
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Im struggling to understand how I can seperate out the two different id numbers.

    If I click on the profile of 350 I have the ID 350. But how do I seperate that from ID 355? Whatever I do have the ID showing as 355 if I am logged is as 355.

  7. #7
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I have made some progress with this. I can now echo all the rows I need and it no longer runs when the page is reloaded however the code no longer works when the button is pressed.

    I have tried printing some values but these dont seem to help. Does anyone have any suggestions please on what I can try to get it to work?

    Code:
    <?php
     
    	
    
     $followerid = intval($_SESSION['userID']);
            $profileid  = intval($row['id']);
    		
    
    	print_r ($followerid); 
    	print_r ($profileid);
    echo '<pre>' . print_r($_SESSION, true) . '</pre>';
    	echo print_r ($followbutton);
    		echo '<pre>' . print_r($_POST, true) . '</pre>';
    
    	
    if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){
     
    print_r ($followerid); 
    	print_r ($profileid);
    	print_r ($_POST['followbutton']);
    
    	echo print_r ($followbutton);
    		echo '<pre>' . print_r($_POST, true) . '</pre>';
    		
    		echo '<pre>' . print_r($_SESSION, true) . '</pre>';
    		
    
    	if($profileid =  $followerid) {
            $errors['profileid'] = "This is a test error.";
        }
    
    
    
    if(!$errors){
            //Validation of input vars passed, attempt to run query
            //Force vars to be ints to be safe for query statement
    
    		
    		    $followerid = intval($_SESSION['userID']);
            $profileid  = intval($row['id']);
    
            $query = "INSERT INTO `follow` (`user_id`, `follow_user_id`) VALUES ('{$profileid}', '{$followerid}')";
            $result = mysql_query($query);
    
    		
    		
            if (!$result)
            {
                    $errors[] = "Query: {$query}<br>Error: " . mysql_error();
    
            }
    	}	
    	}
    	
    
    	
    ?>
    
    
    <?php
    		print_r ($followerid); 
    	print_r ($profileid);
    	print_r ($followbutton);
    	echo print_r ($_POST['followbutton']);
    	
    
    		
    		
    			echo '<pre>' . print_r($_POST, true) . '</pre>';
    		
    	echo print_r ($followbutton);
    	
    	?>
    
           <?php if($errors['profileid']) print '<div class="invalid">' . $errors['profileid'] . ''; ?>  
    
    
    
    </div>
    <div class="followbuttonbox"> 
    
    <a href="<?php echo $_SERVER['PHP_SELF']; ?>?ID=<?php echo $profileid; ?>"><img src="/images/follow.png" id="followbutton"   /></a>
    <input type="hidden"  id="followbutton" value="true" /> 
    </div>

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){ 
    That conditional check looks a bit weak.

    Try adding this line before that condition to see what you are actually getting.
    PHP Code:
    var_dump($_POST['followbutton']); 
    If it is not the 4 char string 'true' then that condition will be passed over and the code will not be run.

  9. #9
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks mate. It echoes "NULL".

    Also the var_dump and the print below "if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){" dont display or appear to do anything. Should they be echoing on the page?


    Code:
    	print_r ($followerid); 
    	print_r ($profileid);
    echo '<pre>' . print_r($_SESSION, true) . '</pre>';
    	echo print_r ($followbutton);
    		echo '<pre>' . print_r($_POST, true) . '</pre>';
    
    	var_dump($_POST['followbutton']); 
    	
    if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){
    
    var_dump($_POST['followbutton']); 
     
    print_r ($followerid); 
    	print_r ($profileid);
    	print_r ($_POST['followbutton']);

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    It echoes NULL:

    When `followbutton` is activated or not activated?

  11. #11
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Both, when I view the page or press the button it only displays NULL.

  12. #12
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    No wonder the code never runs then!

    This is why when debugging forms, your PHP form handler should start with the lines:
    PHP Code:
    <?php
    // temp debug code you use only on your development server
    var_dump($_POST);  // or GET
    echo '<hr / '// just to separate it visually.
    ?>

    /// now get on with your logic ...
    My guess is that will turn up your error ...

    Otherwise you will have to show us the code for the form.

    Generate the form in html and then show us the resulting HTML, it sounds as though you have either made an invalid html form, or you have incorrectly typed the variable names.

  13. #13
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This is what it displays

    array(0) { }

  14. #14
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This is the code I have...


    Code:
    <?php
    // temp debug code you use only on your development server
    var_dump($_POST);  // or GET
    echo '<hr / '; // just to separate it visually.
    ?>
    
    
    <?php
     
    	
    
     $followerid = intval($_SESSION['userID']);
            $profileid  = intval($row['id']);
    		
    
    	print_r ($followerid); 
    	print_r ($profileid);
    echo '<pre>' . print_r($_SESSION, true) . '</pre>';
    	echo print_r ($followbutton);
    		echo '<pre>' . print_r($_POST, true) . '</pre>';
    
    	var_dump($_POST['followbutton']); 
    	
    if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){
    
    var_dump($_POST['followbutton']); 
     
    print_r ($followerid); 
    	print_r ($profileid);
    	print_r ($_POST['followbutton']);
    
    	echo print_r ($followbutton);
    		echo '<pre>' . print_r($_POST, true) . '</pre>';
    		
    		echo '<pre>' . print_r($_SESSION, true) . '</pre>';
    		
    
    	if($profileid =  $followerid) {
            $errors['profileid'] = "This is a test error.";
        }
    
    
    
    if(!$errors){
            //Validation of input vars passed, attempt to run query
            //Force vars to be ints to be safe for query statement
    
    		
    		    $followerid = intval($_SESSION['userID']);
            $profileid  = intval($row['id']);
    
            $query = "INSERT INTO `follow` (`user_id`, `follow_user_id`) VALUES ('{$profileid}', '{$followerid}')";
            $result = mysql_query($query);
    
    		
    		
            if (!$result)
            {
                    $errors[] = "Query: {$query}<br>Error: " . mysql_error();
    
            }
    	}	
    	}
    	
    
    	
    ?>
    
    
    <?php
    		print_r ($followerid); 
    	print_r ($profileid);
    	print_r ($followbutton);
    	echo print_r ($_POST['followbutton']);
    	
    
    		
    		
    			echo '<pre>' . print_r($_POST, true) . '</pre>';
    		
    	echo print_r ($followbutton);
    	
    	?>
    
           <?php if($errors['profileid']) print '<div class="invalid">' . $errors['profileid'] . ''; ?>  
    
    
    
    </div>
    <div class="followbuttonbox"> 
    
    <a href="<?php echo $_SERVER['PHP_SELF']; ?>?ID=<?php echo $profileid; ?>"><img src="/images/follow.png" id="followbutton"   /></a>
    <input type="hidden"  id="followbutton" value="true" /> 
    </div>

  15. #15
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    So this is your form?

    PHP Code:
    <div class="followbuttonbox"> 
    <a href="<?php echo $_SERVER['PHP_SELF']; ?>?ID=<?php echo $profileid?>"><img src="/images/follow.png" id="followbutton"   /></a>
    <input type="hidden"  id="followbutton" value="true" /> 
    </div>
    Either create a proper form and use the post method, or if you are using the GET method by creating a URL -- as you seem to be doing then this changes things a little.

    PHP Code:
    <div class="followbuttonbox"> 
    <a href="<?php echo $_SERVER['PHP_SELF']; ?>?followbutton=true&ID=<?php echo $profileid?>">
    <img src="/images/follow.png" id="followbutton"   /></a>
    </div>
    In your PHP postback form handler can now access these variables using GET rather than post.

    PHP Code:
    <?php
    // temp debug code you use only on your development server
    var_dump($_GET);  
    echo 
    '<hr / '// just to separate it visually.


    if( $_GET['followbutton'] === 'true' ){ 
    echo 
    ' do your stuff ' ;
    }

    ?>

  16. #16
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I did have the code working however the code would work whenever I viewed the page. I am now trying to make it so that it only runs when I press the button.

    What would you advise I do for this?

    This is my link profileinserttest.php?ID=355

    I am also running a query on the page as I am displaying the ID also.

    Im not sure what you mean by a proper form as I dont want a form to appear on the page. Just the button. What do you think is the best option to follow?

  17. #17
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    As I am using the form on a page with multiple code on should I use a form? The thing I dont know how it should work.

  18. #18
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Its up to you as far as I can see.

    If your button is an image inside a url string then that is fine, but you have to add to the string the variables you want.

    Else, create a small but correctly formed POST form with the hidden element - but in my experience these mini forms never behave the same on all browsers.

  19. #19
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks, so how do I add the string to the variables.

    I thought thats what I was doing with the POST = true.

    Is there a proper technique I should use?

  20. #20
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I showed you how to add it in post #15

  21. #21
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Apologies, its getting late here. Been upto long!

    I tried it but doesn't appear to work. Nothing will print inside this function, is that significant. Is there any form of error messages I can use to see what the issue is?

    Code:
    if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){

  22. #22
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    What TimeZone are you in then?

    I'm off too, far too much wine in me for this time of night/year.

  23. #23
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    As above I did try but it didnt seem to work.

  24. #24
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    731
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Does anyone have any suggestions on what I can try next please. I did have the code working but it ran whenever someone visited the page. Now I am trying to make it so that it runs whenever the button is pressed.


    Code:
    <?php
     
    	
    
     $followerid = intval($_SESSION['userID']);
            $profileid  = intval($row['id']);
    		
    
    	print_r ($followerid); 
    	print_r ($profileid);
    echo '<pre>' . print_r($_SESSION, true) . '</pre>';
    	echo print_r ($followbutton);
    		echo '<pre>' . print_r($_POST, true) . '</pre>';
    
    	var_dump($_POST['followbutton']); 
    	
    if(isset($_POST['followbutton']) && $_POST['followbutton'] == 'true'){
    
    var_dump($_POST['followbutton']); 
     
    print_r ($followerid); 
    	print_r ($profileid);
    	print_r ($_POST['followbutton']);
    
    	echo print_r ($followbutton);
    		echo '<pre>' . print_r($_POST, true) . '</pre>';
    		
    		echo '<pre>' . print_r($_SESSION, true) . '</pre>';
    		
    
    	if($profileid =  $followerid) {
            $errors['profileid'] = "This is a test error.";
        }
    
    
    
    if(!$errors){
            //Validation of input vars passed, attempt to run query
            //Force vars to be ints to be safe for query statement
    
    		
    		    $followerid = intval($_SESSION['userID']);
            $profileid  = intval($row['id']);
    
            $query = "INSERT INTO `follow` (`user_id`, `follow_user_id`) VALUES ('{$profileid}', '{$followerid}')";
            $result = mysql_query($query);
    
    		
    		
            if (!$result)
            {
                    $errors[] = "Query: {$query}<br>Error: " . mysql_error();
    
            }
    	}	
    	}
    	
    
    	
    ?>
    
    
    <?php
    		print_r ($followerid); 
    	print_r ($profileid);
    	print_r ($followbutton);
    	echo print_r ($_POST['followbutton']);
    	
    
    		
    		
    			echo '<pre>' . print_r($_POST, true) . '</pre>';
    		
    	echo print_r ($followbutton);
    	
    	?>
    
           <?php if($errors['profileid']) print '<div class="invalid">' . $errors['profileid'] . ''; ?>  
    
    
    
    </div>
    <div class="followbuttonbox"> 
    <a href="<?php echo $_SERVER['PHP_SELF']; ?>?ID=<?php echo $profileid; ?>"><img src="/images/follow.png" id="followbutton"   /></a>
    <input type="hidden"  id="followbutton" value="true" /> 
    </div>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •