duanfan5012 2011-10-29 21:35
浏览 17
已采纳

将数组值插入表列

I am trying to write a function that will insert data into a MySQL table like this:

for ($i = 0; $i < $num; $i++){
    if ($header[$i] == $user_table_property->name) {
        $import = "
INSERT into testing (
    $header[$i]
) values (
    '$data[$i]'
)";
    }
}

If I have something like this, it will just insert first data into first column, can I know what should I change or add? I Googled some examples and edited it myself, but it's still not working.

Here is the longer part of the codes.

if (isset($_POST['submit'])) {

if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
    echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "<br></h1>";
    echo "<h2>Displaying contents:</h2>";
    readfile($_FILES['filename']['tmp_name']);
    echo "<br>";
    echo $headers;
}


$handle = fopen($_FILES['filename']['tmp_name'], "r");

    $header = fgetcsv($handle);


    while(! feof($handle)){


while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

                $num = count($data);

               mysql_select_db("EMC", $db);

               $sql = "SELECT * from CSVtest";
               $result = mysql_query($sql,$db);

                   while ($user_table_property = mysql_fetch_field($result)) 
                            {

                  for($i=0; $i<$num; $i++){

                                  if($header[$i] == $user_table_property->name )
                                               {

                                               $import = "insert into CSVtest ( `" . $header[$i] . "`) values ('" . $data[$i] . "')";

                                               }

                                           }

                              mysql_query($import) or die(mysql_error()) ; 
                            }

                                                       } 
               }

fclose($handle);

print "Import done";
  • 写回答

3条回答 默认 最新

  • dongtou5557 2011-10-29 21:43
    关注

    I can tell you that you're not interpolating the array values properly:

    $import = "
    INSERT into testing (
        {$header[$i]}
    ) values (
        '{$data[$i]}'
    )";
    

    When they're array values, you have to use {} around the variable and index (or indices) for it to expand the variable value.

    Also, it should probably be doing something like this:

    $value = mysql_real_escape_string($data[$i]);
    $import = "
    INSERT into testing (
        `{$header[$i]}`
    ) values (
        '$value'
    )";
    

    You don't have any mysql_query() or whatnot in the if statement, so it's not apparent the code is going to work (you can't run $import after the for loop or it will only have the last $import set).

    The whole technique you're using here doesn't seem correct anyway; you probably actually want to build the column and value lists, then after the for loop, implode() them into a string for $import.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿