Hello,
I'm trying to set up a website where user can upload files to which should be readable by everybody. Logged users should be able to comment on the files and rate the files.
I'm new to MySql and quite new to PHP so it has taken me already lots of hours for not so much result so far.
Here is where I am:
In MySql I've created four tables with MyISAM (members, files, comments, ratings).
I've set up a user registration system which works more or less fine (I've used big parts of this code http://www.html-form-guide.com/php-form/php-login-form.html). The users are registered in my members table with the user id, name, firstname, username, email and password.
Now, I've created a small script to upload files to the files table called addnewtest.php with a form included in a php file called upload.php. I can insert a trace of the files in my files table, but I can't retrieve the user id from the members table.
I have a php script I can include in a page to see whether a user is logged in. Here is a short version of my uploadtest.php
<?PHP
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("login.php");
exit;
}
?>
<!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" xml:lang="en-US" lang="en-US">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<title>test upload site</title>
<link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css"/>
</head>
<body>
<div id='fg_membersite_content'>
<h2>Upload</h2>
Hello <?= $fgmembersite->UserFullName(); ?>!
<form method="post" action="addnewtest.php" enctype="multipart/form-data">
<p>
pls indicate your username
</p>
<p>
username:
</p>
<input type="text" name="username"/>
<p>
upload a file with max. 2MB:
</p>
<p>
file:
</p>
<input type="hidden" name="size" value="20480000">
<input type="file" name="notes">
<p>
your faculty
</p>
<p>
Faculty:
</p>
<select name="faculty">
<option>ABC</option>
<option>DEF</option>
<option>GHI</option>
</select>
<br/>
<br/>
<input TYPE="submit" name="upload" title="Add data to the Database" value="Valider"/>
</form>
</div>
</body>
</html>
the file addnewtest.php looks like
<?php
//This is the directory where files will be saved
$target = "usernotes/";
$target = $target . basename( $_FILES['notes']['name']);
//This gets all the other information from the form
$name=$_POST['username'];
$size=($_FILES['notes']['size']);
$faculty=$_POST['faculty'];
$note=($_FILES['notes']['name']);
$mime=($_FILES['notes']['type']);
// Connects to your Database
mysql_connect("....", "...") or die(mysql_error()) ;
mysql_select_db("...") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO files (username,size,faculty,notes,type)
VALUES ('$name', '$size', '$faculty', '$note', '$mime')" ) ;
//Writes the file to the server
if(move_uploaded_file($_FILES['notes']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else {
//Gives and error if its not
echo "Sorry, there has been a problem with the upload.";
}
?>
the members table has
id_user int(11) auto_increment primary key
name varchar(40)
firstname varchar(40)
email varchar(64)
username varchar(16) unique
password varchar(32)
confirmcode varchar(32)
…
(I don't know if I have to add field here yet to link the tables ?? like
IDFILE to link id_user.members with file_id.files
IDCOM to link id_user.members with comment_id.comments etc)
the files table has
file_id int(11) auto_increment primary key
id_user int(11) (not sure whether this is needed here)
size int(11)
notes varchar(60)
faculty varchar(60)
…
(I have the same problem for the comments table as for my files table (how to retrieve the user id of the logged in user from the members table and I haven't figured out yet how to do the rating as most scripts on the web are for rating a site or comments, but not for rating files on a file system.)
What do I have to do for mySql knows which user has uploaded which file??
How to retrieve the id_user of the members table of the person which is uploading the file??
thanks in advance for your help!!
Roger