1

I have this PHP Code:

for($i=1; $i<=$_POST["counter"]; $i++)
{
    if($_POST["checkbox$i"])
    {
        //select the billing_pdf row
        $sql="SELECT * from billing_pdf_archive where sequence = '".$_POST["checkbox$i"]."' ";
        $rs=mysql_query($sql,$conn);
        while($result=mysql_fetch_array($rs))
        {
            //now select all contacts for the company who receive all accounts emails
            $sql2="SELECT * from contacts where company_sequence = '".$result["customer_sequence"]."' and receive_accountsemails = 'yes' ";
            $rs2=mysql_query($sql2,$conn) or die(mysql_error());
            if(mysql_num_rows($rs2) > 0)
            {
                while($contacts2=mysql_fetch_array($rs2))
                {
                    //generate the list of emails address in an array
                    $emails_list[] = $contacts2["email"];
                }
                $emails_list = implode(',',$emails_list);
            }
        }

        $ins_sql2="INSERT into email_attachments (email_seq, attachment) values ('".$email_sequence."', '".$result["pdf"]."') ";
        $ins_rs2=mysql_query($ins_sql2,$conn) or die(mysql_error());

        $up_sql="UPDATE emails set emailto = '".$emails_list."' where sequence = '".$email_sequence."' ";
        $up_rs=mysql_query($up_sql,$conn);
    }
}

but I am getting this error:

Fatal error: [] operator not supported for strings in

on the line that says:

$emails_list[] = $contacts2["email"];

I am using the same array code on other pages (without the for loop) and they work fine

what am I doing wrong?

2

2 Answers 2

3

you fill the array $emails_list[] in the second while with $contacts2["email"]. in the next line, you implode the array to a string.

for the next result (first while loop) $emails_list is a string. now you can't convert a string to an array

try this:

        $emailsListData = array();
        while($contacts2=mysql_fetch_array($rs2))
        {
            //generate the list of emails address in an array
            $emailsListData[] = $contacts2["email"];
        }
        $emails_list = implode(',',$emailsListData);
Sign up to request clarification or add additional context in comments.

3 Comments

Better but now you've created the same error as DanFromGermany's answer (look at where $emails_list is used).
This answer is whack according to AD7six because you don't fix all of the crap
@DanFromGermany "Fix all the crap" means only initializing the array in an appropriate place i.e. line 3 of the code in the question. If it were necessary to rewrite the whole code to avoid the error I'd understand your POV; it isn't, and therefore I don't ;).
2

You have already used $emails_list as a string some where in your page like $emails_list = 7;

and again using it like

$emails_list[] = array(1,2,4);

Comments

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.