SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    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.

    PHP Code:
    <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:
    PHP Code:
    $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);
      }


  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    change
    PHP Code:
    $result=mysql_query($sql); 
    in
    PHP Code:
    $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.

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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...

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,058
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Can you provide the exact error/info/warning?
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.
    PHP Code:
    in_array$row_rs_genres["cat_id"], $music_genre 
    - Robert

  6. #6
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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?

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,058
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (is_array($music_genre))
       
    $result in_array$row_rs_genres["cat_id"], $music_genre ); 
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  8. #8
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That last one didn't solve it either...

  9. #9
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,058
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    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...
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  10. #10
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,995
    Mentioned
    100 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by brad62 View Post
    Earlier in the code I have the following as well:
    PHP Code:
    $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);
      }

    @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 prepared statements) otherwise your code will be vulnerable to an 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.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,139
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Post the code that declares $music_genre.
    The only code I hate more than my own is everyone else's.

  12. #12
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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:
    PHP Code:
    <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...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •