使用PHP将数组插入数据库

时间:2022-06-01 17:54:35

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,y和z是数组。数据显示正常,但我无法将其插入到我的数据库中。它插入适当数量的行作为全0,而不是用户输入的int值。这是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

它位于所有3个bindValue行上

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.

我知道foreach是错的,但那是我接近的唯一循环。给我适当的行数,但只将0插入数据库。

2 个解决方案

#1


2  

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

你必须在同一个索引上插入带有y,z的x值,比如

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;
 }

#2


0  

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();
}

#1


2  

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

你必须在同一个索引上插入带有y,z的x值,比如

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;
 }

#2


0  

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();
}