简体   繁体   中英

Insert array into database using PHP

I have x, y, and z that are arrays. The data displays properly but I cannot get it to insert into my database. It inserts the proper amount of rows as all 0's and not the int values that are entered by the user. Here is the php.

$x = $_POST['x'];
$y = $_POST['y'];
$z = $_POST['z'];


foreach($x as $result){
    $query = 'INSERT INTO table
            (x, y, z)
                VALUES (:x, :y, :z)';
    $statement = $db->prepare($query);
    $statement->bindValue(':x', $x);
    $statement->bindValue(':y', $y);
    $statement->bindValue(':z', $z);
    $statement->execute();
    $statement->closeCursor();
}

I get this error: Notice: Array to string conversion in

It is on all 3 bindValue lines

I know the foreach is wrong but that is the only loop that I have gotten close with. Gives me the proper amount of rows but only inserts 0's into the database.

You have to insert x value with y, z on same index like,

foreach($x as $key=>$xval){
    $query = 'INSERT INTO table
            (x, y, z)
                VALUES (:x, :y, :z)';
    $statement = $db->prepare($query);
    $statement->bindValue(':x', $xval);
    // check if y value exist on same key
    $statement->bindValue(':y', isset($y[$key]) ? $y[$key] : '');
    // check if z value exist on same key
    $statement->bindValue(':z', isset($z[$key]) ? $z[$key] : '');
    $statement->execute();
    $statement->closeCursor();
}

From the Bulk Insert Prepared Statements you can try batch insert like,

try {
    $sql="INSERT INTO table (x, y, z) VALUES ";
        $insertQuery    = array();
        $insertData     = array();
        foreach ($x as $key=>$xval ) {
            $insertQuery[] = '(?,?,?)';
            $insertData[] = $xval;
            $insertData[] = isset($y[$key])?$y[$key]:'';
            $insertData[] = isset($z[$key])?$z[$key]:'';
        }

        if (!empty($insertQuery)) {
            $sql .= implode(', ', $insertQuery);
            $stmt = $this->db->prepare($sql);
            $stmt->execute($insertData);
            $stmt->closeCursor();
        }
 } catch (PDOException $e) {
        error_log('Error reading the session data table in the session reading method.');
        error_log(' Query with error: '.$sql);
        error_log(' Reason given:'.$e->getMessage()."\n");
        return false;
 }

Below is my logic simple and straight to understand for that type of problems :

$sizex = count($x);
$sizey = count($y);
$sizez = count($z); 

$maxsize = max($sizex,$sizey,$sizez);
for ($i = 0; $i < $maxsize; $i++) {
  $query = 'INSERT INTO table
            (x, y, z)
                VALUES (:x, :y, :z)';
    $statement = $db->prepare($query);
    $statement->bindValue(':x', isset($x[$i])?$x[$i]:''));
    // check if y value exist on same key
    $statement->bindValue(':y', isset($y[$i])?$y[$i]:'');
    // check if z value exist on same key
    $statement->bindValue(':z', isset($z[$i])?$z[$i]:'');
    $statement->execute();
    $statement->closeCursor();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM