輸出的數組如何寫入資料庫?

來源:互聯網
上載者:User
輸出的數組如何寫入資料庫?
2個值要寫入2個欄位.
Array ( [0] => upload/2015/09/06/20150906164734000.jpg [1] => upload/2015/09/06/20150906164734001.jpg


回複討論(解決方案)

1、遍曆數組,每次插入一個元素
2、串連成串後插入

怎麼做是根據你的需要來的,並無一定之規
你是程式的主人,而不是程式的奴隸

foreach($arr as $v){    mysql_query("insert into tbname (image_url) values ('".$v."')");}

0 對應 第一個 1 對應第二個 要看你是怎麼設計的....

foreach($arr as $v){    mysql_query("insert into tbname (image_url) values ('".$v."')");}



我說詳細一點吧。因為新手,請版主多多指教。
upload.php頁面
error(99);    if($path) $this->upload_path = $path;    if($_FILES && $this->timetree) {        $this->upload_path .= date('/Y/m/d');        if(! file_exists($this->upload_path)) mkdir($this->upload_path, 0666, true);    }    foreach($_FILES as $info) {        if(! is_array($info['name'])) {            $this->upload_callback($info);            continue;        }        for($i=0;$iupload_callback(array(                'name' => $info['name'][$i],                'type' => $info['type'][$i],                'tmp_name' => $info['tmp_name'][$i],                'error' => $info['error'][$i],                'size' => $info['size'][$i],                ));        }    }  }   /**   * 上傳處理回調方法   * 功能 儲存上傳檔案   **/  function upload_callback($info) {    if($info['error']) return $this->error($info['error']);    if(!($ext = $this->extension($info['name']))) return;      $t= date('YmdHis');    $n = 0;      do {           $filename = sprintf('%s/%s%03d.%s', $this->upload_path, $t, $n++, $ext);    }while(file_exists($filename));      copy($info['tmp_name'], $filename);      $this->upload_file[] = $filename;  }  function extension($filename) {    $t = strtolower(pathinfo($filename, PATHINFO_EXTENSION));    if(in_array($t, $this->allow)) return $t;    $this->error("$t 非法的類型");    return '';  }  /**   * 錯誤處理   **/  function error($errno) {    $msg = '';      switch($errno) {          case UPLOAD_ERR_INI_SIZE:              $msg = '上傳的檔案超過了 '.ini_get('upload_max_filesize');              break;          case UPLOAD_ERR_FORM_SIZE:              $msg = '上傳檔案的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值';              break;          case UPLOAD_ERR_PARTIAL:              $msg = '檔案只有部分被上傳';              break;        case UPLOAD_ERR_NO_FILE:            $msg = '沒有檔案被上傳';            break;        case UPLOAD_ERR_NO_TMP_DIR:            $msg = '找不到臨時檔案夾';            break;        case UPLOAD_ERR_CANT_WRITE:            $msg = '檔案寫入失敗';            break;          default:              $msg = '錯誤:'.$errno;              break;      }    echo "";  }}$p = new upload;print_r($p->upload_file);?>



上傳並返回的頁面up.html
下面這句是返回接收的顯示就是這個內容:Array ( [0] => upload/2015/09/06/20150906164734000.jpg [1] => upload/2015/09/06/20150906164734001.jpg


我現在就是要把這兩個值寫入資料。分別寫入IMG1,IMG 2兩個欄位

$sql = "insert into 表 (IMG1,IMG 2) values('$p->upload_file[0]', '$p->upload_file[1]')";

$sql = "insert into 表 (IMG1,IMG 2) values('$p->upload_file[0]', '$p->upload_file[1]')";

$sql = "insert into 表 (IMG1,IMG 2) values('$p->upload_file[0]', '$p->upload_file[1]')";



非常感謝!同時也謝謝其他幾位版主。

先序列化為字串,然後存進去,取出來之後再序列化,最方便的操作,還保留原結構。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.