Jump to content

PHP not inserting to Database


jalexsmith

Recommended Posts

Hi guys.....

 

I'm having a problem that I've been tearing my hair out over for way too long. I'm hoping that some of you may be able to help me or at least point me in the right direction.

 

I'm using MySQL 5.0.91 (via PHPmyAdmin from GoDaddy).

 

This query should basically grab the data from the Facebook API and insert it into the database. It's not doing that. After inserting the data, it queries the database, and redirects the user to their profile page (USERNAME.php). Because there is no data in the database for that user, it has no idea what their username is, so it redirects them to ".php"....yes, that is "[dot]php".

 

I'm not entirely sure how to debug this either. I've tried a few var_dump()'s with not much luck.

 

Is there a specific order in which this stuff needs to be in? Maybe that's the problem, so I'm also including a screenshot from PHPmyAdmin of the database table.

 

Here's the code:

 

// user not in db, insert details	
	if(!empty($user)){
        	$apiGet = array(
        		'method' => 'users.getinfo',
               		'uids' => $uid,
               		'fields' => 'uid, name, first_name, last_name, pic_square, pic_big, sex, email, birthday_date, activities, interests, status, about_me' //theses are the fields it pulls from the Facebook API
                	);                                                                                                            
        		// create array to hold returned values
        		$fbi = $facebook->api($apiGet);
		// insert details
		$iString = "oauth_provider, oauth_uid, username, name, first_name, last_name, sex, pic_big, email, joined, lastLogon, birthday_date, user_activities, user_interests, user_status, user_about_me";

		$iArray = array(); //the following are the values of the fields
		array_push($iArray,'facebook');
		array_push($iArray,$user['id']);
		array_push($iArray,$user['name']);
		array_push($iArray,$fbi[0]['name']);
		array_push($iArray,$fbi[0]['first_name']);
		array_push($iArray,$fbi[0]['last_name']);
		array_push($iArray,$fbi[0]['sex']);
		array_push($iArray,$fbi[0]['pic_big']);
		array_push($iArray,$fbi[0]['email']);
		array_push($iArray,time());
		array_push($iArray,time());
		array_push($iArray,$fbi[0]['birthday_date']);
		array_push($iArray,$fbi[0]['user_activities']);
		array_push($iArray,$fbi[0]['user_interests']);
		array_push($iArray,$fbi[0]['user_status']);
		array_push($iArray,$fbi[0]['user_about_me']);
		var_dump($email);
		$db->insert('users',$iArray,$iString);
		$where = "oauth_uid = '{$uid}'";
		$db->select('*','users',$where);
		$result = $db->getResult();					
                  // the next line creates a profile page. After that, there are lines that point the user to that page.
                  createProfile($result['username']);

 

I've added some comments to it to articulate what is going on.

 

Thanks in advance!

Alex

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/217674-php-not-inserting-to-database/
Share on other sites

You gave us just a small snippet of code, so there's not much help we can give.  You have a bunch of functions/classes that are unexplained, and variables like $user. 

 

Starting with that, what does $user contain prior to your $fbi = $facebook->api($apiGet)?

 

After that call, what does $fbi contain?

 

Then you do:  $db->insert('users',$iArray,$iString)

 

Apparently you have some wrapper class.  What does $iArray and $iString contain prior to this method call.  Is this method successful or are there errors generated, and how would you know it there were?

 

 

 

 

Thanks for the reply! Too much to just explain though. I'll just paste the full page of code here:

<?php
ini_set("display_errors","1");
ERROR_REPORTING(E_ALL);

ob_start();
session_start();

// Include required files.
require "includes/config.php";
require "includes/database.php";
require "includes/facebook.php";
require "includes/language.php";

// init database object
$db = new database();
$db->connect();

// Create Facebook connect object
$facebook = new Facebook(array(
    'appId'  => $fb_app_id,
    'secret' => $fb_app_secret,
    'cookie' => true
));
// Check for an active session
$session = $facebook->getSession();
// If a session exists, proceed to check details against database
if(!empty($session)) {
        try{
                $uid = $facebook->getUser();
                $user = $facebook->api('/me');
        } catch (Exception $e){}
// check to see if user is in the database
$where = "oauth_uid = '{$uid}'";
$db->select('*','users',$where);
$rows = $db->getRows();
$result = $db->getResult();
if($rows == 0) {
	// user not in db, insert details	
	if(!empty($user)){
        	$apiGet = array(
        		'method' => 'users.getinfo',
               		'uids' => $uid,
               		'fields' => 'uid, name, first_name, last_name, pic_square, pic_big, sex, email, birthday_date, activities, interests, status, about_me'
                	);                                                                                                            
        		// create array to hold returned values
        		$fbi = $facebook->api($apiGet);
		// insert details
		$iString = "oauth_provider, oauth_uid, username, name, first_name, last_name, sex, pic_big, email, joined, lastLogon, birthday_date, user_activities, user_interests, user_status, user_about_me";

		$iArray = array();
		array_push($iArray,'facebook');
		array_push($iArray,$user['id']);
		array_push($iArray,$user['name']);
		array_push($iArray,$fbi[0]['name']);
		array_push($iArray,$fbi[0]['first_name']);
		array_push($iArray,$fbi[0]['last_name']);
		array_push($iArray,$fbi[0]['sex']);
		array_push($iArray,$fbi[0]['pic_big']);
		array_push($iArray,$fbi[0]['email']);
		array_push($iArray,time());
		array_push($iArray,time());
		array_push($iArray,$fbi[0]['birthday_date']);
		array_push($iArray,$fbi[0]['user_activities']);
		array_push($iArray,$fbi[0]['user_interests']);
		array_push($iArray,$fbi[0]['user_status']);
		array_push($iArray,$fbi[0]['user_about_me']);
		var_dump($email);
		$db->insert('users',$iArray,$iString);
		$where = "oauth_uid = '{$uid}'";
		$db->select('*','users',$where);
		$result = $db->getResult();					
                  // create profile page
                  createProfile($result['username']);

		// set session vars
		validate_user($result['username'],$result['userType'],$result['id']);

		// divert to profile
		header('location: users/'.$result['username'].'.php');
	}

        } else {
		validate_user($user['name'],$result['userType'],$result['id']);
                    // user exists, redirect to profile page
                      header('location: users/'.$user['name'].'.php');

        }

}
ob_flush();
?>

 

Thanks again, guys!

 

The kind of error checking I'm talking about is like this:

 

        		// create array to hold returned values
        		$fbi = $facebook->api($apiGet);
                        if ($fbi === false) {
                                trigger_error("Facebook api fetch failed!", E_USER_ERROR);
                        }

 

I don't know if that code is correct though as I don't know what $facebook->api() returns when it fails.  Usually "false" means failure.

I'm with btherl about beefing up your error checking.  In terms of debugging, put an echo or var_dump() for every variable that gets filled by a function call, so that you are sure of what the parameters are before the function is called, and then what the return value contains. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.