SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict MikesBarto2002's Avatar
    Join Date
    May 2006
    Location
    New York City
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Foreach in prepare statement not working

    I have a method that inserts multiple mass dates and times into a database. I have checked to make sure that the method is receiving the array of masses correctly. The array it receives takes the following structure:

    array(2) {
    [0]=> array(3) {
    ["mass_name"]=> string(13) "Barry\'s Mass"
    ["mass_date_time"]=> string(19) "2012-04-22 16:00:00"
    ["mass_private_public"]=> string(6) "public"
    }
    [1]=> array(3) {
    ["mass_name"]=> string(14) "Jaclyn\'s Mass"
    ["mass_date_time"]=> string(19) "2012-04-22 16:00:00"
    ["mass_private_public"]=> string(6) "public"
    }
    }

    Here is the PHP used to INSERT into the database.

    PHP Code:
        public function Set_add_masses($set_masses) {
        
            global 
    $mysqli;
            
            if (
    $stmt $mysqli->prepare("INSERT INTO masses (mass_name, mass_date_time, mass_private_public, user_edited) VALUES (?, ?, ?, ?)")) {
                foreach (
    $set_masses as $key => $value) {
                    
    $stmt->bind_param("sssi"$set_masses[$key]['mass_name'], $set_masses[$key]['mass_date_time'], $set_masses[$key]['mass_private_public'], $_SESSION['user_id']);
                    if (
    $stmt->execute()) {
                        
    $this->masses 'Success';
                    } else {
                        
    $this->masses 'Failed at INSERT ' $key;
                        break;
                    }
                }
                
    $stmt->close();
            } else {
                
    $this->masses NULL;
            }
            
            return 
    $this->masses;
        
        } 
    When I dump the $this->masses variable, I get "NULL" and the masses aren't inserted into the database. Any ideas?
    James Web Development | New York, NY
    Design, Develop, Deliver

  2. #2
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Probably there is something wrong with your $mysqli, and $mysqli->prepare returns NULL thus NULL in $this->masses.

    See http://us2.php.net/manual/en/mysqli.prepare.php

  3. #3
    SitePoint Addict MikesBarto2002's Avatar
    Join Date
    May 2006
    Location
    New York City
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    My query produces the following:

    PHP Code:
    INSERT INTO masses (mass_namemass_date_timemass_private_publicuser_editedVALUES ("Barry\'s Mass""2012-04-23 00:00:00""public"1), ("Jaclyn\'s Mass""2012-04-23 00:00:00""public"1
    Is there something that I am doing wrong in the query?
    James Web Development | New York, NY
    Design, Develop, Deliver

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think you need change the ' (single quote) to a code that will be accepted by the query.

    See this page for more info:
    http://us2.php.net/manual/en/mysqli....ape-string.php

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    Have you tried echoing $mysqli->error ? According to your script, if you're getting NULL out of the function, your initial IF returned FALSE.
    For that matter, have you made sure $mysqli is a valid object?

    (PS: Mysqli->prepare cant return Null. It will return an object or FALSE.)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think his $mysqli->prepare did return FALSE and then he set $this->masses to NULL.

  7. #7
    SitePoint Addict MikesBarto2002's Avatar
    Join Date
    May 2006
    Location
    New York City
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it out. I actually had changed the name of the column in the database and I didn't realize it. Sorry for all of the trouble for a mistake on my end, but thanks for the help!
    James Web Development | New York, NY
    Design, Develop, Deliver


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
  •