简体   繁体   English

使用数组插入mysql和php

[英]Insert into mysql and php using array

I have part of the code below: 我有下面的代码的一部分:

while($array = $result->fetch_assoc() ){


    $second_query = "INSERT INTO".TBL_USERSDONTPAY."VALUES ($array[\"username\"], $array[\"password\"], '0',$array[\"userid|\"], )";

$second_result = $database->query($second_query);

}

The query doesn't seem to work. 该查询似乎不起作用。 Any clues? 有什么线索吗? I think it's a problem with the quotes or something. 我认为这是引号或其他问题。 How can actually pass array elements? 实际如何传递数组元素?

here is my whole code i want to move one row to another table 这是我的整个代码,我想将一行移到另一张表

$q = "SELECT * FROM ".TBL_USERS." WHERE username = '$subuser'"; 
          $result = $database->query($q); 

          if($result && $result->num_rows == 1){
              while($array = $result->fetch_assoc() ){
                  $second_query = "INSERT INTO" . TBL_USERSDONTPAY . "VALUES ('" . $array['username'] . "', '" . $array['password'] . "', '0', '" . $array['userid'] ."')";


                  $second_result = $database->query($second_query);
                  if($second_result){
                      // it worked!
                      $q = "DELETE FROM ".TBL_USERS." WHERE username = '$subuser'";
                      $database->query($q);
                  }
            }
          }

您需要清理该查询并删除最后的逗号。

$second_query = "INSERT INTO " . TBL_USERSDONTPAY . " VALUES ('" . $array['username'] . "', '" . $array['password'] . "', '0', '" . $array['userid'] . "')";

I see several issues with your query code 我发现您的查询代码有几个问题

  1. escaping of the array indexes in your string: 转义字符串中的数组索引:

    you can either end the string and concatenate the parts together: 您可以结束字符串并将部分连接在一起:

     $second_query = "INSERT INTO " . TBL_USERSDONTPAY . " VALUES ('" . $array['username'] . "', '" . $array['password'] . "', '0', '" . $array['userid'] . "')"; 

    or use the {$var} syntax: 或使用{$var}语法:

     $second_query = "INSERT INTO " . TBL_USERSDONTPAY . " VALUES ('{$array['username']}', '{$array['password']}', '0', '{$array['userid']}')"; 
  2. missing spaces (see example code above .. you were missing the spaces before and after the table name) 缺少空格(请参见上面的示例代码..您在表名之前和之后都缺少空格)

  3. missing field names. 缺少字段名称。 your query may work without if you specify all fields in the right order, but will fail misteriously when you alter the table later (eg add a field to the table) 如果您未按正确的顺序指定所有字段,则查询可能无法工作,但是稍后更改表时(例如向表中添加字段),查询将失败。

     $second_query = "INSERT INTO " . TBL_USERSDONTPAY . " (username, password, foo, user_id)". " VALUES ('{$array['username']}', '{$array['password']}', '0', '{$array['userid']}')"; 

    please note you should actually insert the correct field names in the second line of my example above. 请注意,您实际上应该在上述示例的第二行中插入正确的字段名称。 You can find more information on this in the MySQL docs for INSERT 您可以在INSERTMySQL文档中找到有关此信息的更多信息

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

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