-2

I'm constructing a good code to check the database and some more things, but i stopped in the line... it says:

notice: Undefined variable: getUserNew in D:\xampp\htdocs\SilverCommunity(1.6.2)\asp\class\giveaway\winner.php on line 134

My Started code:

         if(isset($_POST['giftID'])) {

            //select user from db.
            $getUserNew = getUser_ID_($db, $id_us);    

            if(!empty($getUserNew)) {

                //if empty, add new ticket.
                newGiveaway($db, $id_us);
                $hide = rand_line("../../codes.txt");
                echo $hide;

            } 
            else 
            {
                    //check dates and return
                    echo "Next step";
            }
 } 

Function Code to search the user.

            function getUser_ID_($db, $id_us)
        {
            $id_us = $_SESSION['steamid'];
            global $host; $username; $password; $dbname; 

            $sqlu = "SELECT id_steam FROM public_giveaway WHERE id='$id_us'";
            $query = mysqli_query($db, $sqlu);

            while($Qrow = mysqli_fetch_array($query))
            {

                $getUserNew = $Qrow['id_steam'];

            }

            return $getUserNew;

        }   

maybe i'm blind... but i can't find the error.. maybe is an "', or ;"... Really... i don't know...

John
  • 33
  • 4
  • If your getUser_ID_ function, you only define getUserNew if you have a db result, but you always return it. – Matt Mar 30 '17 at 20:59
  • How can this is a dupplicate if is my code??? Explain me... – John Mar 30 '17 at 20:59
  • It's a duplicate issue, the answers in these questions marked duplicates, holds the answer to your question. – Qirel Mar 30 '17 at 21:02
  • Indeed, the same error you're describing has been discussed before. Use those questions as references to help solve your issue. – cteski Mar 30 '17 at 21:02
  • hum.. is this a joke?... – John Mar 30 '17 at 21:04
  • 1
    No. Your error says "*notice: Undefined variable*", and that answer which I linked, the title clearly states the same warning. And that answer holds the solution to **your** error. – Qirel Mar 30 '17 at 21:05

1 Answers1

0

I think your problems comes form getUser_ID_, your mysql_fetch_array return 0 result then you don't go into your while then your $getUserNew is not initialized.

To avoid this problem you need to initialize your variable $getUserNew before the while statement.

like this:

function getUser_ID_($db, $id_us)
    {
        $id_us = $_SESSION['steamid'];
        global $host; $username; $password; $dbname; 

        $sqlu = "SELECT id_steam FROM public_giveaway WHERE id='$id_us'";
        $query = mysqli_query($db, $sqlu);

        $getUserNew = false;
        while($Qrow = mysqli_fetch_array($query))
        {

            $getUserNew = $Qrow['id_steam'];

        }

        return $getUserNew;

    } 
ndufreche
  • 147
  • 6
  • I try it but is not working... When i run the page the add 2 or more times the same values.. and that function is to Select the user in the DB, if exists like (if(empty), he run to "else" and runn another code... that's what i want.. – John Mar 30 '17 at 21:09
  • but yes i agreed , my problem is that function but i'm blind... so... i'm forgetting something... – John Mar 30 '17 at 21:12
  • if you add `$getUserNew = false;`, you can remove the empty function and do if ($getUserNew) – ndufreche Mar 30 '17 at 21:15
  • and empty() don't test if var is set, for that use isset() – ndufreche Mar 30 '17 at 21:27
  • humm i try use if($variable == '') and it works.. thank you. – John Mar 30 '17 at 21:30
  • better doing `if($variable === false)` if your function return false in case of you don't find your user – ndufreche Mar 30 '17 at 21:33