将数组插入db表

时间:2017-02-20 18:49:08

标签: php mysql arrays database

我试图学习如何使用数组将多个条目插入数据库表。这是我的尝试。我做错了什么?

$servername = "localhost";
$username = "#";
$password = "#";
$dbname = "hosts";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$client = array(
    "1" => array("Jerry Garcia", "193.169.5.11"),

    "2" => array("Bill Graham", "193.169.5.12"),

    "3" => array("Arlo Guthrie", "193.169.5.13")
     );

if(is_array($client) {
    $DataArr = array();
    foreach($client as $row) {
        $fieldVal1 = mysqli_real_escape_string($client[$row][1]);
        $fieldVal2 = mysqli_real_escape_string($client[$row][2]);
        $fieldVal3 = mysqli_real_escape_string($client[$row][3]);

        $DataArr[] = "('fieldVal1', 'fieldVal2', 'fieldVal3')";

    }

    $sql = "INSERT INTO ip_data (field1, field2, field3) values ";
    $sql .= implode(',' , $DataArr);

    mysqli_query($conn, $query);
}

我试过了,但它仍然没有用。我错过了什么?

$servername = "localhost";
$username = "#";
$password = "#";
$dbname = "hosts";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$client = array(
    "0" => array("Jerry Garcia"),

    "1" => array(""193.169.5.11"),
     );

if(is_array($client)) {
    $DataArr = array();
    foreach($client as $row) {
        $fieldVal1 = mysqli_real_escape_string($client[$row][0]);
        $fieldVal2 = mysqli_real_escape_string($client[$row][1]);


        $DataArr[] = "('$fieldVal1', '$fieldVal2')";

    }

    $sql = "INSERT INTO ip_data (field1, field2) values ";
    $sql .= implode(',' , $DataArr);

    mysqli_query($conn, $query);
}

感谢您的建议。

接下来试试。

$servername = "localhost";
$username = "#";
$password = "#";
$dbname = "hosts";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$client = array(
    "0" => array("name" => "Peter Maxx", "ip" => "193.169.5.16"),
    "1" => array("name" => "Ravi Shankar", "ip" => "193.169.5.17")
     );

if(is_array($client)) {
        $DataArr = array();         
        foreach($client as $row) {

            $DataArr[] = "('". mysqli_real_escape_string($conn, $row[0]) ."', '". mysqli_real_escape_string($conn, $row[1]) ."')";
        }
        $sql = "INSERT INTO ip_data (name, ip)
VALUES 
( 'Peter Maxx', '193.169.5.16'),
('Ravi Shankar', '193.169.5.17')";
        $sql .= implode(", " , $DataArr);
        mysqli_query($conn, $sql);  

}

我收到这些错误消息。

PHP Notice:  Undefined offset: 0 in php shell code on line 5
PHP Notice:  Undefined offset: 1 in php shell code on line 5
PHP Notice:  Undefined offset: 0 in php shell code on line 5
PHP Notice:  Undefined offset: 1 in php shell code on line 5

3 个答案:

答案 0 :(得分:1)

请将assert_match user.name, mail.text_part.body.to_s.encode("UTF-8") assert_match user.activation_token, mail.text_part.body.to_s.encode("UTF-8") assert_match CGI.escape(user.email), mail.text_part.body.to_s.encode("UTF-8") 的附加内容更改为此内容:

$DataArr[]

答案 1 :(得分:1)

目前还不清楚您的目的是什么,因为您的问题中的SQL查询指定要插入表中的3个字段(field1,field2,field3),但您的clients数组中只有2个值。如果要在一个查询中插入多行,可以说客户端数组中的“name”和“ip”值,可以这样做:

    if(is_array($client)) {
        $DataArr = array();         
        foreach($client as $row) {
            //CREATE ARRAY WITH name AND ip VALUES FOR EACH USER...               
            $DataArr[] = "('". mysqli_real_escape_string($conn, $row[0]) ."', '". mysqli_real_escape_string($conn, $row[1]) ."')";
        }
        $sql = "INSERT INTO ip_data (name, ip) VALUES ";
        $sql .= implode(", " , $DataArr);
        mysqli_query($conn, $sql);
    }

$ sql变量将包含符合多个插入语法的以下查询:

INSERT INTO ip_data (name, ip) 
VALUES ('Jerry Garcia', '193.169.5.11'), 
       ('Bill Graham', '193.169.5.12'), 
       ('Arlo Guthrie', '193.169.5.13')

请注意,您的问题中包含$query而不是$slq。它应该是:mysqli_query($conn, $sql);

另一件事:使用mysqli_real_escape_string()程序方式而不是object oriented way,如您的示例所示,需要将链接标识符作为参数传递给连接:mysqli_real_escape_string($conn, $row[0])

更新: 在最近的尝试中,您已将数组更改为关联数组,因此应执行此操作:

 $servername = "localhost";
 $username = "#";
 $password = "#";
 $dbname = "hosts";

 $conn = new mysqli($servername, $username, $password, $dbname);

 if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
 }
 $client = array(
     "0" => array("name" => "Peter Maxx", "ip" => "193.169.5.16"),
     "1" => array("name" => "Ravi Shankar", "ip" => "193.169.5.17")
 );

 if(is_array($client)) {
    $DataArr = array();         
    foreach($client as $row) {

        $DataArr[] = "('". mysqli_real_escape_string($conn, $row["name"]) ."', '". mysqli_real_escape_string($conn, $row["ip"]) ."')";
    }
    $sql = "INSERT INTO ip_data (name, ip) VALUES ";
    $sql .= implode(", " , $DataArr);
    mysqli_query($conn, $sql);  

 }

答案 2 :(得分:0)

正确

发件人

   if(is_array($client) {

   if(is_array($client)){

您的数组$client在每次迭代中只有2个元素

发件人

    # You will get PHP Warning:  Illegal offset type
    # since $row is already array

    $fieldVal1 = mysqli_real_escape_string($client[$row][1]);
    $fieldVal2 = mysqli_real_escape_string($client[$row][2]);
    $fieldVal3 = mysqli_real_escape_string($client[$row][3]);

    // For first iteration 
    // $row[0] = "Jerry Garcia"
    // $row[1] = "193.169.5.11"

    $fieldVal1 = mysqli_real_escape_string($conn, $row[0]);
    $fieldVal2 = mysqli_real_escape_string($conn, $row[1]);

    // $row[3] does not exists so comment it and set $fieldVal3 some data
    // $row[3] not exists in your array and even $row[2]
    // or add one value to your $client array and access using $row[2]
    // $fieldVal3 = mysqli_real_escape_string($row[3]);

    $fieldVal3 ='somedata';

最后

发件人

 $DataArr[] = "('fieldVal1', 'fieldVal2', 'fieldVal3')";

 $DataArr[] = "('$fieldVal1', '$fieldVal2', '$fieldVal3')";