简体   繁体   English

将数据从数组插入数据库

[英]insert data from array to DB

I know that this theme is very common, but i'm stuck and can't find an error. 我知道这个主题很普遍,但是我被卡住了,找不到错误。

I created an array in PHP: 我用PHP创建了一个数组:

$dataarray=array("FECHAS" => date("Y-m-d"),"HORAS" => date("H:i:s"),
             "RGD" => 0,"RGA" => 0,"FLU" => 0,"DD2" => 0,
             "H2O" => 0,"PRES:U" => 0,"U" => 0,"V" => 0,"TS" => 0,
             "T1" => 0,"T2" => 0,"H1" => 0,"H2" => 0, "HS" => 0,
             "VV1" => 0,"VV2" => 0);

and i've got a table in MYSQL with the same names, but when i try to put data into it, it does nothing. 并且我在MYSQL中有一个具有相同名称的表,但是当我尝试将数据放入表中时,它什么也没做。

for($j=0;$j<$variable_para_base;$j++)
{
    $keys;
    $vars;
foreach($dataarray[$j] as $k=>$v)
{
    $keys.= $k.',';
    $vars.= $v.",";
}
echo $keys."<br>";
echo $vars."<br>";
mysqli_query($mysqli,'INSERT INTO ff ( .$keys.) VALUES ( .vars. ) ') or die(mysql_error());

unset($keys);
unset($vars);
}

if i do it with die option it does for only once another way my key starts to have strange values in the end of it. 如果我用die选项来做的话,它只会以另一种方式使我的键开始在其结尾处具有奇怪的值。 Any ideas, and again sorry for maybe a repeted question. 任何想法,并再次为可能重复的问题感到抱歉。 I get access to DB because it doesn't give me any error, though noow i'm doubting :(. 我可以访问数据库,因为它不会给我任何错误,尽管现在我怀疑:(。

You have syntax promlems in your query. 您的查询中有语法问题。

INSERT INTO ff ( .$keys.) VALUES ( .vars. ) ' change it to INSERT INTO ff ( .$keys.) VALUES ( .vars. ) '将其更改为

INSERT INTO ff ( '.$keys.') VALUES ( '.$vars.') '

Also you need to add ' to the varialbles inserted as VALUES. 另外,您需要将'添加到作为VALUES插入的变量中。

like that: $vars.= "'".$v."',"; 像这样: $vars.= "'".$v."',";

In addition your last variable is also ending with , and it shouldn't be. 此外,您的最后一个变量也以结束,但不应以结束。

You can not insert a array directly to mysql as mysql doesn't understand php data types. 您不能直接将数组插入mysql,因为mysql无法识别php数据类型。 Mysql only understands SQL. Mysql仅了解SQL。 So to insert this array into a mysql database you have to convert it to an sql statement. 因此,要将这个数组插入到mysql数据库中,您必须将其转换为sql语句。 This can be done manually or by a library. 可以手动或通过库来完成。 The output should be an INSERT statement. 输出应为INSERT语句。

Here is a standard mysql insert statement. 这是标准的mysql插入语句。

INSERT INTO TABLE1(COLUMN1, COLUMN2, ....) VALUES (VALUE1, VALUE2..)

If you have a table with name fbdata with the columns which are presented in the keys of your array you can insert with this small snippet. 如果您有一个名称为fbdata的表,其中的列显示在数组的键中,则可以插入此小片段。 Here is how your array is converted to this statement. 这是将数组转换为该语句的方式。

$columns = implode(", ",array_keys($insData));
$escaped_values = array_map('mysql_real_escape_string', array_values($insData));
$values  = implode(", ", $escaped_values);
$sql = "INSERT INTO `fbdata`($columns) VALUES ($values)";

您在查询中有错误尝试此方法,

mysqli_query($mysqli,'INSERT INTO ff (' .$keys. ') VALUES (' .$vars. ') ') or die(mysql_error());

So your end result might look something like this: 因此,您的最终结果可能如下所示:

<?
for($j=0;$j<$variable_para_base;$j++)
{
    $keys = array();
    $vars = array();
    foreach($dataarray[$j] as $k=>$v)
    {
        $keys[] = $k;
        $vars[] = $v;
    }

    $placeholders = array_fill(0, count($keys), '?'); //used to fill a number of '?' needed to fill later

    //here we use the '?' array to be placeholders for the values
    $query = "INSERT INTO ff (".implode(', ', $keys).") VALUES (".implode(', ', $placeholders).")"; //implode the arrays and separate by comma

    $statement = $mysqli->prepare($query); 

    $types  = array(str_repeat('s', count($vars))); //get the number of parameters and put the 's' to it (used for string values)
    $values = array_merge($types, $vars); //merge the arrays (gets you {'s', $value})

    call_user_func_array(array($statement, 'bind_param'), $values); //bind the values to the statement 

    $result = $statement->execute(); //execute.

    if($result) {
        print "Array inserted, worked like a charm.";
    }
    else {
        print "I failed, sorry...". $mysqli->error();
    }

    unset($keys);
    unset($vars);
}
$statement->close();
?>

This is however untested so test it good. 但是,这未经测试,因此请测试良好。

References you can use: 您可以使用的参考:

Stackoverflow question: PHP - MySQL prepared statement to INSERT an array Stackoverflow问题:PHP-MySQL准备插入语句以插入数组

Stackoverflow question: Best way to INSERT many values in mysqli Stackoverflow问题:在mysqli中插入许多值的最佳方法

Stackoverflow question: Mysqli insert command Stackoverflow问题:Mysqli插入命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM