将值插入数据库并使用Ajax和php返回另一个值

时间:2015-10-29 20:10:56

标签: php jquery mysql ajax

我试图通过ajax

将数据插入数据库

当用户登录facebook时,它将调用该函数 这将返回用户ID

我的代码

  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.id);
      document.getElementById('status').innerHTML =
        'Thanks for logging in, <b>' + response.name + '!</b><br>';


        document.getElementById('pic').innerHTML = "<img class='image-wrap' style=' width: 100px; height: 100px;' id='profileImage' src='http://graph.facebook.com/" + response.id + "/picture?width=180&height=180'>"

function callPHP(params) {
    var httpc = new XMLHttpRequest(); 
    var url = "test.php";
    httpc.open("POST", url, true); 

    httpc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    httpc.setRequestHeader("Content-Length", params.length); 

    httpc.onreadystatechange = function() { 
    if(httpc.readyState == 4 && httpc.status == 200) { 
        alert(httpc.responseText); 
        }
    }
    httpc.send(params);
}



callPHP(response.id);

  } 

test.php

<?php
// configuration
$dbtype     = "sqlite";
$dbhost     = "localhost";
$dbname     = "test";
$dbuser     = "root";
$dbpass     = "1234";

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

// new data
$link = $_GET;
$crr = rand(5, 15);

// query
$sql = "INSERT INTO user (link,crr) VALUES (:link,:crr)";
$q = $conn->prepare($sql);
$q->execute(array(':link'=>$link,
                  ':crr'=>$crr));

echo $crr;
?> 

我想将(response.id)插入数据库 并返回($ crr)

但我的空缺值 或者我得到(500(内部服务器错误))

1 个答案:

答案 0 :(得分:0)

首先 - $_GET是一个数组。将数组转换为字符串意味着您在字段中获得字符串'Array'

第二 - ':link'=>$lnik,。它是$link还是$lnik。修正你的错字。

第三 - 阅读错误日志。 始终