1

I am working on an android app that should create some records on a MySQL database.

This is the PHP file that receives the POST values from the app. As you may see, there are three arrays that collect specific POST. The first and second loops are working fine and executing the related guardar_post_media() function.

But the third loop is not executed and there are real values on the variables inside the third array.

May be there is something wrong that I can´t detect, may be you can.

<?php
        require_once '../mis_php_functions/funciones_basicas.php';

        if($_SERVER['REQUEST_METHOD']=='POST'){

            $val39 = $_POST['val39']; 
            $val40 = $_POST['val40']; 
            $val46 = $_POST['val46']; 
            $val48 = $_POST['val48']; 
            $val50 = $_POST['val50']; 
            $val52 = $_POST['val52']; 
            $val54 = $_POST['val54']; 
            $val56 = $_POST['val56']; 
            $val58 = $_POST['val58']; 
            $val60 = $_POST['val60']; 
            $val62 = $_POST['val62']; 
            $val64 = $_POST['val64']; 
            $val65 = $_POST['val65']; 
            $val67 = $_POST['val67']; 
            $val69 = $_POST['val69']; 
            $val71 = $_POST['val71'];
            $val73 = $_POST['val73'];
            $val75 = $_POST['val75'];
            $val77 = $_POST['val77'];
            $val79 = $_POST['val79'];
            $val81 = $_POST['val81'];
            $val82 = $_POST['val82'];
            $val83 = $_POST['val83'];
            $val84 = $_POST['val84'];
            $val85 = $_POST['val85'];
            $val86 = $_POST['val86'];
            $val87 = $_POST['val87'];
            $val88 = $_POST['val88'];
            $val89 = $_POST['val89'];
            $val100 = $_POST['val100'];
            $val101 = $_POST['val101'];
            $val102 = $_POST['val102'];
            $val103 = $_POST['val103'];
            $val104 = $_POST['val104'];
            $status = 1;



             $post = guardar_post($val40,$val39,$val100,$val102,$status,$val103);

     if ($post != false) {


            $fotos = array($val48,$val50,$val52,$val54,$val56,$val58,$val60,$val62,$val64);


        $arrayLength = count($fotos);
        echo "Numero de fotos ".$arrayLength;
        $i = 0;
        while ($i < $arrayLength)
        {


            if ($fotos[$i] == 0){

            }
            else{
                guardar_post_media(1,$fotos[$i],$val102,$val100,$post);
            }


            echo "<br />".$fotos[$i] ."<br />";
            $i++;
        }

   $videos = array($val67,$val69,$val71,$val73,$val75,$val77,$val79,$val81,$val83);


        $arrayLength2 = count($videos);
        echo "Numero de videos ".$arrayLength2;
        $i = 0;
        while ($i < $arrayLength2)
        {


            if ($videos[$i] == 0){

            }
            else{

                guardar_post_media(2,$videos[$i],$val102,$val100,$post);
            }


            echo "<br />".$videos[$i] ."<br />";
            $i++;
        }

   $youtube = array($val85,$val86,$val87,$val88,$val89);


        $arrayLength3 = count($youtube);
        echo "Numero de youtube ".$arrayLength3;
        $i = 0;
        while ($i < $arrayLength3)
        {


            if ($youtube[$i] == 0){

            }
            else{

                guardar_post_media(3,$youtube[$i],$val102,$val100,$post);
            }


            echo "<br />".$youtube[$i] ."<br />";
            $i++;
        }



            sendMessageNuevoPost($val39,$val102,$val103,$val104); // envio de push





        }
        else{
            echo 'error';
        }
    }

    ?>
14
  • 4
    What a nice way of filtering arrays :) Commented Oct 23, 2019 at 13:24
  • 1
    Here there's something you can use: php.net/manual/en/function.extract.php Commented Oct 23, 2019 at 13:26
  • @TahaPaksu, I am here to learn. I appreciate all improving proposals. Commented Oct 23, 2019 at 13:26
  • 1
    Why didn't you use the array directly and assigned each of it into variables? You can do guardar_post($_POST['val101'], ... Commented Oct 23, 2019 at 13:27
  • 1
    … or all the $youtube[$i] values you are outputting inside the loop via echo might be just whitespace, who knows. Commented Oct 23, 2019 at 13:35

1 Answer 1

1

You have:

if ($youtube[$i] == 0){

}
else {

}

But POST vars are all strings. Change to:

if ($youtube[$i] == "0"){

}
else {

}

In otherwords, an equality on a string to numerical 0 will be true in your cases. And thus your else never executes.

*** Edit. PROOF

$test1 = "filename.dat";
$test2 = "2939";
$test3 = "some useful data";
$test4 = "0";

if ($test1 == 0) {
    // Dont do anything
}
else {
    echo "Do Work 1.";
}

if ($test2 == 0) {
    // Dont do anything
}
else {
    echo "Do Work 2.";
}

if ($test3 == 0) {
    // Dont do anything
}
else {
    echo "Do Work 3.";
}

if ($test4 == 0) {
    // Dont do anything
}
else {
    echo "Do Work 4.";
}

Only echoes out Do Work 2.. All other echo()s are not run because the equality of a string to a number 0 will return true except in those cases where the string also is numerical, and then the interpreter will compare the numerical values.

As how this relates to the OP's question: It can be inferred that the OP's POST vars must contain some non-numerical data because the OP insists that the 3rd array is populated:

But the third loop is not executed and there are real values on the variables inside the third array.

I will add likely the loop is executed, but not giving the expected the results due the reasons so mentioned.

Tested on PHP 5 and 7.

Sign up to request clarification or add additional context in comments.

5 Comments

@mvasco, what kind of values are you storing to the POST vars. Could you give an example? At all do you store non-numerical data in particular at times?
@GetSet, they will store string values
@mvasco Thats the answer i was waiting for. Have a look at my solution above which may address your problem.
You are, changing the comparison to =="0" solves the issue.
Excellent. Was curious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.