使用php将数组插入数据库

8ehkhllq  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(427)

我尝试了许多方法通过localhost将数组插入phpmyadmin数据库。到目前为止还没有一个成功。我想我可能把太多不同的解决方案混合在一起了。我可能试图错误地插入int。
我的解决方案基于以下参考:https://www.phpflow.com/php/insert-php-array-into-mysql-table/

$result = array( "0" => array(1, "blah", "blah", "blah", "blah", 2, 3),
                 "1" => array(2, "blah", "blah", "blah", "blah", 4, 5));

$connect = mysqli_connect("localhost", "root", "", "test");
if(is_array($result)){
foreach ($result as $row) {  
    $fieldVal0 = (int) $result[$row][0];
    $fieldVal1 = mysql_real_escape_string($result[$row][1]);
    $fieldVal2 = mysql_real_escape_string($result[$row][2]);
    $fieldVal3 = mysql_real_escape_string($result[$row][3]);
    $fieldVal4 = mysql_real_escape_string($result[$row][4]);
    $fieldVal5 = (int) $result[$row][5];
    $fieldVal6 = (int) $result[$row][6];

    $query ="INSERT INTO testtable ( id, english, navajoVerb, person, mode, verbNum, bookNum) VALUES ( '". $fieldVal0."','".$fieldVal1."','".$fieldVal2."','". $fieldVal3."','".$fieldVal4."','".$fieldVal5."','".$fieldVal6."' )";

    mysqli_query($connect,$query);  
}
}

我的数据库结构的图像

oaxa6hgo

oaxa6hgo1#

在你的房间里 $row 是数组,而不是键(“0”,“1”),您只需更改代码即可使用$row[0],如下所示:

foreach ($result as $row) {  
    $fieldVal0 = (int) $row[0];
    $fieldVal1 = mysql_real_escape_string($row[1]);
    $fieldVal2 = mysql_real_escape_string($row[2]);
    $fieldVal3 = mysql_real_escape_string($row[3]);
    $fieldVal4 = mysql_real_escape_string($row[4]);
    $fieldVal5 = (int) $row[5];
    $fieldVal6 = (int) $row[6];
}
yi0zb3m4

yi0zb3m42#

我相信mysqli是从php5.5.x开始的,不管怎样,我建议在使用数据库和php时使用pdo。它使数据的转义变得更容易。
1:if语句中的$记录来自哪里?因为如果不是这样的话,它就永远不会过去。
2:在嵌套数组中将数值声明为字符串,但不确定类型转换是否是最佳解决方案。你可以只用整数。

array(1, "blah" ....

这可能有用:

$result = array( "0" => array(1, "blah", "blah", "blah", "blah", 2, 3),
                 "1" => array(2, "blah", "blah", "blah", "blah", 4, 5));

$connect = mysqli_connect("localhost", "root", "", "test");

if(is_array($result))
{

    // First array ( 0 and 1) 
    foreach ($result as $key =>$row) 
    {
        // We can just loop over the next array so we have to type less
        // lets declare a new array with escaped results

        $escaped_results = array();

        // Now loop over the row
        foreach( $row as $key => $value)
        {
            // if we don't set anything inbetween the brackets [] we tell php to auto fill the array 
            // so it adds a new array element starting from 0 
            $escaped_results[] = mysqli_real_escape_string($connect, $value);
        }

        // lets make the query easier and simpler
        // we just use implode to build the values 
        $query ="INSERT INTO testtable ( id, english, navajoVerb, person, mode, verbNum, bookNum) VALUES ('". implode("','", $escaped_results) ."')";

        // execute the query
        $result = mysqli_query($connect, $query);  

        // catch the error
        if (!$result) {
            die('Invalid query: ' . mysqli_error($connect));
        }
    }
}

尽管我会推荐一些php的pdo教程,因为这会让你的生活更轻松。

相关问题