#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    8
    Rep Power
    0

    Pdo request insert problem


    can someone tell me what is problem with this application.
    am trying to send a friend request but when i click add as friend link,
    nothing is inserted into table friendship_requests and sO the request is not sent.
    please i need correction

    Code:
    <?php
    
    session_start();
    //session_regenerate_id();
    
    $db = new PDO (
    	'mysql:host=localhost;dbname=test', // dsn
    	'root', // username
    	'' // password
    );
    //Login section start
    
    
    
    
    
    if (!isset($_SESSION['logged'])) {
    
    	if (isset($_POST['username']) && isset($_POST['password'])) {
    		$statement = $db->prepare('
    			SELECT id FROM site_members
    			WHERE username = :username
    			AND password = :password
    		');
    		$statement->execute(array(
    			':username' => $_POST['username'],
    			':password' => $_POST['password']
    		));
    //if ($statement->fetchColumn()) {
    
    
    		if ($statement->rowCount()) {
    			$row = $statement->fetchColumn();
    			$_SESSION["logged"] = $row["id"];
    
    //echo $row["id"];
    
    
    			header("Location: " . $_SERVER["PHP_SELF"]);
    			/* why are you redirecting to self? Just drop-through! */
    		} 
    }else {
    			/* you should probably bomb for failed login here ! */
    echo("<form method=\"POST\">
        <input type=\"text\" name=\"username\" value=\"Type username here\">
    	<input type=\"text\" name=\"password\" value=\"Type username here\">
        <input type=\"submit\" name=\"submit\">  
        </form>");
    		}
    	}
    else {
    //end of login section
    if (isset($_GET['add'])) {
    		// make sure ID trying to be added exists
    		$statement = $db->prepare('
    			SELECT id FROM site_members
    			WHERE id = :id
    		');
    		$statement->execute(array(
    			':id' => $_GET['add']
    		));
    		/*
    			Not sure I understand this, only add a record if the currently
    			logged user has already added a user that exists? Just what
    			are you trying to do here?!?
    		*/
    		if ($statement->rowCount()) {
    			$leoJon = array(
    				':sender' => $_SESSION['logged'],
    				':recipient' => $_GET['add']
    			);
    			/*
    				I'm pulling count instead of whole records as you don't seem
    				 to be using the data for anything!
    			*/
    			$statement = $db->prepare('
    				SELECT * FROM friendship_requests
    				WHERE sender = :sender
    				AND recipient = :recipient
    			');
    			$statement->execute($leoJon);
    			if ($statement->fetchColumn()) {
    				$statement = $db->prepare('INSERT INTO friendship_requests ( sender, recipient ) VALUES ( :sender, :recipient )');
                                    //$statement = $db->prepare('INSERT INTO friendship_requests SET sender = :sender, recipient = :recipient');
    				$statement->execute($leoJon);
    			}
    		}
    	} // END
    
    //Section for exceting friendship requests
    
    	if (isset($_GET['accept'])) {
    		
    		$statement = $db->prepare('
    			SELECT * FROM friendship_requests
    			WHERE sender = :sender
    			AND recipient = :recipient
    		');
    		$statement->execute(array(
    			':sender' => $_GET['accept'],
    			':recipient' => $_SESSION['logged']
    		));
    
    		if ($statement->fetchColumn()) {
    
    
    			$selectRec = $db->prepare('
    				SELECT * FROM site_members  
    				WHERE id = :id                   
    			');
                             $selectRec->execute(array(
    				'id' => $_GET['accept']
    			));
    
    
    
    			$_row = $statement->fetchColumn();     
    			
                             $friends = unserialize($_row["friends"]);
                            $friends[] = $_SESSION['logged'];
    
    			$updateLogin = $db->prepare('
    				UPDATE site_members
    				SET friends = :friends
    				WHERE id = :id
    			');
    		
    			$updateLogin->execute(array(
    				':friends' => serialize($friends),
    				':id' => $_GET['accept']
    			));
    
    			$selectRec = $db->prepare('
    				SELECT * FROM site_members  
    				WHERE id = :id
    			');
                             $selectRec->execute(array(
    				':id' => $_SESSION['logged']
    			));
                           
                            $_row = $statement->fetchColumn();
                             $friends = unserialize( $_row["friends"]);
                            $friends[] = $_GET['accept'];
    
    			$updateLogin = $db->prepare('
    				UPDATE site_members
    				SET friends = :friends
    				WHERE id = :id
    			');
    		
    			$updateLogin->execute(array(
    				':friends' => serialize($friends),
    				':id' => $_SESSION['logged']
    			));
    
    		}
    
    		$statement = $db->prepare('
    			DELETE FROM friendship_requests
    			WHERE sender = :sender
    			AND recipient = :recipient
    		');
    		$statement->execute(array(
    			':sender' => $_GET['accept'],
    			':recipient' => $_SESSION['logged']
    		));
    	} // END
    
    //Section for showing friendship requests
    
    	$selectData = $db->prepare('
    		SELECT * FROM friendship_requests
    		WHERE recipient = :recipient
    	');
    	
    	$selectData->execute(array(
    		':recipient' => $_SESSION['logged']
    	));
    	if ($selectData->rowCount()) {
    		$selectLogin = $db->prepare('
    			SELECT * FROM site_members
    			WHERE id = :id
    		');
    		while ($row = $selectData->fetch()) {
    			$selectLogin->execute(array(
    				':id' => $row['sender']
    			));
    			while ($_row = $selectLogin->fetch()) {
    				// assuming you are echoing out something there.
    
    
    			}
    		}
    	}//END
    
    // showing result
    
    	
    	$selectLogin = $db->prepare('
    		SELECT * FROM site_members
    		WHERE id != :id
    	');
    	$selectLogin->execute(array(
    		':id' => $_SESSION['logged']
    	));
    	$userList = '';      //200
    	while ($row = $selectLogin->fetch()) {
    		$alreadyFriend = false;
    		$friends = unserialize($row['friends']);                            
    		if (isset($friends[0])) {
    			
    			foreach ($friends as $recData) {
    				if ($recData == $_SESSION["logged"]) $alreadyFriend = true;
    				
    			}
    		}
    		
         echo $row["id"];
    		
    		$selectData = $db->prepare('
    			SELECT * FROM friendship_requests
    //SELECT count(*) FROM friendship_requests
    			WHERE sender = :sender
    			AND recipient = :recipient
    		');
    		$selectData->execute(array(
    			':sender' => $_SESSION['logged'],
    			':recipient' => $row['id']
    		));
    
    if ($selectData->rowCount()) {    
    
    		//if ($selectData->countRows()) {    
    			echo " - Friendship requested.";
    		} elseif ($alreadyFriend == false) {
    			 echo " - <a href=\"" . $_SERVER["PHP_SELF"] . "?add=" . $row['id'] . "\">Add as friend</a>";
        
    		} else {
    			      echo " - Already friends.";
    		}
    		echo '<br />';
    		
    
    
    
    
    
    //END
    }
    ?>
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    1
    Rep Power
    0
    So, i can't see the problem in your code, but anyway it is incomplete. So, do you have information in your database table? Please, send me all code, until your HTML code (form)...

    Before, follow the the red lines in your code below, and tell me what it is doing..




    Code:
    <?php
    
    session_start();
    //session_regenerate_id();
    
    $db = new PDO (
    	'mysql:host=localhost;dbname=test', // dsn
    	'root', // username
    	'' // password
    );
    //Login section start
    
    
    
    
    
    if (!isset($_SESSION['logged'])) {
    
    	if (isset($_POST['username']) && isset($_POST['password'])) {
    		$statement = $db->prepare('
    			SELECT id FROM site_members
    			WHERE username = :username
    			AND password = :password
    		');
    		$statement->execute(array(
    			':username' => $_POST['username'],
    			':password' => $_POST['password']
    		));
    //if ($statement->fetchColumn()) {
    
    
    		if ($statement->rowCount()) {
    			$row = $statement->fetchColumn();
    			$_SESSION["logged"] = $row["id"];
    
    //echo $row["id"];
    
    
    			header("Location: " . $_SERVER["PHP_SELF"]);
    			/* why are you redirecting to self? Just drop-through! */
    		} 
    }else {
    
    			/* you should probably bomb for failed login here ! */
    echo("<form method=\"POST\">
        <input type=\"text\" name=\"username\" value=\"Type username here\">
    	<input type=\"text\" name=\"password\" value=\"Type username here\">
        <input type=\"submit\" name=\"submit\">  
        </form>");
    		}
    	}
    else {
    echo "okay 01<br>";
    //end of login section
    if (isset($_GET['add'])) {
    echo "okay 02<br>";
    		// make sure ID trying to be added exists
    		$statement = $db->prepare('
    			SELECT id FROM site_members
    			WHERE id = :id
    		');
    		$statement->execute(array(
    			':id' => $_GET['add']
    		));
    		/*
    			Not sure I understand this, only add a record if the currently
    			logged user has already added a user that exists? Just what
    			are you trying to do here?!?
    		*/
    		if ($statement->rowCount()) {
    echo "okay 03<br>";
    			$leoJon = array(
    				':sender' => $_SESSION['logged'],
    				':recipient' => $_GET['add']
    			);
    			/*
    				I'm pulling count instead of whole records as you don't seem
    				 to be using the data for anything!
    			*/
    echo "okay 04<br>";
    var_dump($_SESSION['logged']);
    var_dump($_GET['add']);
    
    			$statement = $db->prepare('
    				SELECT * FROM friendship_requests
    				WHERE sender = :sender
    				AND recipient = :recipient
    			');
    			$statement->execute($leoJon);
    			if ($statement->fetchColumn()) {
    				$statement = $db->prepare('INSERT INTO friendship_requests ( sender, recipient ) VALUES ( :sender, :recipient )');
                                    //$statement = $db->prepare('INSERT INTO friendship_requests SET sender = :sender, recipient = :recipient');
    				$statement->execute($leoJon);
    			}
    		}
    	} // END
    
    //Section for exceting friendship requests
    
    	if (isset($_GET['accept'])) {
    		
    		$statement = $db->prepare('
    			SELECT * FROM friendship_requests
    			WHERE sender = :sender
    			AND recipient = :recipient
    		');
    		$statement->execute(array(
    			':sender' => $_GET['accept'],
    			':recipient' => $_SESSION['logged']
    		));
    
    		if ($statement->fetchColumn()) {
    
    
    			$selectRec = $db->prepare('
    				SELECT * FROM site_members  
    				WHERE id = :id                   
    			');
                             $selectRec->execute(array(
    				'id' => $_GET['accept']
    			));
    
    
    
    			$_row = $statement->fetchColumn();     
    			
                             $friends = unserialize($_row["friends"]);
                            $friends[] = $_SESSION['logged'];
    
    			$updateLogin = $db->prepare('
    				UPDATE site_members
    				SET friends = :friends
    				WHERE id = :id
    			');
    		
    			$updateLogin->execute(array(
    				':friends' => serialize($friends),
    				':id' => $_GET['accept']
    			));
    
    			$selectRec = $db->prepare('
    				SELECT * FROM site_members  
    				WHERE id = :id
    			');
                             $selectRec->execute(array(
    				':id' => $_SESSION['logged']
    			));
                           
                            $_row = $statement->fetchColumn();
                             $friends = unserialize( $_row["friends"]);
                            $friends[] = $_GET['accept'];
    
    			$updateLogin = $db->prepare('
    				UPDATE site_members
    				SET friends = :friends
    				WHERE id = :id
    			');
    		
    			$updateLogin->execute(array(
    				':friends' => serialize($friends),
    				':id' => $_SESSION['logged']
    			));
    
    		}
    
    		$statement = $db->prepare('
    			DELETE FROM friendship_requests
    			WHERE sender = :sender
    			AND recipient = :recipient
    		');
    		$statement->execute(array(
    			':sender' => $_GET['accept'],
    			':recipient' => $_SESSION['logged']
    		));
    	} // END
    
    //Section for showing friendship requests
    
    	$selectData = $db->prepare('
    		SELECT * FROM friendship_requests
    		WHERE recipient = :recipient
    	');
    	
    	$selectData->execute(array(
    		':recipient' => $_SESSION['logged']
    	));
    	if ($selectData->rowCount()) {
    		$selectLogin = $db->prepare('
    			SELECT * FROM site_members
    			WHERE id = :id
    		');
    		while ($row = $selectData->fetch()) {
    			$selectLogin->execute(array(
    				':id' => $row['sender']
    			));
    			while ($_row = $selectLogin->fetch()) {
    				// assuming you are echoing out something there.
    
    
    			}
    		}
    	}//END
    
    // showing result
    
    	
    	$selectLogin = $db->prepare('
    		SELECT * FROM site_members
    		WHERE id != :id
    	');
    	$selectLogin->execute(array(
    		':id' => $_SESSION['logged']
    	));
    	$userList = '';      //200
    	while ($row = $selectLogin->fetch()) {
    		$alreadyFriend = false;
    		$friends = unserialize($row['friends']);                            
    		if (isset($friends[0])) {
    			
    			foreach ($friends as $recData) {
    				if ($recData == $_SESSION["logged"]) $alreadyFriend = true;
    				
    			}
    		}
    		
         echo $row["id"];
    		
    		$selectData = $db->prepare('
    			SELECT * FROM friendship_requests
    //SELECT count(*) FROM friendship_requests
    			WHERE sender = :sender
    			AND recipient = :recipient
    		');
    		$selectData->execute(array(
    			':sender' => $_SESSION['logged'],
    			':recipient' => $row['id']
    		));
    
    if ($selectData->rowCount()) {    
    
    		//if ($selectData->countRows()) {    
    			echo " - Friendship requested.";
    		} elseif ($alreadyFriend == false) {
    			 echo " - <a href=\"" . $_SERVER["PHP_SELF"] . "?add=" . $row['id'] . "\">Add as friend</a>";
        
    		} else {
    			      echo " - Already friends.";
    		}
    		echo '<br />';
    		
    
    
    
    
    
    //END
    }
    ?>
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    8
    Rep Power
    0

    pdo request insert problem


    i think the problem is that when click add as friend button,the sender and recipient id is not inserted via SQL INSERT statement and hence the request is not sent. let value be inserted into table friendship_requests and i think things will be okay.

    here is the table and you run the code on your own

    Code:
    CREATE TABLE IF NOT EXISTS `friendship_requests` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `sender` int(11) NOT NULL,
      `recipient` int(11) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
    
    
    CREATE TABLE IF NOT EXISTS `site_members` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `username` varchar(255) NOT NULL,
      `password` varchar(11) NOT NULL,
      `friends` text NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
    
    
    
    INSERT INTO `site_members` (`id`, `username`, `password`, `friends`) VALUES (1, 'test', '1234', ''),
    (2, 'bob', '1234', ''),
    (3, 'chuck', '1234', '');

IMN logo majestic logo spyfu logo threadwatch logo seochat tools logo