使用PHP将数组数据插入MySQL数据库

时间:2015-09-25 21:39:00

标签: php mysql arrays

我是PHP MySQL的新手。我正在研究基于数据的大型项目。我为PHP中的每个数据创建了数组。现在我想将它插入到MySQL数据库中。

   $data =   array (
        [1] => array(
            'title' => "xxxxx",
            'content' => "xxxxx",
            'category' => "xxxxx",
        ), 
        [2] => array(
            'title' => "xxxxx",
            'content' => "xxxxx",
            'category' => "xxxxx",
        ), 
        [3] => array(
            'title' => "xxxxx",
            'content' => "xxxxx",
            'category' => "xxxxx",
        ), 
     );

我尝试了下面的代码但是没有用。

if(is_array($data)){

    $sql = "INSERT INTO `table`( `title`, `content`, `category`) values";

    $valuesArr = array();
    foreach($data as $row){

        $title = mysql_real_escape_string( $row['title'] );
        $content = mysql_real_escape_string( $row['content'] );
        $category = mysql_real_escape_string( $row['category'] );


        $valuesArr[] = "('title', 'content', 'category')";
    }

    $sql .= implode(',', $valuesArr);

    mysql_query($sql) or exit(mysql_error()); 
}

以下代码工作正常但它按给定值插入数据。我想插入数组。

<?php
$servername = "localhost";
$username = "xxxxx";
$password = "xxxxxxx";
$db_name = "xxxxxxxxx";

// Create connection
$link = new mysqli($servername, $username, $password, $db_name);

if(!$link){
echo "Failed" .mysqli_connect_error();
}

$title="John";
$content="John";
$cateogry="John";

 $query = "INSERT INTO table (title, content,category) VALUES ('$title', '$content','$category')";

if(mysqli_query($link,$query)) {
echo "Insertion successful";
} else {
echo "Error: " .$query . "<br>" . mysqli_error($link);
}
    mysqli_close($link);

?>

更新

<?php
$servername = "localhost";
$username = "xxxx";
$password = "xxxxx";
$db_name = "xxxxxxx";

// Create connection
$link = mysql_connect($servername, $username, $password, $db_name);

if(!$link){
echo "Failed" .mysql_connect_error();
}



if(is_array($data)){

    $sql = "INSERT INTO `table`( `title`, `content`, `category`) values";

    $valuesArr = array();
    foreach($data as $row){

        $title = mysql_real_escape_string( $row['title'] );
        $content = mysql_real_escape_string( $row['content'] );
        $category = mysql_real_escape_string( $row['category'] );


        $valuesArr[] = "('$title', '$content', '$category')";
    }

    $sql .= implode('title',  'content' , 'category' , $valuesArr);

    mysql_query($sql) or exit(mysql_error()); 
}
?>

它表示未选择数据库

第二次更新

<?php
$servername = "localhost";
$username = "xxxx";
$password = "xxxxx";
$db_name = "xxxxx";

// Create connection
$link = mysql_connect($servername, $username, $password, $db_name);

if(!$link){
echo "Failed" .mysql_connect_error();
}
mysql_select_db($db_name);


 $data =   array (
        [1] => array(
            'eng_title' => "john",
            'tel_title' => "john",
            'category' => "john",

        ), 
        [2] => array(
            'eng_title' => "john",
            'tel_title' => "john",
            'category' => "john",
        ), 
        [3] => array(
            'eng_title' => "john",
            'tel_title' => "john",
            'category' => "john",
        ), 
     );

if(is_array($data)){

    $sql = "INSERT INTO `table_a`( `eng_title`, `tel_title`, `category`) values";

    $valuesArr = array();
    foreach($data as $row){

        $eng_title = mysql_real_escape_string( $row['eng_title'] );
        $tel_title = mysql_real_escape_string( $row['tel_title'] );
        $category = mysql_real_escape_string( $row['category'] );


        $valuesArr[] = "('$eng_title', '$tel_title', '$category')";
    }

   $sql .= implode(',', $valuesArr);

    mysql_query($sql) or exit(mysql_error()); 
}



?>

2 个答案:

答案 0 :(得分:4)

您在分配到$valuesArr[]时未使用变量,因为您忘记了$前缀。它应该是:

$valuesArr[] = "('$title', '$content', '$category')";

您的&#34;没有选择数据库的原因&#34;错误是因为您从未调用mysql_select_db()

$link = mysql_connect($servername, $username, $password);

if(!$link){
    die("Failed" .mysql_connect_error());
}
mysql_select_db($db_name);

您无法将数据库作为mysql_connect的参数(您将其与mysqli_connect混淆 - 您真的应该从mysql转换为{{ 1}}或mysqli)。

您的PDO电话错误。它应该是:

implode()

答案 1 :(得分:1)

Session

有更好的机会......