For loop & array_push

hi,

I am trying to do something with this code:


        $authArray = array();

        for($i=0;$i<=100;$i++):
            if(isset($_POST['author'.$i])){
                array_push($authArray, mysql_real_escape_string($_POST['author'.$i]));
            }else{
                exit;
            }
        endfor;

        die("Array is ".print_r($authArray));

If you look at this page:

http://www.prima.cse.salford.ac.uk:8080/~ibrarhussain/test.html

You can click on the Author link to add multiple authors, what i am trying to do is echo out each author, but with the code i have posted above it doesn’t work, it returns nothing.

The above page is merely an example, to show you what i am trying to do. I fire this code on button click. what am i doing wrong?

Thanks

Hey Thanks,

I have gone with this code:


$authArray = $_POST['author'];

foreach ( $authArray as &$value ) {
  $value = mysql_real_escape_string($value);
} 

When i print_r the array i get this:

Array ( [0] => Testing Author 1 [1] => Testing Author 2 ) Array is 1

Which is perfect! :wink:

So moving on, my first step is to try and get this code working using the array, i am trying to execute this code:


class People {

    public function insertAuthor(){

        $authArray = $_POST['author'];
        $PCorder = 0;

        foreach($authArray as &$author):
            //Check if Pname exists
            $query = "SELECT * FROM People WHERE Pname = '".$author."'";
            $result = mysql_query($query);

            if(mysql_num_rows($result) > 0):
                $row = mysql_fetch_array($result);
                $Pid = $row['Pid'];

                $sql = "INSERT INTO PeopleCon(Pid, PCorder, PCdateadded, PCdeleted) VALUES (".$Pid.", ".$PCorder++.", now(), 0)";
                //die($sql);
                $result = mysql_query($sql);
            endif;
        endforeach;
    }
}

So basically for EVERY author that is added i want to check if the name exists, if so INSERT into PeopleCon. This code does not throw back any errors, but the INSERT does not work…

Can anyone please advise…

Thanks again :slight_smile:

I also tried this:


var_dump( $_POST ); 

And it returns nothing… :confused:

Hi,

Well basically i will be creating an insert statement:


class People {

    public function insertAuthor(){

        $authArray = array();

        for($i=0;$i<=100;$i++):
            if(isset($_POST['author'.$i])){
                array_push($authArray, mysql_real_escape_string($_POST['author'.$i]));
            }else{
                exit;
            }
        endfor;

        die("Array is ".print_r($authArray));

        //Insert statement goes here..
    }
}

I fire the code in the front end like so:


if(isset($_POST['insert_author'])){
        People::insertAuthor();
}

The input elements are like so:


<input type="text" name="author1" id="author1" />
<input type="text" name="author2" id="author2" />
<input type="text" name="author3" id="author3" />
<input type="text" name="author4" id="author4" />
...
...
<input type="text" name="author10" id="author10" />

So for now all i simply want to do is print out:


die("Array is ".print_r($authArray));

On button click, so it shows me that values in the array…

Can you see where i am going wrong?

Thanks

Did you look at the output of ;


var_dump( $_POST );

Post elements are allowed to be arrays using the syntax

<input name="author[]" ... >

You can even be specific if you need to preserve the key that goes there…

<input name="author[<?= $i ?>]" ... >

Having iterators be part of a variable’s name (i.e. $author1, $author2, $author3 … or even $array[‘element1’], $array[‘element2’] … ) is INCREDIBLY BAD PRACTICE (I’m frankly surprised no one in this thread has commented on this either).

Build the array right and you’ll be able to iterate over it as intended.


$authArray = $_POST['author'];

foreach ( $authArray as &$value ) {
  $value = mysql_real_escape_string($value);
}

The & before $value means to pass by reference. The foreach loop goes over all the authors and escape strings their values.

thanks for the feedback…

please try this;

on HTML code


<input type="text" name="author[]" id="author" />
<input type="text" name="author[]" id="author" />
<input type="text" name="author[]" id="author" />
<input type="text" name="author[]" id="author" />
<input type="text" name="author[]" id="author" />

on PHP code - inside - public function insertAuthor(){


$author = $_POST['author'];
$authors = array_filter($author);
die("Array is ".print_r($authors));

please let me know if you don’t understand something on the codes above and always check user inputs against malicious intentions

Well, if on the top line of your postback handler the var_dump( $_POST ); is outputting nothing, then the values are not even getting through - there must be something wrong with your html form.

please show the origin of the code where you’ve set $_POST[‘author’.$i]

Try


class People {

    public function insertAuthor(){

        $authArray = array();

        for($i=1;$i<=100;$i++):
            if(isset($_POST['author'.$i])){
                array_push($authArray, mysql_real_escape_string($_POST['author'.$i]));
            }else{
                break;
            }
        endfor;

        die("Array is ".print_r($authArray));

        //Insert statement goes here..
    }
}