简体   繁体   English

PHP数组到mysql BD…缺少数组字段

[英]PHP array to mysql BD… missing array fields

Need your help. 需要你的帮助。

I need to update the array data in the mysql table. 我需要更新mysql表中的数组数据。 my problem is that there are some array values which dont have "photo" coloum (check 4th field in the array) because of that my query is failing with error "Column count doesn't match value count at row 1" below is what im trying. 我的问题是,有些数组值没有“照片”列(检查数组中的第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 Data 数组数据

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 ' '

table structure 表结构

 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;

The actual problem is that you missed a comma. 实际的问题是您错过了逗号。

Change: 更改:

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

To: 至:

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

The missing values will not cause a problem, they will just cause an empty string to be inserted, because the string is quoted. 缺少的值不会引起问题,它们只会导致插入空字符串,因为该字符串已被引用。 However, you may wish to test to make sure the key exists in the array array before you try to use it, to avoid any nasty E_NOTICE s. 但是,您可能希望在尝试使用该键之前进行测试,以确保该键存在于数组数组中,以避免产生任何讨厌的E_NOTICE

Also, make sure you properly escape your data before you use it in a query - you don't want a visit from Bobby Tables ... 此外,在查询中使用数据之前,请确保已正确转义数据-您不希望Bobby Tables进行访问...

First - if that's your real code I think you're missing a comma after $row['role'] . 首先-如果那是您的真实代码,我认为您在$row['role']之后缺少逗号。 Also - why not check if array key exists? 另外-为什么不检查数组键是否存在?

Try something like (not tested, just to give you an idea) 尝试类似的方法(未经测试,只是为了给您一个想法)

$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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM