Wrong datatype for second argument

When I look in my error file it says Wrong datatype for second argument for the following code.
What is wrong in this one? Can anyone explain what I should change? It seems to work when I run it, but I don’t like the error message.


<label>
<input type="checkbox" name="music_genre[]" id="music_genre<?php echo $row_rs_genres['cat_id']; ?>" value="<?php echo $row_rs_genres['cat_id']; ?>" <?php echo in_array('$row_rs_genres["cat_id"]',$music_genre) ? 'checked="checked"' : ''; ?> /><?php echo $row_rs_genres['category']; ?></label>

Earlier in the code I have the following as well:

$user_id = $_SESSION['code'];
foreach ($_POST['music_genre'] as $value) {

  if ($value) {
$sql="INSERT INTO genre(user, genre)VALUES('$user_id', '$value')";
$result=mysql_query($sql);
  }
}

change


$result=mysql_query($sql); 

in


$result=mysql_query($sql) or die('mysql error' . mysql_error() . ' in query $sql '); 

and see how the query looks like when you get the error.

By the way, if you’re learning PHP or working on a new script, you should look into mysqli and/or pdo, since mysql has been deprecated.

Hmmm, there is no error at all when changing the code either. It’s just a file on the hosting server, called error_log that is listing this error. Otherwise it seems to work fine. The result is inserted into the db.
I just thought it would be nice to get rid of the error… whatever it might be.

By the way… is it hard to change things from mysql to mysqli? I’ve done quite a lot of things on this one and it’s almost ready…

Can you provide the exact error/info/warning?

I imagine it’s referring to
in_array(‘$row_rs_genres[“cat_id”]’,$music_genre)
and it’s saying that $music_genre is not an array.

Also, you don’t want quotes around the first part, unless that is the actual text you’re looking for.


in_array( $row_rs_genres["cat_id"], $music_genre )

The error message I get is this:
PHP Warning: in_array() [<a href=‘function.in-array’>function.in-array</a>]: Wrong datatype for second argument in thefilename.php

I removed the quotes. And how do I solve it if the music genre isn’t an array?

if (is_array($music_genre))
   $result = in_array( $row_rs_genres["cat_id"], $music_genre );

That last one didn’t solve it either…

where is the line that uses in_array() in your code? Can you provide that for us, as I was unsure where QMonkey got that line from…

@brad62; I see there you’re plugging user submitted data straight into a query (from the $_POST array). You should be sanitizing the contents of the $_POST array before allowing it anywhere near the database (either by use of the mysqli_real_escape_string() string function or more preferably by making use of [URL=“http://php.net/manual/en/mysqli.quickstart.prepared-statements.php”]prepared statements) otherwise your code will be vulnerable to an [URL=“http://php.net/manual/en/security.database.sql-injection.php”]SQL Injection attack. All user submitted data no matter how it’s being submitted (GET, POST or REQUEST arrays or a cookie) must always be considered unsafe untill it has been validated and sanitized.

Post the code that declares $music_genre.

Thank you oddz. I started looking for where in the script $music_genre was set and all of a sudden I remember that it was a part of another thought I had that I skipped later. So this line was probably something I worked on earlier and now got back to. Then I changed bits and pieces around it and I fugured out that I actually don’t need that one.
Now I wrote the whole part again and it works without the error part. Sorry, it’s been a while since I worked with this page and always hard to get back into the way I was thinking before.

So, now it’s solved and the code looks like this:

<label>
<input type="checkbox" name="music_genre[]" id="music_genre<?php echo $row_rs_genres['cat_id']; ?>" value="<?php echo $row_rs_genres['cat_id']; ?>"/><?php echo $row_rs_genres['category']; ?></label>

And I have a loop around this that I made before that is working fine. I guess I was thinking about a loop or not when I was working with the page.

And thanx SpacePhoenix for the reminder about sanitizing. I have that in a lot of places in this script and I probably just forgot it here. I will try to search for it all over my code now and make sure it’s a part of it.

And then I will probably start to change everything to MySqli… and that sounds really sad… after all the work…
And I’m also working in Dreamweaver that doesn’t support mysqli… only because I’m always a beginner and play around with php. Then Dreamweaver is helping me out a lot… since it’s the way I once learned this a couple of years ago… the wrong way I suppose.
And I’m 51 yo and not really happy to start learning the things all over again, the right way…