通过CSV复制记录更新数据库

时间:2019-07-09 20:02:17

标签: php mysql

我正在尝试从网页更新数据库。我可以连接到数据库,并且可以从网页更改数据库中的记录。我目前的问题是,当我上传CSV时,网页会更改记录,但会将数据库中的所有记录更改为CSV的最后一行。它为CSV中的每个项目创建一行,但是每一行都重复到同一记录。谁能指出哪里可能有错误?

$connect = mysqli_connect("$dbHost", "$dbUsername", "$dbPassword", 
"$dbName");
$message = '';

if(isset($_POST["upload"]))
{
 if($_FILES['product_file']['name'])
 {    
  $filename = explode(".", $_FILES['product_file']['name']);
  if(end($filename) == "csv")
  {
   $handle = fopen($_FILES['product_file']['tmp_name'], "r");
   while($data = fgetcsv($handle))
   {
    $device_state = mysqli_real_escape_string($connect, $data[0]);
    $last_seen = mysqli_real_escape_string($connect, $data[1]);  
    $device_type = mysqli_real_escape_string($connect, $data[2]);
    $site_number = mysqli_real_escape_string($connect, $data[3]);
    $meritage_site_number = mysqli_real_escape_string($connect, 
$data[4]);
    $store_address = mysqli_real_escape_string($connect, $data[5]);
    $city = mysqli_real_escape_string($connect, $data[6]);
    $state = mysqli_real_escape_string($connect, $data[7]);
    $ip_address = mysqli_real_escape_string($connect, $data[8]);
    $hostname = mysqli_real_escape_string($connect, $data[9]);
    $node_number = mysqli_real_escape_string($connect, $data[10]);
    $real_status = mysqli_real_escape_string($connect, $data[11]);
    $short_model = mysqli_real_escape_string($connect, $data[12]);
    $software_version = mysqli_real_escape_string($connect, $data[13]);
    $query = "
     UPDATE offline_devices 
     SET 
        device_state = '$device_state', 
        last_seen = '$last_seen', 
        device_type = '$device_type',
        site_number = '$site_number',
        meritage_site_number = '$meritage_site_number',
        store_address = '$store_address',
        city = '$city',
        state = '$state',
        ip_address = '$ip_address',
        hostname = '$hostname',
        node_number = '$node_number',
        real_status = '$real_status',
        short_model = '$short_model' 
      WHERE software_version = '$software_version'
    ";

    mysqli_query($connect, $query);
   }
   fclose($handle);
   header("location: ORBupload.php?updation=1");
  }
  else
  {
   $message = '<label class="text-danger">Please Select CSV File 
 only</label>';
  }
 }
  else
 {
  $message = '<label class="text-danger">Please Select File</label>';
 }
 }

 if(isset($_GET["updation"]))
{
 $message = '<label class="text-success">Database Updated</label>';
}

$query = "SELECT * FROM offline_devices";
$result = mysqli_query($connect, $query);

当我上传文件时,数据库为每一行复制相同的记录。数据库中的每一行都重复CVS的最后一行。

1 个答案:

答案 0 :(得分:0)

在没有看到数据库结构和数据或CSV文件内容的情况下,我不能确定是什么问题,但是我认为您应该非常仔细地检查查询。在我看来,这可能是问题所在。如果数据库中的每个记录都具有相同的 software_version ,则每次使用该software_version值运行此查询时,您都将更新数据库中的每个记录:

    $query = "
     UPDATE offline_devices 
     SET 
        device_state = '$device_state', 
        last_seen = '$last_seen', 
        device_type = '$device_type',
        site_number = '$site_number',
        meritage_site_number = '$meritage_site_number',
        store_address = '$store_address',
        city = '$city',
        state = '$state',
        ip_address = '$ip_address',
        hostname = '$hostname',
        node_number = '$node_number',
        real_status = '$real_status',
        short_model = '$short_model' 
      WHERE software_version = '$software_version'
    ";

该查询是否要检查product_id或其他内容?也许是node_number?