插入Mysql数据库时,Facebook用户数据为空

时间:2011-05-26 15:17:59

标签: php mysql facebook

我目前正在IFrame中开发一个php facebook画布。该创意是供用户使用Facebook提供的用户数据从api上传图片到mySql数据库。

一切顺利,直到我必须将Userdata与图片一起保存。我推送提交后,图片上传。它重新加载页面并显示图片,但来自facebook的用户数据不会随之插入。我在互联网上尝试了很多选项,但它并不关心..它不会随着图片一起用户数据。

我有一个索引文件,如下所示:

        <?php 
    setcookie("fag");
    session_start();
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link type="text/css" rel="stylesheet" href="css/style.css" />
    <title>Facebook</title>
    <?php
    define('MyConst', TRUE);
    include_once 'ClassLibrary/User_AR.php';
    include_once 'ClassLibrary/FacebookApi.php';
    include_once 'ClassLibrary/ImageUpload.php';
    $sr = new User_AR();
    $fb = new FacebookApi();
    $iu = new ImageUpload();
                $info = $fb->getUserInfo();
                $_COOKIE["fag"] = $info;
                    foreach ($info as $key => $v )
            {
            switch ($key)
            {
            case "ID":
                echo $v;
                break;
            case "Name":

                echo $v;
                break;
            case "Email":
                echo    $v;
                break;
            case "Img":
                echo $v;

                break;
            }
            }
    ?>

    </head>

    <body>

    <?php 

             if (isset($_POST["sendPhoto"])) {
                echo "Is Set";

                if ($sr->exists($info["ID"]) == false) {
             $iu->processForm();
                }else{
                $iu->displayForm();
                }
            }
            else
            {
                $iu->displayForm();
                echo "Is not Set";
            }
    $arr = array($sr->getAllUsers());
     for($i = 0; $i < count($arr); $i++){
    foreach ($arr[$i] as $key => $value) {
            echo "<div class=".'"'."userBox".'"'.">"."<div class=".'"'."innerBox".'"'.">
            <div class=".'"'."Title".'"'.">Navn :</div><div class=".'"'."Values".'"'.">".$value["Name"]."</div>".
            "<div class=".'"'."Dato".'"'.">Dato :</div><div class=".'"'."Values".'"'.">".date("m.d.y")."</div>".
            "<div class=".'"'."Title".'"'.">Billede :</div><div class=".'"'."img".'"'."><img alt=".'"'."BrugerBillede".'"'." height=".'"'."150".'"'." width=".'"'."150".'"'." class=".'"'."Image".'"'."src=".'"'.$value["Img"].'"'."/></div></div></div>";
         }
     }

    ?>



    </body>
    </html>

请注意,此代码是沙盒代码,可能包含一些奇怪的单词,这些单词是疲惫的开发人员无法使其工作的结果!

无论如何,这应该显示uploadform并允许你上传文件,如果你在mysql db中注册,它就不会向你显示表单。

现在这是ImageUpload文件,其中包含获取信息的上传图片的代码。     

function __construct()
{       
        $killer = $_COOKIE["fag"];

            foreach ($killer as $key => $v )
            {
        switch ($key)
        {
        case "ID":
            $id = $v;
            echo $id;
            break;
        case "Name":
            $name = $v;
            echo $name;
            break;
        case "Email":
            $email = $v;
            echo $email;
            break;
        case "Img":
            $img = $v;
            echo $img;
            break;
            }

        }

}

    function displayForm() {

    echo "<form action=".'"'.$this->processForm().'"'. "method=".'"'."post".'"'."enctype=".'"'."multipart/form-data".'"'.">
<div>

<input type=".'"'."hidden".'"'. "name=".'"'."MAX_FILE_SIZE".'"'."value=".'"'."100000".'"'."/>
<label for=".'"'."userfile".'"'.">Upload file:</label>
<input type=".'"'."file".'"'."name=".'"'."photo".'"'. "id=".'"'."photo".'"'."/>
<input type=".'"'."submit".'"'. "name=".'"'."sendPhoto".'"'."value=".'"'."Send Photo".'"'."/>

</div>
</form>";
    }
function processForm(){
    $se = new User_AR();
            if (isset($_FILES["photo"]) && $_FILES["photo"]["error"] == UPLOAD_ERR_OK){

                if ($_FILES["photo"]["type"] !="image/jpeg") {
                        echo "<p>Kun JPG or PNG </p>";
                        $this->bool = false;
                }
                else if(!move_uploaded_file($_FILES["photo"]["tmp_name"], "images/".basename($_FILES["photo"]["name"])))
                {
                    //echo "<p>Im sorry there was a problem uploading that photo</p>".$_FILES["photo"]["error"];
                }
                else 
                {



                $se->InsertPict($this->name,$this->email,"images/".basename($_FILES["photo"]["name"]),$this->id);           
                $this->displayThanks();         

                }

            }else{

            echo $_FILES["photo"]["error"];
            }



/*              if (isset($_FILES["photo"])&& $_FILES["photo"]["error"] == UPLOAD_ERR_OK) {
            if ($_FILES["photo"]["type"] !="image/jpeg") {
                echo "<p>Kun JPG </p>";

            }else if (!move_uploaded_file($_FILES["photo"]["tmp_name"], "images/".basename($_FILES["photo"]["name"])))
            {
                echo "<p>Im sorry there was a problem uploading that photo</p>".$_FILES["photo"]["error"];

            }else{

                }
        }else 
        {
            switch ($_FILES["photo"]["error"]) {
                case UPLOAD_ERR_INI_SIZE:
                    $message = "The photo is larger than server allows";
                    break;
                case UPLOAD_ERR_FORM_SIZE:  
                    $message = "The photo is larger  than the script allows.";
                break;
                case UPLOAD_ERR_NO_FILE:
                    $message = "No file was uploaded. Make sure you choose a file to upload.";

                default:
                    $message = "Please contact your server administrator for help";
                break;
            }
    echo "FEST          ". $killer["Name"];
        }*/
            function getitman() {


            }

    }

    function displayThanks() {
        echo "<p><img src=".'"'."images/".$_FILES["photo"]["name"].'"'." alt=".'"'."Photo".'"'."/> Thanks!</p>";
    }

    function __destruct() {

    }
}

?>

现在这是完全奇怪和疯狂的地方。这行代码它不会做我告诉它做的事情(只保存图像)

$se->InsertPict($this->name,$this->email,"images/".basename($_FILES["photo"]["name"]),$this->id);

当把它if / else构造时,它只会保存我的图像而不是userdata

function processForm(){
    $se = new User_AR();
            if (isset($_FILES["photo"]) && $_FILES["photo"]["error"] == UPLOAD_ERR_OK){

                if ($_FILES["photo"]["type"] !="image/jpeg") {
                        echo "<p>Kun JPG or PNG </p>";
                        $this->bool = false;
                }
                else if(!move_uploaded_file($_FILES["photo"]["tmp_name"], "images/".basename($_FILES["photo"]["name"])))
                {
                    //echo "<p>Im sorry there was a problem uploading that photo</p>".$_FILES["photo"]["error"];
                }
                else 
                {
                $se->InsertPict($this->name,$this->email,"images/".basename($_FILES["photo"]["name"]),$this->id);           
                $this->displayThanks();         

                }

            }else{

            echo $_FILES["photo"]["error"];
            }





}

但是只要我把它放在if / else构造之外它就会保存所有信息..但不是图像,只要我连接到facebook ap / canvas页面就会这样做。这是不可取的(因为用户必须上传图片)。但是我很困惑,并且因为这个疯狂的代码而快要疯狂了!

这是User_AR php文件:

<?php
include_once 'ClassLibrary/DbConn.php';
include_once 'ClassLibrary/FacebookApi.php';

class User_AR  {

    function getAllUsers() {
    $exp = array();
    $con = new DbConn();
    $result =  $con->command("select * FROM User");
    while (($row = mysqli_fetch_array($result))!=false) {
        $exp[] = array("ID" => $row["UserID"],"Name" => $row["UserName"],"Email" =>$row["UserEmail"],"Img" => $row["UserImg"],"FaceID" => $row["UserFaceID"]);
}
    return $exp;
    }

public  function InsertPict($Name,$Email,$Img,$ID) {
        $con = new DbConn();
        if (mysqli_num_rows($con->command("SELECT * FROM User WHERE UserFaceID =".$ID.""))== 0) {
            $res = $con->command("INSERT INTO User(UserName, UserEmail,UserImg, UserFaceID) values('$Name','$Email','$Img','$ID')");
        echo    $message = "Dit billede er nu lagt op og du deltager i konkurrencen";
        }else 
        {
        echo    $message = "Du har allerede indsendt et billede og deltager i konkurrencen";

        }

    }
    function exists($faceID) {
        $fb = new FacebookApi();
        $gg = $fb->getUserInfo();

//$game  = count_chars($_COOKIE["fag"]["Name"],3);
///     echo $game.$gg["Name"];
        $con = new DbConn();
        if (mysqli_num_rows($con->command("SELECT * FROM User WHERE UserFaceID = $faceID")) == 0) {
            $var = false;

            return $var;
        }else 
        {
            $var = true;
            return $var;
        }
    }


}

?>

我知道这是可行的,因为当我在ImageUpload类的if / else之外移动函数“InsertPict”时,它可以很好地工作。

因为我一直都是为了永恒而且可能对明显的错误或其他类似的人视而不见。在我完全坚果并因吃键盘而被解雇之前,请帮助我。我很乐意为任何帮助提供帮助。

事先感谢!

1 个答案:

答案 0 :(得分:0)

我最终重写了整个代码并解决了问题。而不是使用facebook dev上给出的示例代码。页面我使用了php基本代码文件,并且工作正常。很抱歉给您带来不便。