使用PHP将数组插入数据库

使用PHP将数组插入数据库,php,mysql,arrays,database,Php,Mysql,Arrays,Database,我尝试了许多方法通过localhost将数组插入PHPmyAdmin数据库。到目前为止,还没有人成功。我相信我可能混合了太多不同的解决方案。我可能试图错误地插入INT 我的解决方案基于以下参考: 在foreach中,$row是您的数组,而不是键(“0”、“1”),您只需将代码更改为使用$row[0],如下所示: foreach ($result as $row) { $fieldVal0 = (int) $row[0]; $fieldVal1 = mysql_real_esc

我尝试了许多方法通过localhost将数组插入PHPmyAdmin数据库。到目前为止,还没有人成功。我相信我可能混合了太多不同的解决方案。我可能试图错误地插入INT

我的解决方案基于以下参考:


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

我相信mysqli是从PHP5.5.x开始的, 总之,我建议在使用数据库和PHP时使用。 它还使转义数据变得容易得多

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教程
因为这将使您的生活更加轻松。

首先,您使用了错误的功能。mysqli_查询,您缺少“i”。第二个
$row
不是这里的索引,它是实际的行/数组。所以:
$fieldVal0=(int)$row[0](等等)第三:使用准备好的语句!这是一个节省程序,您可以去掉所有的
$fieldVal
s-为您节省至少4行容易出错的代码。第四:检查以查看出错的地方我应该在示例中将错误代码放在哪里?仍然没有插入数据库=/它是否打印一些错误?如果您打印查询,它是确定的?没有错误。我将print$query放在我声明的行后面,但什么也没发生。是否启用了
display\u errors
?您可以通过将此代码放在php
ini\u集合的开始处('display\u errors',1);错误报告(E_全部)谢谢,对不起,我在问题中复制了一些错误的项目。我已经纠正了问题中的错误,并坚持您的答案,但仍然没有任何内容插入到数据库中。好的,您在运行脚本时有任何错误吗?你还确定你正在运行sql server吗?是的,我确定我已连接,因为我在本地主机上使用另一个数据库执行其他操作,并且与它通信良好。我更改了代码,因此你可能希望重试。同时,我想知道我是否能帮你一点忙:)好的,我一直在看它,它似乎PHP7.0.0和更高版本完全删除了函数mysql_real_escape_string让我们说它不值得学习mysqli,所以请开始研究PDO。也就是说我仍然会帮你解决这个问题。
 $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));
        }
    }
}