|
Posted by Chris on 08/09/06 21:14
Thanks again Tim,
It's working fine now. Here are my responses to your suggestions:
"Call mysql_error() after mysql_query to see if the query syntax is at
fault, print out
$doc_insert and try running the query by hand in phpmyadmin or mysql cli,
are you getting the expected number of "file $i: Uploaded br>"
messages?"
Wow - do I feel stupid - I had deleted one of the columns in the table, and
had forgotten about it -- so the query just had too many values. Something
so simple gets overlooked after you've been staring at it for so many
hours - thanks for reminding me to check the query directly.
"I did wonder if the problem was inserting '' into the auto_increment
column, the mysql manual says only NULL or 0 will trigger an auto
increment - I've double checked the manual but I'm still not sure if '' is
treated the same as 0. It may not make a difference but would you
try INSERT ... VALUES(NULL, '$file_name'...) just in case."
FYI - the '' seems to work as a NULL. I use it with all of my INSERT
queries The pk is auto-incremented in every case.
"Btw, top posting can make it hard for most people using newsreader clients
to follow the thread - could you put your reply to messages
underneath the previous message. Ta."
Sorry for toppost - a habit I developed from reading my personal messages -
I always know what I sent, so I want to see the response first.
I was also able to pass values for $mtgid and $num_files by rearranging the
elements (forms vs. form processing) then adding hidden fields into the
forms. However, I have noticed that the 'str_replace' that is supposed to
change any spaces in the file name to an underscore doesn't work. Any
suggestions for a method to make this work? Following is my final code for
anyone who is preparing a similar document:
<?php
//set variables for meeting input:
$date = $_POST['mtgdate'];
$team = $_POST['team'];
$project = $_POST['project'];
$subject = $_POST['subject'];
if (isset($_POST['numfiles'])){
$num_files = $_POST['numfiles'];
} else {
$num_files = 1;
}
//check that form has been submitted
if (isset($_POST['addmeeting'])) { //begin check for submission
//insert data into meetings table
mysql_select_db($database_website, $website);
$doc_insert = "INSERT INTO meetings VALUES ('', '$date', '$team',
'$subject', '$project')";
//Confirm if data inserted
$success = mysql_query($doc_insert); //create variable to hold insert
values
$mtgid = @mysql_insert_id(); //create variable for last inserted id
//check if meeting input is successful
if ($success) {
echo "Your ". date('M d, Y', strtotime($date)) . " meeting has been added
to the database. <br />Your meeting number is " .$mtgid .",
<br />keep this number handy for future reference. <br />";
} else {
echo "There was an error adding your meeting to the database, please try
again.";
}//end check if meeting successful
if (isset($_POST['upload'])){ //check if file upload request ?>
Please use the form below to upload your meeting documents.<br /><br />
<h2>Select files to upload:</h2> Files names should not have any spaces,
single quotes, or slashes. These will be replaced with an underscore (_)
character in the file name.<br />
<form method="post" action="<?php $_SERVER['PHP_SELF']?>"
enctype="multipart/form-data">
<input type="hidden" name="numfiles" value="<?php echo $num_files ?>" />
<?php // show the file input fields based on($num_files).
for ($i = 1; $i <= $num_files; $i++) { //begin the for loop?>
<strong>File <?php echo $i ?>:</strong> <input type="file" name="file<?php
echo $i ?>"/>
<strong>Document Title:</strong>(Required - Max 50 characters):
<input type="text" size="30" maxlength="50" name="title<?php echo $i
?>"/><br />
<?php
} //end for loop ?>
<input type="hidden" name="mtgid" value="<?php echo $mtgid ?>" />
<input type="submit" name="upload_form" value="Upload Now!" />
</form>
<?
} else { //else if no upload requested
echo "<a href='new.php'>Add another meeting.</a>";
} //end check for upload request
}//end check if meeting added
if (isset($_POST['upload_form'])){ //if the upload form has been submitted,
then do the upload process
//upload directory.
$upload_dir = "docs/";
$mtgid= $_POST['mtgid'];
$num_files = $_POST['numfiles'];
echo "<h3>Upload results:</h3>";
//do a loop for uploading files based on ($num_files) number of files.
for ($i = 1; $i <= $num_files; $i++) {
//define file variables to hold the values.
$new_file = $_FILES['file'.$i];
$file_name = $new_file['name'];
//to remove spaces and apostrophes from file name we have to replace it
with "_".
$file_name = str_replace(' ', '_', $file_name);
$file_name = str_replace("'", "_", $file_name);
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_tmp = $new_file['tmp_name'];
//define other variables
$title = $_POST['title'.$i];
//check if file selected
if (!is_uploaded_file($file_tmp)) {
//print error message and file number.
echo "File $i: Not selected.<br>";
}else{
//check if file is Already EXISTS.
if(file_exists($upload_dir.$file_name)){
echo "File $i: ($file_name) already exists.<br>";
}else{
// upload the files.
if (move_uploaded_file($file_tmp,$upload_dir.$file_name)) {
//enter file data into database
mysql_select_db($database_website, $website);
$doc_insert = "INSERT INTO meeting_docs VALUES ('', '$file_name',
'$title', '$mtgid')";
$success = mysql_query($doc_insert); //create variable to hold insert
values
if ($success) { //confirm if values added to database
echo "File $i ($file_name) has been uploaded and added to the meeting
docs table.<br>";
echo "File $i: Title: $title<br>";
echo "File $i: Meeting ID: $mtgid<br><br>";
} else {
echo "There was an error adding your meeting document to the database,
please try again.";
}//end check if meeting successful
}else{
echo "File $i: ($file_name) failed to upload.<br>";
}//end of (move_uploaded_file).
}//end of (file_exists).
}//end of (!is_uploaded_file).
}//end of (for loop).
} //end upload process
?>
Chris
Navigation:
[Reply to this message]
|