Erreur dans le code pour faire un Upload dans mysql
Bonjour a tous,
J'ai creer un fichier.php s'appele add.php qui sert a ajouter l'id dans l'url a travers une fonction javascript et on passe vers addFile.php.
Cette page sert a ajouter l'id d'un cours avec un fichier (upload) dans mysql.
dans cette page, l'url est: .../addFile.php?courseId=1
Lorsque je "BROWSE" un fichier et on cliquant sur Bouton upload j'ai eu une erreur: Undefined varible: courseId.
Alors comment je peut envoyer de nouveaux le course Id dans le fichier upload.php.
Le code est le suivant:
La premiere page addFile.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| <?
session_start();
if(!session_is_registered(myusername)){
header("location:index.html");
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>File Upload</title>
<style type="text/css">
.c {
text-align: center;
}
</style>
</head>
<body>
<p> </p>
<p>
<form action="upload.php" method="POST" enctype="multipart/form-data" class="c">
<p><img src="../files/earth-upload-icon.jpg" width="168" height="168"></p>
<p>
Course ID:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="***"; // Database name
$tbl_name="***"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
session_start();
$username=$_SESSION['myusername'];
//echo $_SESSION['myusername']; // will echo out the username
$courseId = $_REQUEST['courseId'];
echo $courseId;
$action=$_REQUEST['action'];
?>
;
Course Action: <?php echo $action ?>;
</p>
</p>
<p> File:
<input type="file" name="file"><br>
<input type="Submit" id="submitId" value="Upload File">
</p>
</form>
</body>
</html> |
La bouton upload:
Upload.php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="***"; // Database name
$tbl_name="***"; // Table name
//This is the directory where files will be saved
$target = "C:\wamp\www\***\upload\upload";
$target = $target . basename( $_FILES['file']['name']);
//This gets all the other information from the form
$courseId=$_REQUEST['courseId'];
$file=($_FILES['file']['name']);
$action=$_GET['action'];
// Connects to your Database
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
session_start();
$username=$_SESSION['myusername'];
//Writes the information to the database
mysql_query("INSERT INTO $tbl_name(course_id, file, action) VALUES ('$courseId', '$file', '$action')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?> |