【问题标题】:How to insert data which does not have specific sequence?如何插入没有特定顺序的数据?
【发布时间】:2013-06-04 20:24:33
【问题描述】:

我的数组 print_r 是:

Array
(
    [email] => xxx@cs.caddmu.edu
    [attempt] => 0
    [timestamp] => 1369676665
    [smtp-id] => <1369676650.51a39b6a76cec@www.openaccesspub.org>
    [response] => 451 4.2.0 Temporarily Grey listed.  Try again in a couple of minutes 
    [category] => Invitation
    [event] => deferred
)
Array
(
    [email] => reidsdds@cs.cdsmu.edu
    [timestamp] => 1369676845
    [smtp-id] => <1369676650.51a39b6a76cec@www.openaccesspub.org>
    [response] => 250 2.0.0 r4RHlOGH017661 Message accepted for delivery 
    [category] => Invitation
    [event] => delivered
)
Array
(
    [email] => jrai@openhh.com
    [timestamp] => 1369678994
    [smtp-id] => <1369678990.51a3a48e93428@ohhpb.org>
    [category] => Reviewers
    [event] => processed
)
Array
(
    [email] => sss@lusst.fi
    [timestamp] => 1369678997
    [smtp-id] => <1369678990.51a3a48e93428@oub.org>
    [response] => 250 2.0.0 Ok: queued as 02C103F0454 
    [category] => Revie
    [event] => delivered
)

表格有这些行:

event_id、事件、电子邮件、类别、时间戳、响应、尝试、url、状态、原因、类型、操作、m_id。

我正在尝试用这个插入 mysql 表:-

foreach ($temp_array as $key => $poke) 
{ 
    mysql_query ("INSERT INTO temp_array (email,timestamp,category,event,response,attempt,reason,o_id,operator,action,...)VALUES ('$poke[email]','$poke[timestamp]','$poke[category]','$poke[event]',.... )" ); 
}

但我收到错误“未定义索引:

中的类别
  C:\xampp\htdocs\eembeta\array_temp.php on line 34

如何插入没有特定顺序的数据?

【问题讨论】:

    标签: mysql multidimensional-array


    【解决方案1】:

    这与序列无关,但 $poke['category'] 在该数组(要插入的元素)中不存在。确保您要使用的所有数组字段都已初始化:

    $poke['category'] = $poke['category'] ? $poke['category'] : '';
    

    该代码的意思是:该字段是否已设置?是的:使用它。否:使用空字符串。 这样 $poke['category'] 是已知的,所以你不会出错。

    顺便说一句,您可以将 '' 替换为 0、null 或您需要的任何内容。

    【讨论】:

    • 非常感谢您的建议。
    • 但我仍然得到错误..! foreach($temp_array as $key => $poke) { $poke['category'] = $poke['category'] ? $poke['category'] : ''; mysql_query("INSERT INTO temp_array (email,timestamp,category,event)VALUES ('$poke[email]','$poke[timestamp]','$poke[category]',' $poke[事件]' )" ); }
    • 您似乎在使用单引号尝试替换为 : ('.$poke[email].','.$poke[timestamp].','.$poke[category].', '.$poke[event].' )" );
    • 如果不在双引号中,请在键周围使用单引号。 .','.$poke['category'].',' - 简单的规则:每个字符串都被引用。在您的情况下,整个 SQL 部分和数组键都是字符串。
    【解决方案2】:

    在使用查询中的数据之前,先对其进行清理以避免 SQL 注入。为此,请考虑使用 PDO。

    回答你的问题

    为每个键创建一个临时变量,例如: $event = isset($_GET ['event']) ?$_GET ['event'] : null;

    【讨论】:

      猜你喜欢
      • 2010-10-21
      • 1970-01-01
      • 2012-12-10
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-02
      • 2022-11-30
      相关资源
      最近更新 更多