Forum Moderators: coopster

Message Too Old, No Replies

check contents of mysql table

before adding a new record

         

coolo

6:54 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



ok, so now that I've figured out how to allow users to upload files (with help!) I'm trying to run a script that will check the name of the uploaded file and determine if that file name already exists in a specific mysql table. Here's what I've got so far:

$filename = $_FILES['uploadfile']['name'];

$query = "SELECT * FROM $table";
$result = mysql_query($query, $conn);

while ($checkfile = mysql_fetch_assoc($results)){
$existfile = $checkfile['filename'];
}//end while

if ($existfile == $filename){
print "A file with that name already exists.";
}
else {
some script that adds the file info to $table
}

Now, this combination of while and if statements will only catch a match with an already existing filename if the already existing filename was the last one to be updated. I've tried adding the if and else statements into the while statement but then the file info gets added to the table multiple times. Does anyone have any ideas? Am I even making any sense?

justageek

7:18 pm on Mar 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change the query to something like this:

$query = "SELECT count(*) as fcount FROM $table where file_field_name_column = '".mysql_escape_string($_FILES['uploadfile']['name'])."'";

Then if the fcount var is less than 0 add the file.

JAG

coolo

5:43 am on Mar 3, 2004 (gmt 0)

10+ Year Member



hey JAG, thanks for the tip. I actually didn't use it, but it pointed me in the right direction on how to do what I wanted. I prolly wouldn't have figured it out without that.

Bigjohn

12:13 pm on Mar 3, 2004 (gmt 0)

10+ Year Member



So, something like this:

$query = "SELECT count(*) as fcount FROM $table where file_field_name_column = '".mysql_escape_string($_FILES['uploadfile']['name'])."'";

could be used to check if an email name and customer name both existed in your database so you did not add a duplicate? Sorry if it's a dumb question, but I'm a serious newbie...

John