java - Error in moving variables from android class to php side -
i developing app , have problem in transferring variables android php script. connection established. appreciated.
here java code.
// open url connection servlet fileinputstream fileinputstream = new fileinputstream(sourcefile); url url = new url(uploadserveruri); // open http connection url conn = (httpurlconnection) url.openconnection(); conn.setdoinput(true); // allow inputs conn.setdooutput(true); // allow outputs conn.setusecaches(false); // don't use cached copy conn.setrequestmethod("post"); conn.setrequestproperty("connection", "keep-alive"); conn.setrequestproperty("enctype", "multipart/form-data"); conn.setrequestproperty("content-type", "multipart/form-data;boundary=" + boundary); conn.setrequestproperty("uploaded_file", filename); dos = new dataoutputstream(conn.getoutputstream()); dos.writebytes(twohyphens + boundary + lineend); dos.writebytes("content-disposition: form-data; name=\"uploaded_file\";filename=\"" + filename + "\"" + lineend); dos.writebytes(lineend); dos.writebytes(twohyphens + boundary + lineend); dos.writebytes("content-disposition: form-data; name=\"uploaded_file\";name=\"" + username + "\"" + lineend); dos.writebytes(lineend); // create buffer of maximum size bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); buffer = new byte[buffersize]; // read file , write form... bytesread = fileinputstream.read(buffer, 0, buffersize); while (bytesread > 0) { dos.write(buffer, 0, buffersize); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); bytesread = fileinputstream.read(buffer, 0, buffersize); } // send multipart form data necesssary after file data... dos.writebytes(lineend); dos.writebytes(twohyphens + boundary + twohyphens + lineend);
and php script
<?php require("config.inc.php"); $file_path = "uploads/"; $file_path = $file_path . basename( $_files['uploaded_file']['filename']); $user = $_files['username']; if(move_uploaded_file($_files['uploaded_file']['tmp_name'], $file_path)) { $query = "insert users ( username, password, picture ) values ( '1234567', $user, $file_path ) "; $stmt = $db->prepare($query); $stmt->execute(); echo "success"; } else{ echo "fail"; } ?>
the problem in section of setting values , variables moved empty. in database nothing being added.
the reason why fails because did not prepare query properly:
$query = "insert users ( username, password, picture ) values ( ?, ?, ? ) "; $stmt = $db->prepare($query); $stmt->execute(array('1234567', $user, $file_path));
Comments
Post a Comment