輸出的數組如何寫入資料庫?
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]')";
非常感謝!同時也謝謝其他幾位版主。
先序列化為字串,然後存進去,取出來之後再序列化,最方便的操作,還保留原結構。