Mysql 难以将数组数据输入数据库表

Mysql 难以将数组数据输入数据库表,mysql,arrays,database,insert,Mysql,Arrays,Database,Insert,难以将数组数据输入数据库表 我想知道是否有人能指出以下脚本的错误。我在将数组数据输入数据库表时遇到困难。完整代码如下所述,最后一个数组的结果应输入数据库表中,并以print\r()格式打印出来 就收集6个数据数组的内容和创建MySQL数据库表而言,一切似乎都正常工作。难点在于代码的最后一部分,在该部分中,数据将被输入到数据库表中 脚本的底部包含5个不同版本的MySQL命令,这些命令是其他参与者和站点用户提供的,他们本应将数据输入数据库表。我分别尝试了每个版本,但所有5个版本都失败了。版本标签旁边

难以将数组数据输入数据库表

我想知道是否有人能指出以下脚本的错误。我在将数组数据输入数据库表时遇到困难。完整代码如下所述,最后一个数组的结果应输入数据库表中,并以
print\r()
格式打印出来

就收集6个数据数组的内容和创建MySQL数据库表而言,一切似乎都正常工作。难点在于代码的最后一部分,在该部分中,数据将被输入到数据库表中

脚本的底部包含5个不同版本的MySQL命令,这些命令是其他参与者和站点用户提供的,他们本应将数据输入数据库表。我分别尝试了每个版本,但所有5个版本都失败了。版本标签旁边的注释说明了故障模式

任何帮助都将不胜感激

<?php

// Data Arrays (6)
     $current_topic_array =       array();
     $post_date_array =           array();
     $phone_array =               array();
     $item_title_original_array = array();
     $item_title_array =          array();
     $item_link_original_array =  array();

// Enter Data into the Data Arrays.
// This can be done either by local loop as shown here, or gathered from an earlier part of the routine

   for($c = 0; $c < 3; $c++) {              // eventually the maximum value shown here, 3, will be a $variable
     $current_topic_array[] =       'a'.$c; // text string - Topic Section
     $post_date_array[] =           'b'.$c; // text string - Article Post Date as text string i.e. 03-30-2012
     $phone_array[] =               'c'.$c; // text string - Author's Contact Phone number as text string i.e. 888-555-1212
     $item_title_original_array[] = 'd'.$c; // text string - Original Article Title
     $item_title_array[] =          'e'.$c; // text string - Modified Article Title (shortened)
     $item_link_original_array[] =  'f'.$c; // text string - Author's website URL i.e. http://www...
   }

// MySQL Database Connection -  Authentication: user, password
     $TableName = 'User_Data';
     $dbh = mysql_connect ("localhost", "user", "password")
     or die ('Cannot connect to the database because: ' . mysql_error());
     mysql_select_db ("database");

// Creates a Database Table only if the Table does not already exist
     if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$TableName."'")) != 1) {
        mysql_query("CREATE TABLE $TableName(
          id INT NOT NULL AUTO_INCREMENT,
          PRIMARY KEY (id),
          Topic                varchar(300)  NOT NULL default '',
          Post_Date            varchar(100)  NOT NULL default '',
          Phone_Number         varchar(100)  NOT NULL default '',
          Original_Item_Title  varchar(2000) NOT NULL default '',
          Modified_Item_Title  varchar(2000) NOT NULL default '',
          Original_Item_Link   varchar(2000) NOT NULL default '') ") or die(mysql_error()
        );
     }

// Combine all 6 Data Array into one array
     $queries = array(); 
     for($i = 0; $i < count($current_topic_array); $i++) {
        $queries[] = '(
           '.$current_topic_array[$i].',
           '.$post_date_array[$i].',
           '.$phone_array[$i].',
           '.$item_title_original_array[$i].',
           '.$item_title_array[$i].',
           '.$item_link_original_array[$i].'
        )'; 

        // Check - $queries array item &i
           echo '<span style="color: #008000;">'.$queries[$i].'</span>';
     }

// Check - Final $queries array content
     echo '<hr>Final $queries array content =';
     echo '<pre>';
     print_r($queries);
     echo '</pre>';
     echo '<hr>';

// Enter the $queries Data into the MySQL Database Table 

// Version 1 - Failed: Column count doesn't match value count at row 1
    // mysql_query("insert into $TableName (Topic, Post_Date, Phone_Number, Original_Item_Title, Modified_Item_Title, Original_Item_Link)
    // values (". implode(',', $queries) . ")") or die(mysql_error());

// Version 2 - Failed: Column count doesn't match value count at row 1
    // mysql_query("insert into $TableName (Topic, Post_Date, Phone_Number, Original_Item_Title, Modified_Item_Title, Original_Item_Link)
    // values ('". implode("','", $queries) . "')") or die(mysql_error());

// Version 3 - Failed - No Error message and No Data imported into Table
    // $query = mysql_query("insert into $TableName (Topic, Post_Date, Phone_Number, Original_Item_Title, Modified_Item_Title, Original_Item_Link) 
    // values ('. implode(',', $queries)') or die(mysql_error()");

// Version 4 - Failed - No Error message and No Data imported into Table
    // $query = "insert into User_Data(Topic, Post_Date, Phone_Number, Original_Item_Title, Modified_Item_Title, Original_Item_Link)
    // values " . implode(", ", $queries);

// Version 5 - Failed - No Error message and No Data imported into Table
     $query = "insert into (Topic, Post_Date, Phone_Number, Original_Item_Title, Modified_Item_Title, Original_Item_Link)
     values " . implode(", ", $queries);

?>
最终
$querys
数组内容:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
根据MySql:

这就是一次插入多行的方法。请注意值之间的“,”。虽然使用for循环逐行插入数据并从查询数组中获取第i行会更容易、更安全


另外,我想指出的是,当前代码很容易受到SQL注入攻击,因为它操纵字符串,并且没有SQL检查

感谢Dmitry,如果是这种情况,那么最终的MySQL命令将如何执行数据导入到表中,在上面的代码中引用版本1到5?这样,我可以立即尝试它,看看它是否有效。请尝试将第一个版本与$queries一起使用[0]。按照版本1和$queries[0]的建议,我收到了“第1行的列计数与值计数不匹配”错误,这意味着数据太多。请打印出结果查询,您将立即看到错误。包括查询本身(“插入(……)值(…)”)最终$queries数组内容=数组([0]=>(a0,b0,c0,d0,e0,f0))这是您想要的吗?否则,我不知道怎么做。
Array
(
    [0] => (
           a0,
           b0,
           c0,
           d0,
           e0,
           f0
        )
    [1] => (
           a1,
           b1,
           c1,
           d1,
           e1,
           f1
        )
    [2] => (
           a2,
           b2,
           c2,
           d2,
           e2,
           f2
        )
)
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);