从数据库中删除用户

时间:2015-01-26 11:26:06

标签: php facebook

我是PHP的新手,我遇到了以下问题。用户登录时会被添加到数据库中(page2.php),但我遇到的问题是当他/她转到page3.php时将用户从数据库中删除。任何帮助,将不胜感激。非常感谢。

Page2.php

        <?php 
          echo "<p> Hello World!</p>";

        // php 5.3 and up can throw an error if this is not set  
        date_default_timezone_set("Europe/London");


        // much of the example code on the web forgets to include these HttpClients, for some reason
        require_once( '../docs/Facebook/HttpClients/FacebookHttpable.php' );
        require_once( '../docs/Facebook/HttpClients/FacebookCurl.php' );
        require_once( '../docs/Facebook/HttpClients/FacebookCurlHttpClient.php' );
        require_once( '../docs/Facebook/FacebookSession.php' );
        require_once( '../docs/Facebook/FacebookRedirectLoginHelper.php' );
        require_once( '../docs/Facebook/FacebookRequest.php' );
        require_once( '../docs/Facebook/FacebookResponse.php' );

        require_once( '../docs/Facebook/FacebookSDKException.php' );
        require_once( '../docs/Facebook/FacebookRequestException.php' );
        require_once( '../docs/Facebook/FacebookAuthorizationException.php' );
        require_once( '../docs/Facebook/GraphObject.php' );

        // This one is also often left out
        require_once( '../docs/Facebook/Entities/AccessToken.php');

        // store your $HOSTNAME, $APPID and $SECRET in this file:
        require_once( '../docs/my_app_credentials.php' );

        use Facebook\FacebookSession;
        use Facebook\FacebookRedirectLoginHelper;
        use Facebook\FacebookRequest;
        use Facebook\FacebookResponse;
        use Facebook\FacebookSDKException;
        use Facebook\FacebookRequestException;
        use Facebook\FacebookAuthorizationException;
        use Facebook\GraphObject;

        session_start();

        // init app with app id (APPID) and secret (SECRET)
        FacebookSession::setDefaultApplication($APPID,$SECRET);

        // login helper with uri
        $PAGENAME="page2.php";
        $REDIRECT_URL="http://".$HOSTNAME.'/'.$PAGENAME;

        $helper = new FacebookRedirectLoginHelper( $REDIRECT_URL );
        try {
            echo "<p> about to try to get session: the helper variable: </p>";
            $session = $helper->getSessionFromRedirect();
        //  echo "<p> the session variable:</p>";
        //  var_dump($session);
            } catch( FacebookRequestException $ex ) {
          // When Facebook returns an error
          echo "<p> There was a facebook request exception</p>";
        } catch( \Exception $ex ) {
          echo "<p> There was a validation failure</p>";
          var_dump($ex);

          // When validation fails or other local issues
        }
          echo "<p> 'get session' block now completed...</p>";

        // see if we have a session
        if ( isset( $session ) ) {
          echo "<p> Now try to get a graph node</p>";

          // graph api request for user data
          $request = new FacebookRequest( $session, 'GET', '/me' );
          $response = $request->execute();
          // get response
          $graphObject = $response->getGraphObject();

          // print data
          echo  print_r( $graphObject, 1 );


          $FBID=$graphObject->getProperty('id');
          $FIRSTNAME=$graphObject->getProperty('first_name');
          $SECONDNAME=$graphObject->getProperty('last_name');
          $TOKEN=$_GET['code'];

          //write code to check if current user is in database
          //if not add them to database
          //finally, give link to page3.php which removes them (user) from database.
          $con = mysqli_connect("localhost", "", "", "");
          $query = "SELECT FBID FROM USERS WHERE FBID='$FBID'";

          $result = $con->query($query);

          if ($result) {
          echo "Username already exists!";
        }
          else
          {

           $con->query("INSERT INTO USERS (FBID,FIRSTNAME,SECONDNAME,TOKEN) VALUES ('$FBID','$FIRSTNAME','$SECONDNAME','$TOKEN')");
            echo"user inserted";

          }


        } else {
          // show login url
          echo 'No session was set. Try logging in again by clicking here: <a href="' . "page1.php". '">Login</a>';
        }


        ?>

Page3.php

    <?php  

       $con->query("DROP FROM USERS (FBID,FIRSTNAME,SECONDNAME,TOKEN) VALUES ('$FBID','$FIRSTNAME','$SECONDNAME','$TOKEN')");

    ?>

1 个答案:

答案 0 :(得分:1)

你的SQL错了。

您希望DELETE不是DROP。例如:

DELETE FROM USERS WHERE FBID='$FBID';

删除某个字段中的某些内容会删除整行,而不仅仅是一个字段(因此用户将被删除)。

确保在page3.php中通过facebook API或$FBID呼叫$_POST

要通过php中的POST发送数据,您可以执行以下操作:

<form action='page3.php' method='post' name='page3form'>
  <?php
    echo "<input type='text' name='FBID' value='$FBID' /> 
  ?>
</form>

<script>
  document.page3form.submit();
</script>