【问题标题】:PHP array to mysql BD... missing array fieldsPHP数组到mysql BD...缺少数组字段
【发布时间】:2012-02-05 14:14:42
【问题描述】:

需要你的帮助。

我需要更新mysql表中的数组数据。我的问题是有一些数组值没有“照片”列(检查数组中的第 4 个字段),因为我的查询失败并出现错误“列数与第 1 行的值计数不匹配” 下面是我正在尝试的。

    $dept = $job->user();
$sql = array(); 
    foreach( $dept as $row ) {
       $sql[] = '('.$row['dob'].', "'.($row['name']).'", "'.($row['role']).'"
 "'.$row['email'].'", "'.($row['photo']).'" )';
    }
    mysql_query('INSERT INTO cast (dob, name, role, email, photo) VALUES '.implode(',', $sql)) or die(mysql_error());

数组数据

array
  0 => 
    array
      'dob' => string '01121978'
      'name' => string 'Ram Avtar'
      'role' => string 'Inspector'
      'email' => string 'ramavtar@gmail.com'
      'photo' => string ' '
  1 => 
    array
      'dob' => string '15021978'
      'name' => string 'Suresh Babu'
      'role' => string 'Constable'
      'email' => string 'ssbh1@mail.yahoo.com'
      'photo' => string ' ' 
  2 => 
    array
      'dob' => string '11111965'
      'name' => string 'Dean'
      'role' => string 'Inspector'
      'email' => string 'ddepth@live.in'
      'photo' => string ' '
  3 => 
    array
      'dob' => string '10061979'
      'name' => string 'Rohit Shette'
      'role' => string 'Sergeant'
      'email' => string ' '
      'photo' => string ' '
  4 => 
    array
      'dob' => string '15081979'
      'name' => string 'Ian'
      'role' => string 'warden'
      'email' => string ' '

表结构

 CREATE TABLE user(
      id INT(5) NOT NULL AUTO_INCREMENT,
      dob TEXT NOT NULL,
      name TEXT NOT NULL,
      role TEXT DEFAULT NULL,
      email TEXT DEFAULT NULL,
      photo TEXT DEFAULT NULL
    )
    ENGINE = INNODB
    CHARACTER SET latin1
    COLLATE latin1_swedish_ci;

【问题讨论】:

  • 你能显示你真正生成的sql,而不是生成它的php吗?

标签: php mysql arrays insert


【解决方案1】:

实际的问题是你漏掉了一个逗号。

变化:

echo $sql[] = '('.$row['dob'].', "'.($row['name']).'", "'.($row['role']).'"
  "'.$row['email'].'", "'.($row['photo']).'" )';

收件人:

$sql[] = "('{$row['dob']}', '{$row['name']}', '{$row['role']}',
 '{$row['email']}', '{$row['photo']}')";                  // ^^^  Here is the missing comma

缺少的值不会导致问题,它们只会导致插入一个空字符串,因为该字符串被引用了。但是,您可能希望在尝试使用之前测试以确保该键存在于数组数组中,以避免任何讨厌的E_NOTICEs。

另外,请确保在查询中使用数据之前正确转义数据 - 您不希望来自 Bobby Tables...的访问...

【讨论】:

    【解决方案2】:

    首先 - 如果那是您的真实代码,我认为您在 $row['role'] 之后缺少逗号。另外 - 为什么不检查数组键是否存在?

    试试类似的东西(未测试,只是给你一个想法)

    $dept = $job->user();
    $sql = array(); 
        foreach( $dept as $row ) {
           echo $sql[] = '('
           . (array_key_exists('dob', $row) ? $row['dob'] : 'null') . ', "'
           . (array_key_exists('name', $row) ? $row['name'] : 'null') . '", "'
           . (array_key_exists('role', $row) ? $row['role'] : 'null') . '", "'
           . (array_key_exists('email', $row) ? $row['email'] : 'null') . '", "'
           . (array_key_exists('photo', $row) ? $row['photo'] : 'null') . '" )';
        }
        mysql_query('INSERT INTO cast (dob, name, role, email, photo) VALUES '.implode(',', $sql)) or die(mysql_error());
    

    【讨论】:

    • 您的代码将插入字符串 null 而不是空值。您需要移动引号...
    • 谢谢伙计们,它有效。一直以来,我都在想这是因为照片字段中缺少价值。从未在查询时检查过。
    • 在这方面还需要 1 个小帮助。在表中插入数组值之前,我想检查它是否存在。如果它不存在,则插入它,否则跳过该数组。由于该表与其他表具有多对多关系,因此我希望保持该关系。 Name 是此表中的唯一键。
    猜你喜欢
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 2012-11-28
    相关资源
    最近更新 更多