我设法将上传的照片插入到数据库以及指定的路径中,问题是我上传的照片没有插入到日志记录用户列而是创建了另一列,我错过了什么?下面是我的代码和截图,以防你们几乎不明白我的意思
MODEL
function create($data){
$query = $this->db->insert('dosen', $data);
return $query;
}
CONTROLLER
public function Upload(){
$upload = $this->input->post('fotoDsn');
//Foto Set
$photoName = gmdate("d-m-y-H-i-s", time()+3600*7).".jpg";
$config['upload_path'] = './assets/';
$config['allowed_types'] = 'gif||jpg||png';
$config['max_size'] = '2048000';
$config['file_name'] = $photoName;
$this->load->library('upload',$config);
if($this->upload->do_upload('userfile')){
$upload = 1;
}
else{
$upload = 2;
}
if($upload==1){
$data = array(
'foto_dosen'=>$photoName);
$insert = $this->MDosen->create($data);
if($insert){
echo 1;
}else{
echo 2;
}
}//else kalo gagal
}
查看
<a href="#modalUpload" class="upload waves-effect waves-light modal-trigger" id="<?php echo $result->id ?>"><i class="material-icons tiny">assignment_ind</i> Upload</a>
<div id="modalUpload" class="modal" style="width: 40%; height: auto">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<i class="medium material-icons prefix">assignment_ind</i>
<h4 class="modal-title"> Upload Foto</h4>
</div>
<div class="modal-body">
<form action="upload" id="tambahFormUpload" method="post" enctype="multipart/form-data">
<div class="file-field input-field">
<div class="btn">
<span>File</span>
<input type="file" name="userfile">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" >Simpan</button>
</div>
</form>
</div>
</div>
</div>
</div>
JS
$('.upload').click(function(){
id1 = $(this).attr('id');
});
$('#tambahFormUpload').submit(function(e){
alert(id1);
e.preventDefault();
var formData = new FormData($(this)[0]);
$.ajax({
url:'Profil/Upload/'+id1,
data:formData,
type:'POST',
contentType: false,
processData: false,
success:function(data){
$("#modalUpload").hide();
window.location.reload(true);
}
});
});
答案 0 :(得分:0)
基于@Sean注释,确实使用public JSONObject makeHttpRequest2(String url2 , String method,
String name, String password) throws IOException {
// Making HTTP request
try {
// check for request method
if (method == "POST") {
// request method is POST
url = new URL(url2);
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Host", "android.schoolportal.gr");
conn.connect();
JSONObject jsonParam = new JSONObject();
jsonParam.put("name", name);
//jsonParam.put("email", email);
jsonParam.put("password", password);
Log.d("json",String.valueOf(jsonParam));
OutputStreamWriter out=new OutputStreamWriter(
conn.getOutputStream());
out.write(jsonParam.toString());
out.close();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
try {
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
Log.d("json3",String.valueOf(json));
// return JSON String
return jObj;
}
,我们应该使用insert
,所以正确的控制器将使用update
模型,而不是{{1然后把参数放在我的函数上,第二个虽然没有插入正确的列,因为无法找到用户的id(update
子句),所以create
我们可以设置where
子句并将其指向update
where
,希望此帮助
<强> CONTROLLER 强>
user
<强> MODEL 强>
id