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

  • SEARCH
  • Home
  • 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 plz help I am trying to convert this html to jade using
hey guys how to make these php html links codes possible. i have tried
hey guys I have the follow code in js: $(document).ready(function(){ $(.replyLink).click(function(){ $(#form-to-+this.id).html(htmlForm()).toggle(500); return false;
Hey guys here is my php: $x = array(one, two, three); foreach ($x as
Hey guys I'm using python and I was just wondering how we can make
Hey Guys I'm running this little function here function getBeaches() { $request=Slim::getInstance()->request(); $args=filter_var_array(func_get_args(),FILTER_SANITIZE_STRING); $sql=SELECT
Hey guys this is my html code: <div class=nakupy> <li class=icn_kategorie><a href=#>Nákupy</a> <div class=sub_menu>
hey guys having this really simple problem but cant seem to figure out have
Hey guys I wanted to create a JScrollPane but it won't work... and I
Hey guys (and gals) I'm having a problem using Assembly GetExportedTypes() in .NET 4.0.

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.