如何在一个具有不同ID的字段中插入多个数据?

时间:2016-01-29 07:32:15

标签: php mysql phpmyadmin

这是我的代码。它只插入最后一个元素值。我想在一个具有不同id的字段中插入所有值,并将所有数据插入到不同的不同行中。

    public function update($user_slider, $user_welcomebox, $user_servicebox, $user_postbox, $user_testimonials, $user_welcomemessage, $user_welcomededcription, $user_welcomelink, $user_welcomelinktitle, $user_servicetitle, $user_totalservicedisplay, $user_ourservice, $user_blogtitle, $user_totalblogdisplay,$user_ourblog, $user_newstitle, $user_totalnewsdisplay, $user_ournews, $user_totaltestimonialdisplay, $user_ourtestimonial) {
    $db = connectionstart();
     $currentdatetime = date("Y-m-d H:i:s", time());
        $currentgmtdatetime = gmdate("Y-m-d H:i:s", time() - (5 * 60 * 60));
    $sql = "select * from user_options ";
    $result = mysql_evaluate($db, $sql, 0);
    $columns = array(
        "name" => 'slider'
            , "value" => $user_slider
        ,"name" => 'welcomebox'
            , "value" => $user_welcomebox
        ,"name" => 'servicebox'
            , "value" => $user_servicebox
        ,"name" => 'postbox'
            , "value" => $user_postbox
        ,"name" => 'testimonials'
            , "value" => $user_testimonials
        ,"name" => 'welcomemessage'
            , "value" => $user_welcomemessage
        ,"name" => 'welcomededcription'
            , "value" => $user_welcomededcription
        ,"name" => 'welcomelink'
            , "value" => $user_welcomelink
        ,"name" => 'welcomelinktitle'
            , "value" => $user_welcomelinktitle
        ,"name" => 'servicetitle'
            , "value" => $user_servicetitle
        ,"name" => 'totalservicedisplay'
            , "value" => $user_totalservicedisplay
        ,"name" => 'ourservice'
            , "value" => $user_ourservice
        ,"name" => 'blogtitle'
            , "value" => $user_blogtitle
        ,"name" => 'totalblogdisplay'
            , "value" => $user_totalblogdisplay
        ,"name" => 'ourblog'
            , "value" => $user_ourblog
        ,"name" => 'newstitle'
            , "value" => $user_newstitle
        ,"name" => 'totalnewsdisplay'
            , "value" => $user_totalnewsdisplay
        ,"name" => 'ournews'
            , "value" => $user_ournews
        ,"name" => 'totaltestimonialdisplay'
            , "value" => $user_totaltestimonialdisplay
        ,"name" => 'ourtestimonial'
            , "value" => $user_ourtestimonial
        , "entrydate" => $currentdatetime

        );
    if ($result != 0) {
        $result = mysql_update($db, "user_options", $columns, "");
        if ($result == true) {
            $result = 'SUCCESS';
        }
    } else {
        $result = mysql_insert($db, "user_options", $columns, "");
        if ($result == true) {
            $result = 'SUCCESS';
        }
    }
    connectionclose($db);
    return $result;
}

这段代码只插入像这样的最后一个值

id          name             value
 1          testimonials      5

我想插入像这样的数据

id      name               value
 1      slider               0
 2      welcome box          1
 3      welcome message      hi

更新功能

     if (!function_exists('mysql_update')) {

function mysql_update($db, $table, $columns, $where = "") {
    $sql = "UPDATE `" . $table . "` SET";
    $arry = array();
    foreach ($columns as $field => $value) {
        array_push($arry, " `" . $field . "`='" . $value . "' ");
    }
    // implode keys of $array...
    $sql .= implode(",", $arry);
    if (isset($where) && $where != "") {
        $sql .= " " . $where;
    }
    $result = mysql_query($sql, $db) or die(mysql_error());
    return $result;
}

}

插入功能

     function mysql_insert($db, $table, $columns) {
    $sql = "INSERT INTO `" . $table . "` ";
    // implode keys of $array...
    $sql .= " (`" . implode("`, `", array_keys($columns)) . "`)";
    // implode values of $array...
    $sql .= " VALUES ('" . implode("', '", $columns) . "') ";

    $result = mysql_query($sql, $db) or die(mysql_error());
    return $result;
}

3 个答案:

答案 0 :(得分:0)

更改您的$列,使其成为

$columns = array(
            "slider" => $user_slider,
            "welcomebox" => $user_welcomebox,
            ...
            "ourtestimonial" => $user_testimonial);

function mysql_insert($db, $table, $columns) {
    $sql = "INSERT INTO `" . $table . "` ";
    $sql .= " (`" . implode("`, `", array_keys($columns)) . "`)";
    $sql .= " VALUES ('" . implode("', '", array_values($columns)) . "') ";

    $result = mysql_query($sql, $db) or die(mysql_error());
    return $result;
}

答案 1 :(得分:0)

哦对不起,我错过了你的表架构。

$columns = array( 
             array( "name" => "slider", "value"=>$user_slider),
             array( "name" => "welcomebox", "value"=>$user_welcomebox),
             ...
             ); 

然后你需要

function map($item) {
    return "(".$item['name'] . "," . $item['value']. ")";
}

function mysql_insert($db, $table, $columns) {
    $sql = "INSERT INTO `" . $table . "` ";
    $sql .= " (`name`, `value`) ";
    $sql .= " VALUES ('" . implode("', '", array_map("map", $columns)) . "') ";

    $result = mysql_query($sql, $db) or die(mysql_error());
return $result;

}

答案 2 :(得分:0)

如果我使用这个功能那么这个功能有什么样的改变

 function mysql_insert_multiple($db, $table, $columns) {
    $sql = "INSERT INTO `" . $table . "` ";
    // implode keys of $array...
    $sql .= " (`" . implode("`, `", array_keys($columns[0])) . "`)";
    // implode values of $array...
    $sql .= " VALUES ";
    $tempstr = array();
    foreach ($columns as $row) {
        $tempstr[] = " ('" . implode("', '", $row) . "') ";
    }
    $sql.=implode(',', $tempstr);
    $sql.=";";
    $result = mysql_query($sql, $db) or die(mysql_error());
    return $result;