如何在一个具有不同ID的字段中插入多个文本框值? [英] How to insert multiple text box value in one field with different different id?

查看:81
本文介绍了如何在一个具有不同ID的字段中插入多个文本框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.它仅插入最后一个元素值.我想将所有值插入具有不同ID的一个字段中,并将所有数据插入不同行中.

Here is my code. It is insert only last element value. I want to insert all value in one field with different id and all data insert in different different row.

    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;
}

此代码仅插入像这样的最后一个值

this code insert only last value like this

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;
}

}

插入功能

       if (!function_exists('mysql_insert_multiple')) {

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;
}

}

推荐答案

更改要插入的sql:

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("', '", array_values($row)) . "') ";
    $sql.=implode(',', $tempstr);
    $sql.=";";
    $result = mysql_query($sql, $db) or die(mysql_error());
  }

  return $result;
}

如果您的$ column数组的格式为:

This will work if your $column array is in the format:

$columns = array(
    0 => array("name" => 'slider'
     "value" => $user_slider.
    1 => 
       .
       .
       .

所以我认为您也需要更改$ column数组.

So I think you need to change your $column array as well.

这篇关于如何在一个具有不同ID的字段中插入多个文本框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆