Check and Insert Data in a Table

Asked

Viewed 188 times

0

Good evening, I’m implementing a very basic Facebook login system on my site, the system should capture the basic data (id, name and email) of the user and save in a table and that’s where I’m having difficulties, I already ran many forums on the internet and tested various codes, but nothing is working.


UPDATING


"I solved" the problem by inserting Function.php data into fbconfig.php, probably the code should be very heavy, but for now it worked:

fbcongig.php

<?php
session_start();
// added in v4.0.0
require_once 'autoload.php';
require 'dbconfig.php'; // Conexão com o Banco

...

    /* ---- Session Variables -----*/
        $_SESSION['FBID'] = $fbid;           
      $_SESSION['FULLNAME'] = $fbfullname;
        $_SESSION['EMAIL'] =  $femail;      
    /* ---- header location after session ----*/

            if ($fbid>"1"){

                $consulta = "SELECT Fuid FROM Users WHERE Fuid='".$fbid."'";
                $check = mysqli_query($con,$consulta);
                $numeros = mysqli_num_rows ($check);

                if ($numeros <= 0){
                    $query = "INSERT INTO Users (Fuid,Ffname,Femail) VALUES";
                    $query .= "('".$fbid."','".$fbfullname."','".$femail."')";
                    $inserir = mysqli_query($con,$query) or die(mysql_error($con));
                }
            }
    //checkuser($fbid,$fbfullname,$femail);
  header("Location: index.php");
} else {
  $loginUrl = $helper->getLoginUrl(array('scope' => 'email, public_profile,user_friends'));
 header("Location: ".$loginUrl);
}
?>
  • Are you sure you should use exit in function checkuser when you find a record in the table? And why the function mysqli_query, in instruction insert does not have the reference to the connection with the bank $con? Moreover, the function parameters mysqli_query not in order connection, query, guy mysqli_query($con, $query)?

  • No, I’m still studying about... Exit() was the best I had thought and I already understood that it was not such a good idea, I’m modifying the code, but still with a lot of difficulty

  • You can do if ($numeros <= 0) { ...} and eliminate the else, since if the condition by positive, no code is executed.

  • This solved the duplicate problem, but the data is not being entered in the table, it is returning error in this stretch $sql = "INSERT INTO Users ('Fuid','Ffname','Femail') VALUES ('".$fbid."','".$fbfullname."','".$femail."')";&#xA;mysqli_query($con,$sql) or die('Erro ao Cadastrar');

  • And what’s the mistake?

  • Fixing, the error is not in the code I listed, the error is this one: [03-Feb-2017 01:47:42 UTC] PHP Notice: Undefined index: FBID in /home/profissa/public_html/teste/index.php on line 12

  • To be more Xto, line 12 is now if ($numbers <= 0){`

  • Undefined index references some array...

  • Note that the error is happening on line 12 of index.php, not of function.php. See what’s on that line.

  • In the index.php file line 12 has this code <?php if ($_SESSION['FBID']): ?> That just receives the variable and displays on the screen, I’m updating the question with all the code. When I try to login with an account already inserted in the bd, the information usually appears on the screen, but if I use an account that is not in the bd, it returns the "Error when Registering" message in the browser. Since fbconfig.php redirects to index,php, the registration error might not be sending the data to the index, generating the Undefined error?

  • Try to do on this line if (isset($_SESSION["FBID"]))

  • I tried the isset here, the error remains the Undefined above. As I said, when I log in using an account that is already in the comic book, everything works correctly: The data is displayed on the screen and is not being duplicated on the server, the error is only appearing when I try to log in with an account that is not in the comic book... "Error while Registering" (and the url is in "fbconfig.php?code=Aqalflyywz..."), in the system log appears the same error as Undefined

  • I updated the above question, found a "solution", but still don’t know what could be causing the error. Now I’ll try to find a way to better fix that code

  • Edit your question and post only the current codes, it is already very messy.

  • I edited the question, I believe that, for now, the problem is solved , I’m studying on how to optimize this code.

Show 10 more comments

1 answer

0


Your connection is outside the scope of the function. That’s why the first Notice.

Notice: Undefined variable: con in /home/profissa/public_html/test/functions.php on line 8

Place the line below inside the function.

 $con = mysqli_connect('xxxxxxx','xxxxxx','xxxxxxx','xxxxxxx')
 or die('Não foi possível conectar');

OR you leave the connection where this and may (not very recommended) use the global keyword by placing this code within the function:

global $con;

On the scope of variables: http://php.net/manual/en/language.variables.scope.php

Take a look at the mysqli_query syntax as well http://php.net/manual/en/mysqli.query.php , a parameter is missing before the query string:

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

Change the lines

mysqli_query("SELECT Fuid FROM Users WHERE Fuid='".$fbid."'"); 

for

mysqli_query($con,"SELECT Fuid FROM Users WHERE Fuid='".$fbid."'"); 

and

mysqli_query($sql,$con)

for

mysqli_query($con, $sql)
  • The tip and the article regarding the scope were quite useful! I made some changes, but I haven’t had success yet, I’m having difficulties in mysqli_query!

  • See changes I made to the answer

  • I made these changes a few more suggestions from Anderson in the comments above, but I’m still having problems, as if the variable $fbid was not receiving the data. I updated my initial question with the new code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.