duan0424 2015-10-21 09:00
浏览 96
已采纳

如何更新mySQL数据库中的数据?

This is my animals table in the mySQL database:

| id | animal | name | number |
|----|--------|------|--------| 
| 2  |  cat   | john |  6345  |  
| 2  | mouse  | fred |  7463  |
| 3  | monkey | noah |  2342  |

I insert data from a text file, which looks like this (for example):

animal=dog
name=alan
animal=frog
name=sam
animal=bird
name=mike

This is my code:

$id = 1
$sql = "INSERT INTO animals (animal,name,id) values(?,?,?)";
foreach($list as $row) {
$q->execute(array($row['animal'], $row['name'], $id));
}                          

After the insert my animals table looks like this:

| id | animal | name | number |
|----|--------|------|--------|
| 1  |  dog   | alan |        |
| 1  |  frog  | sam  |        |
| 1  |  bird  | mike |        | 
| 2  |  cat   | john |  6345  |  
| 2  | mouse  | fred |  7463  |
| 3  | monkey | noah |  2342  |

Later I have another text file which looks like this:

number=1425
number=9824
number=9932

The numbers are in exactly the same order like the data from the first text file. So what I want to do is just add the numbers, so that my table would look like this:

| id | animal | name | number |
|----|--------|------|--------|
| 1  |  dog   | alan |  1425  |
| 1  |  frog  | sam  |  9824  |
| 1  |  bird  | mike |  9932  | 
| 2  |  cat   | john |  6345  |  
| 2  | mouse  | fred |  7463  |
| 3  | monkey | noah |  2342  |

But I do not know how to do this in one statement. I hope you can help. Thank you very much!

  • 写回答

1条回答 默认 最新

  • douye1876 2015-10-21 09:14
    关注

    I assume that you are wanting to update the "numbers" in your SQL table?

    If so, you will need some sort of identifier to identify the rows which should be affected. So, if you want to set number=235443534 where the animal is equal to "Dog" then you will need to put that identifier in your text file. You may wanted to make the file colon seperated (;)?

    Once you have got an identifer, you will need to read the file and loop through it.

    <?php
    $fileName = "FileNameHere.txt";
    $fileToOpen = fopen($fileName,"r") or die("Error opening file :'(");
    $readFile = fread($fileToOpen,filesize($fileName));
    
    foreach($readFile as $fileLine){
     $item = split(";",$fileLine); //this will return an array {dog,34423}
     $sql = "UPDATE `animals' SET number='".$item[1]."' WHERE animal='".$item[0]."'";
     //Run your SQL and any cool code here...
    }
    
    fclose($fileToOpen);
    ?>
    

    For more about SQL update and working with files have a look at W3Schools tutorials. They're really good! :D

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起