• 0
hugolima

Como posso tratar melhor meu codigo If else no meu update de arquivos, pois pretendo colocar mais dois campos de doc

Question

<?	
// -------- UPDATE ------------------	

	case 'update':
		$id = $_POST['id'];
		$titulo = $_POST['titulo'];
		$doc1 = $_POST['doc1'];
		$doc2 = $_POST['doc2'];


		// Pasta Documento(s)
		$upload_dir = '../../../upload/documentos/';
		
		// Se Upload for só o doc1 ------------------------------------------------------------------------------------------------------------------------------------------//
		if ($doc1 == "" and $doc2 <> "") {
			$doc1= $_FILES['doc1']['name'];
			$upload_file1 = $upload_dir . $_FILES['doc1']['name'];
			move_uploaded_file($_FILES['doc1']['tmp_name'], $upload_file1);
			
		$sql = "UPDATE tb_documentos SET titulo=:titulo, doc1=:doc1, doc2=:doc2, WHERE id=:id";
		$vCampos = array('titulo'=>$titulo, 'doc1'=>$doc1, 'doc2'=>$doc2, 'id'=>$id);
		$DB->Execute($sql, $vCampos);
			
			
			
		// Se Upload for só o doc2 ------------------------------------------------------------------------------------------------------------------------------------------//	
		} elseif ($doc1 <> "" and $doc2 == "") {
			$doc2= $_FILES['doc2']['name'];
			$upload_file2 = $upload_dir . $_FILES['doc2']['name'];
			move_uploaded_file($_FILES['doc2']['tmp_name'], $upload_file2);
			
			
		$sql = "UPDATE tb_documentos SET titulo=:titulo, doc1=:doc1, doc2=:doc2, WHERE id=:id";
		$vCampos = array('titulo'=>$titulo, 'doc1'=>$doc1, 'doc2'=>$doc2, 'id'=>$id);
		$DB->Execute($sql, $vCampos);
			
			
		// Se Upload for doc1 e doc2 ------------------------------------------------------------------------------------------------------------------------------------------//
		} elseif ($doc1 == "" and $doc2 == "") {
			$doc1= $_FILES['doc1']['name'];
			$doc2= $_FILES['doc2']['name'];
			$upload_file1 = $upload_dir . $_FILES['doc1']['name'];
			$upload_file2 = $upload_dir . $_FILES['doc2']['name'];
			move_uploaded_file($_FILES['doc1']['tmp_name'], $upload_file1);
			move_uploaded_file($_FILES['doc2']['tmp_name'], $upload_file2);
			
			
		$sql = "UPDATE tb_documentos SET titulo=:titulo, doc1=:doc1, doc2=:doc2, WHERE id=:id";
		$vCampos = array('titulo'=>$titulo, 'doc1'=>$doc1, 'doc2'=>$doc2, 'id'=>$id);
		$DB->Execute($sql, $vCampos);	
		
		} else {
		
		// Se não tiver Upload de doc1 e doc2 ------------------------------------------------------------------------------------------------------------------------------------------//		
		
		$sql = "UPDATE tb_documentos SET titulo=:titulo, doc1=:doc1, doc2=:doc2, WHERE id=:id";
		$vCampos = array('titulo'=>$titulo, 'doc1'=>$doc1, 'doc2'=>$doc2, 'id'=>$id);
		$DB->Execute($sql, $vCampos);			
		
		}

		header("location:./");
		exit;
	break;
?>

 

Edited by hugolima

Share this post


Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.