0

我正在尝试使用 foreach() 将数组数据推送到 MySQL 数据库。我是 PHP 和 mysql 的新手,所以一直在将这些数据从 Ajax 脚本解析为 PHP,现在只需要操作数据。

将此数据推送到 SQL 以使数组中的每个数据集进入单独的行的正确语法是什么?

我正在尝试使用foreach,但复杂性是数组本身的大小可以改变,第二个复杂性是数据本身可能会被刷新(即新的一天相同的id新值),所以我想构建一些更新信息的智能不仅仅是附加;并将旧数据备份到另一个表。

这个语法正确吗?

$sql = "INSERT INTO table1 (col1, col2) VALUES ";
foreach($rows as $i=>$row) {
    if ($i>0) {
        $sql .= sprintf(",(%s,%s)", $row["col1_value"], $row["col2_value"]);
    } else {
        $sql .= sprintf("(%s,%s)", $row["col1_value"], $row["col2_value"]);
    }
}
mysql_query($sql);

数组中的数据如下 - 这只是多维数组的一部分,但我已经弄清楚如何根据需要操作数组的其余部分。

我需要做的唯一另一件事是找出一种方法来获取坐标字段并按如下方式操作它

从坐标中提取 x & y 数据 Multiple x & y by 600/386 反转 x & y 并取每个坐标的第一个数字以创建新值 y[1]x[1]。

为此,我仅在第一个数据集上进行了尝试,如下所示,但我对 PHP 的数据处理经验不足。很确定是错的。

$testcoords = $_POST['data'][0]['coords'];
list($x,$y) = explode(“:”,str_replace(“’”,“”,$testcoords));
$xtrans = number_format($x*600/386,$decimals=null);
$ytrans = number_format($y*600/386,$decimals=null);
$cont = “C”.$ytrans[0].$xtrans[0]

总结一下,三个问题

  1. 如何将数据传输到表中,在 [data] 数组中包含每个单独数据集的行?
  2. 如何覆盖和归档表中的任何现有值,而不是简单地连接?
  3. 我如何操作一个特定的字符串以返回上面定义的自定义变量?

    [data_type] => city
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 16515340
                    [owner_id] => 3475
                    [owner] => Player1
                    [coords] => '268:252
                    [name] => AC2013
                    [score] => 11863
                    [city_type] => castle
                    [location] => land
                )
    
            [1] => Array
                (
                    [id] => 16515335
                    [owner_id] => 3475
                    [owner] => Player1
                    [coords] => '263:252
                    [name] => AC2013
                    [score] => 7
                    [city_type] => castle
                    [location] => water
                )
    
            [2] => Array
                (
                    [id] => 17891610
                    [owner_id] => 3523
                    [owner] => Player2
                    [coords] => '282:273
                    [name] => City of Repoman9900
                    [score] => 1978
                    [city_type] => castle
                    [location] => water
                )
    
            [3] => Array
                (
                    [id] => 10616856
                    [owner_id] => 73
                    [owner] => Player2
                    [coords] => '024:162
                    [name] => 1killer
                    [score] => 1308
                    [city_type] => castle
                    [location] => water
                )
    
            [4] => Array
                (
                    [id] => 10813465
                    [owner_id] => 2862
                    [owner] => Player3
                    [coords] => '025:165
                    [name] => City of vuvuzea991
                    [score] => 1091
                    [city_type] => castle
                    [location] => land
                )
    
            [5] => Array
                (
                    [id] => 17367317
                    [owner_id] => 84
                    [owner] => Player4
                    [coords] => '277:265
                    [name] => Dreadland
                    [score] => 776
                    [city_type] => castle
                    [location] => water
                )
    
            [6] => Array
                (
                    [id] => 2162850
                    [owner_id] => 2989
                    [owner] => Player5
                    [coords] => '162:033
                    [name] => City of Dinoeyez
                    [score] => 157
                    [city_type] => castle
                    [location] => water
                )
    
            [7] => Array
                (
                    [id] => 2818192
                    [owner_id] => 556
                    [owner] => Player6
                    [coords] => '144:043
                    [name] => City of wildfire123
                    [score] => 7
                    [city_type] => castle
                    [location] => water
                )
    
        )
    
    [sender] => Array
        (
            [world] => Array
                (
                    [id] => 232
                    [name] => Server 4 
                    [number] => NaN
                )
    
            [alliance] => Array
                (
                    [id] => 2
                    [name] => Alliance2
                )
    
            [player] => Array
                (
                    [id] => 98
                    [name] => SuperUser
                )
    
            [browser] => Array
                (
                    [type] => Chrome
                    [version] => 25.0.1364.160
                )
    
            [aix_version] => 1.00
        )
    

    )

4

1 回答 1

0

我看到的问题:

foreach($rows as $i=>$row) {
      $sql = "INSERT INTO table1 (col1, col2) VALUES ";  //reinitialize here.
      if ($i>0) {  //Why have the if statement, the results of the conditions are the same.
        $sql .= sprintf("('%s','%s')", $row["col1_value"], $row["col2_value"]);  //add quotation marks for string values, remove the comma.
      } else {
        $sql .= sprintf("(%s,%s)", $row["col1_value"], $row["col2_value"]);
      }
      mysql_query($sql); //execute the statement here.
   }

您的其他问题:

如何覆盖和归档表中的任何现有值,而不是简单地连接?

为了覆盖你做一个更新语句,MySql 网站给出了语法提示。

要存档,您需要创建一个存档表,该表几乎是正在存档的表的副本,并插入/更新该表。

在这种情况下,我不确定您所说的连接是什么意思,它并不真正适用于规范化的数据库。

我如何操作一个特定的字符串以返回上面定义的自定义变量?

啊要跳过那个,在一个帖子中问很多。

于 2013-04-02T15:53:04.553 回答