Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4249016
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:20:24+00:00 2026-05-21T04:20:24+00:00

Hey guys, I’m using smarty and php I am trying to make this function

  • 0

Hey guys, I’m using smarty and php I am trying to make this function work

                {foreach $rows as $row}
            <input type="checkbox" name="likes[]" value="{$row.ID}">{$row.Interests}<br>
            {/foreach}

That there is the html/template for checkboxes, it grabs data from a table in my database

Now I am trying to store data into my database

     // $likes = mysql_escape_string($likes);

      $connection = mysql_open();
      $insert = "insert into Users " . 
          "values (null, '$firstName', '$lastName', '$UserName', '$email', from_unixtime('$DOB'), '$join', '$gender')";


      $result = @ mysql_query ($insert, $connection)
            or showerror();
        $id = mysql_insert_id();
        //echo $id; testing what it gets.
          mysql_close($connection);

      $connection = mysql_open();
      foreach($likes as $like)
      {

      $insert3 = "insert into ProfileInterests " .
        "values ('$id', '$like', null)";
      $result3 = @ mysql_query ($insert3, $connection)
        or showerror();

      }
      mysql_close($connection)
        or showerror();
    }

That there is the script I am using to enter data into my database…there is more above which is just cleaning the user input really.
mysql_open() is my own function, so don’t worry too much about that.

    $likes = @$_POST['likes'];

that is what I am using to get the likes….I feel that this is wrong. I am not sure what to do….

I get this error at the moment. Invalid argument supplied for foreach()
I think this is completely to do with the variable $likes, I think it’s not being treated like an array…any idea on what I should do.. I am quite a newbie.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:20:24+00:00Added an answer on May 21, 2026 at 4:20 am

    The following line :

    $likes = join(",",$likes);
    

    is transforming your $likes array to a $likes string, containing the values and separating them by commas.

    So, later, when you try to loop over $likes, its no longer an array : it’s a string — which explains the Invalid argument supplied for foreach().

    Edit after the comment : when calling the following line :

    $likes = mysql_escape_string($likes);
    

    If your $likes is an array, you’ll get some trouble, as mysql_escape_string works on a string.

    Instead of trying to escape the whole array at once, you should use mysql_escape_string on each item, while looping over the array — a bit like that :

    foreach($likes as $like)
    {
        // escape the current item :
        $escaped_like = mysql_real_escape_string($like);
        
        $insert3 = "insert into ProfileInterests values ('$id', '$escaped_like', null)";
        $result3 = @ mysql_query ($insert3, $connection) or showerror();
    }
    

    As a sidenote : you should use [**`var_dump()`**][1] on your variables, while developing, to see what they contain 😉 It’ll help you understand what your code is doing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, a couple of us are looking for a collaborative name to work
Hey guys, I'm trying to get my head around LINQ and FP, so forgive
Hey guys. I'm trying to configure TFS2010 on a home server. I want to
Hey guys, first time using stackoverflow. can you guys help me debug? Heres the
Hey guys. I am trying to get the selected option in an each loop.
Hey guys, I've been bashing my head around for hours now, over this login
Hey guys, I'm using GWT for a data-driven web application, and I'm having issues
Hey guys I am trying to implement the twitter for posting the comments about
hey guys, i'm getting an exception on the following inner exception: {Value cannot be
Hey guys i wrote a quick test. I want delete to call deleteMe which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.