help this guy

brillbrill BeginnerLink Clerk
does anyone know?


Hey guys, I'm working on a program for viewing PDF files in Java and I need a system to upload essays in to an SQL database to be pulled by the Java.

I know some basic java but 0 php. A friend has helped me with this basic code and now it is 90% there I just need 1 change.

Right now the dropdown to select which essay(pdf file) submission has no effect. I would like it so that if you choose 'Deadline 1' it uploads the essay into the field "essay" if you choose 'Deadline 2' it uploads into essay2 and etc. Also it would be great if it would not allow the user to upload the essay if the field already contained any data. Thanks for any help.
Here is the code.


<html>
<head>
  <title>Extended Essay Submission</title>
</head>


<body>



<?php

if (isset($_POST['upload'])) {

	if (!isset($_POST['user'])) {

		exit('You must enter a username!');

	}

	

	if (!isset($_POST['password'])) {

		exit('You must enter a password!');

	}

	

	if (!isset($_POST['type'])) {

		exit('You must enter a submission type!');

	}

	

	if (!isset($_FILES['essay'])) {

		exit('You must choose a file to upload!');

	}

	

	$_POST['user'] = addslashes(trim($_POST['user']));

	$_POST['type'] = intval(trim($_POST['type']));

	$_POST['password'] = md5($_POST['password']);



	if (!strlen($_POST['user']) || $_POST['type'] < 1) {

		exit('You must fill in every field on the form!');

	}

	

	if (!$link = mysql_connect('host', 'user', 'pass')) {

		exit('Failed to connect to the database!');

	}

	
	if (!mysql_select_db('database', $link)) {

		exit('Failed to select database!');

	}



	if (!$flink = fopen($_FILES['essay']['tmp_name'], 'rb')) {

		exit('Failed to accept uploaded file!');

	}

	

	if (!$data = fread($flink, $_FILES['essay']['size'])) {

		exit('Failed to read uploaded file!');

	}

	
	$data = addslashes($data);

	

	$upload_sql  = 'UPDATE essays SET essay="'.$data.'"';

	$upload_sql .= ' WHERE username="'.$_POST['user'].'"';

	$upload_sql .= ' AND password="'.$_POST['password'].'"';

	

	if (!$upload_query = mysql_query($upload_sql, $link)

		|| !mysql_affected_rows($link)) {

		exit('File upload failed or already in database!');

	}

	

	echo '<h2>File upload success!</h2><hr />';

}

?>



<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
 Username: <input type="text" name="user" size="25" /><br />
 Password: <input type="password" name="password" size="25" /><br /><br />



 Submission type: 
 <select name="type" size="1">
 <option value="1" selected="selected">Deadline 1</option>
 <option value="2">Deadline 2</option>
 <option value="3">Deadline 3</option>
 <option value="4">Deadline 4</option>
 <option value="5">Deadline 5</option>
 </select>

 <br /><br />

 File: <input type="file" name="essay" /><br /><br />
 <input type="submit" name="upload" value="Upload" />
</form>


</body>
</html>

Comments

  • kinkkink serious member VPS - Virtual Prince of the Server
    i dont know about php but it might be an easy fix to use three different databases .
    from the code i see it already checks for duplicate files and stuff. sorry i cpouldnt do better
Sign In or Register to comment.