Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将此数据插入mysql php_Php_Mysql - Fatal编程技术网

如何将此数据插入mysql php

如何将此数据插入mysql php,php,mysql,Php,Mysql,这是我的数据的样子。这是我的insert 1行代码 Array ( [id] => Array ( [0] => 1 [1] => 1 [2] => 1 ) [kind] => Array ( [0] => 1 [1] => 2 [2] =>

这是我的数据的样子。这是我的insert 1行代码

Array
(
    [id] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
        )

    [kind] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [qty] => Array
        (
            [0] => 123
            [1] => 456
            [2] => 789
        )

)
请帮我做一个函数来插入多行,比如

function insert($table, $array_data)
{
    $key = array();
    $value = array();
    foreach($array_data as $v=>$row){
        $key[] = $v;
        $value[] = "'".$row."'";
    }
    $data_key = implode(',', $key);
    $data_value = implode(',', $value);

    $sql = "INSERT INTO ".$table."(".$data_key.") VALUES (".$data_value.")";
    $this->setQuery($sql);
    $this->query();
}
我尝试了很多方法,但只返回空值或“数组”字符串。我的观点是将这些价值转化为
将值(1、v2、v3)、(1、v2.1、v3.1)插入表(id、种类、数量)…


非常感谢^^

希望这对你有帮助。未测试,因此如果存在任何语法错误,请进行更改。 您只需要生成一个字符串,如(val1,val2),(val3,val3)

函数($table,$array\u data)
{
...
$keys=数组\u键($array\u数据);
$length=count($array_data[$keys[0]]);
$sql='';
对于($i=0;$i<$length;$i++)
{
$sql.=“(”;
foreach($key作为$index=>$key){
$sql.=$array_data[$key][$i];
如果($indexsetQuery($sql);
$this->query();
}

我猜你想要这样的东西:

function them($table, $array_data)
{

    ...
    $keys = array_keys($array_data);
    $length = count($array_data[$keys[0]]);
    $sql = '';
    for ($i=0;$i < $length;$i++)
    {
        $sql .= "('";
        foreach($keys as $index => $key){
           $sql .= $array_data[$key][$i];
           if($index < count($keys)-1)
             $sql .= ",";
        }
        $sql .= "')";
        // if there is another array member, add a comma
        if( $i < $length-1 )
        {
            $sql .= ",";
        }
    }

    $sql = "INSERT INTO ".$table."(".implode(',',$keys).") VALUES ".$sql;
    $this->setQuery($sql);
    $this->query();
}
function insert\u multi($table,$array\u data)
{
$sql=“插入到“$table.”(“.introde(“,”,array_keys($array_data))。”)值中”;
$rows=[];
$columns=array\u键($array\u数据);
对于($ii=0;$iisetQuery($sql);
$this->query();
}

但是,在以这种方式构建查询时,请做好SQL注入的准备…

如果您都知道如何构建查询,我建议您查看@Epodax,这完全没有必要。您可以使用单个查询将多行插入表中,OP建议……当“$data_value=infrade(',',$value);”查找数组时,将返回“Array”。例如,$值为“种类”或“数量”值时。您必须考虑如何将这些数据存储在数据库中,可能是json格式或其他格式。它返回
值(1,1,1)、(1,2,3)、(123456789)
,但我希望它是
值(1,1123)、(1,2456)、(1,3789)
。抱歉,现在它已被修复。这可以准确地获得我想要的值,但表键是emtpy:(好吧,你已经准备好了它的键。所以$data_键来自你的问题。甚至你可以使用我的答案中的$keys作为你的专栏。)。
function them($table, $array_data)
{

    ...
    $keys = array_keys($array_data);
    $length = count($array_data[$keys[0]]);
    $sql = '';
    for ($i=0;$i < $length;$i++)
    {
        $sql .= "('";
        foreach($keys as $index => $key){
           $sql .= $array_data[$key][$i];
           if($index < count($keys)-1)
             $sql .= ",";
        }
        $sql .= "')";
        // if there is another array member, add a comma
        if( $i < $length-1 )
        {
            $sql .= ",";
        }
    }

    $sql = "INSERT INTO ".$table."(".implode(',',$keys).") VALUES ".$sql;
    $this->setQuery($sql);
    $this->query();
}
function insert_multi($table, $array_data)
{
    $sql = "INSERT INTO " . $table . " (" . implode(',', array_keys($array_data)) . ") VALUES ";
    $rows = [];
    $columns = array_keys($array_data);
    for ($ii = 0; $ii < count($array_data[$columns[0]]); $ii++) {
        $values = [];
        foreach ($columns as $column) {
            $values [] = $array_data[$column][$ii];
        }
        $rows [] = "('" . implode("','", $values) . "')";
    }
    $sql .= implode(',', $rows);

    $this->setQuery($sql);
    $this->query();
}