使用PHP将数组插入数据库

时间:2018-05-25 17:55:09

标签: php mysql arrays database

我尝试了很多方法通过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);  
}
}

image of my database structure

2 个答案:

答案 0 :(得分:0)

在您的foreach $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];
}

答案 1 :(得分:0)

我相信mysqli自php 5.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教程 因为这会让你的生活变得更轻松。