Jump to content

Uploading files help!


f4kev

Recommended Posts

I am trying to upload files to a database in mysql.  I am a total beginner and I can't seem to figure it out.  Here is my code which is missing code that uploads files because I have know idea.  Any help would be appreciated.

 

Form code: 

<!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Assignment Upload</title>

<link rel="stylesheet" href="projectstyle.css" type="text/css" />

<meta http-equiv="content-type"

content="text/html; charset=iso-8859-1" />

</head>

<body>

 

<script type='text/javascript'>

 

function formValidator(){

var username = document.getElementById('studentid');

var password = document.getElementById('assignmentname');

 

if(isAlphabet(username, "Please enter a valid student ID")){

if(isAlphanumeric(password, "Please enter a valid assignment name")){

    return true;

    }

    }

 

return false;

 

function isAlphabet(elem, helperMsg){

var alphaExp = /^[a-z A-Z]+$/;

if(elem.value.match(alphaExp)){

return true;

}else{

alert(helperMsg);

elem.focus();

return false;

}

}   

 

function isAlphanumeric(elem, helperMsg){

var alphaExp = /^[0-9a-zA-Z]+$/;

if(elem.value.match(alphaExp)){

return true;

}else{

alert(helperMsg);

elem.focus();

return false;

}

}

 

 

</script>

 

<h1>Assignment Uploader</h1><hr />

<form>

<input type="Button" value="Back" onclick="history.back()">

</form>

 

<table>

<tr>

<td>

<a href="HomeWorkSubmitter.html">Home</a>

</td>

<td>

<a href="ViewAssignments2.php">ViewAssignments</a>

</td>

<td>

<a href="HomeWorkSubmitter.html">Logout</a>

</td>

</tr>

</table>

 

<table>

<tr>

<td>

<p>Upload your assignment below</p>

<form action="UploadAssignment.php" method="post" enctype="multipart/form-data" onsubmit='return formValidator()'>

<h2>Assignment:</h2>

<input type="hidden" name="MAX_FILE_SIZE" value="2000000">

<p>Student ID: <input type="text" name="studentid" id="studentid" size="30" /><p>

<p>Assignment Name:  <input type="text" name="assignmentname" id="assignmentname" size="30" /></p>

<p>Upload File:  <input name="userfile" type="file" /></p>

<p>Comments:</p>

<p><textarea rows="5" cols="30" name="comments"></textarea></p>

<p><input type="submit" value="Submit" name="upload" id="upload" /></p>

</form>

</td>

</tr>

</table>

<table>

<tr>

<td>

<a href="HomeWorkSubmitter.html">Home</a>

</td>

<td>

<a href="ViewAssignments2.php">ViewAssignments</a>

</td>

<td>

<a href="HomeWorkSubmitter.html">Logout</a>

</td>

</tr>

</table>

</body>

</html>

 

PHP code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>UploadAssignment</title>

<link rel="stylesheet" href="projectstyle.css" type="text/css" />

</head>

 

<body>

<?php

if (empty($_POST['studentid']) || empty($_POST['assignmentname']))

die("<p>You must enter your student ID and assignment name! Click your

browser's back button to return to the sign in form.</p>");

 

$DBConnect = @mysqli_connect("*******", "***", "******")

Or die("<p>Unable to connect to the database server.</p>"

. "<p>Error code " . mysqli_connect_errno()

. ": " . mysqli_connect_error()) . "</p>";

 

$DBName = "***";

if (!@mysqli_select_db($DBConnect, $DBName)) {

$SQLstring = "CREATE DATABASE $DBName";

$QueryResults = @mysqli_query($DBConnect, $SQLstring)

Or die("<p>Unable to execute the query.</p>"

. "<p>Error code " . mysqli_errno($DBConnect)

. ": " . mysqli_error($DBConnect)) . "</p>";

echo "<p>Thank you!</p>";

mysqli_select_db($DBConnect, $DBName);

}

 

$TableName = "submitted_assignments";

$SQLstring = "SELECT * FROM $TableName";

$QueryResult = @mysqli_query($DBConnect, $SQLstring);

if (!$QueryResult) {

$SQLstring = "CREATE TABLE $TableName (studentid VARCHAR(10), assignmentid VARCHAR(10),

assignment_file VARCHAR(50), assignment_comments VARCHAR(500), assignment_grade INT)";

$QueryResult = @mysqli_query($DBConnect, $SQLstring)

Or die("<p>Unable to create the table.</p>"

. "<p>Error code " . mysqli_errno($DBConnect)

. ": " . mysqli_error($DBConnect)) . "</p>";

}

 

$StudentID = addslashes($_POST['studentid']);

$Assignment = addslashes($_POST['assignmentname']);

$File = addslashes($_POST['userfile']);

$Comments = addslashes($_POST['comments']);

$SQLstring = "INSERT INTO $TableName VALUES('$StudentID', '$Assignment',

'$File', '$Comments', 'NULL')";

$QueryResult = @mysqli_query($DBConnect, $SQLstring)

Or die("<p>Unable to execute the query.</p>"

. "<p>Error code " . mysqli_errno($DBConnect)

. ": " . mysqli_error($DBConnect)) . "</p>";

echo "<h1>Your assignment has been sent to the instructor!</h1>";

mysqli_close($DBConnect);

 

 

?>

</body>

</html>

 

Link to comment
https://forums.phpfreaks.com/topic/78968-uploading-files-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.