Page 1 of 1

Populate MySQL DB Question

Posted: Mon Jun 26, 2006 4:58 am
by JimiH
Hi I have a form with 2 drop down lists, a description text box and a file upload textbox with a browse button.

I select the first catagory from the first dropdown which populates the second catagory selection list.

Then I browse for a file using the browse button which populates the file textbox with the location.

Then I fill out the description box and press submit.

Code: Select all

<?php

    include 'config.php';
    include 'opendb.php';

///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category"); 
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory"); 
}else{$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); } 
////////// end of query for second subcategory drop down list box ///////////////////////////

echo "<form method=post name=f1 action='dd-check.php'>";


/// Add your form processing page address to action in above line. Example  action=dd-check.php////
//////////        Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo  "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";
//////////////////  This will end the first drop down list ///////////

//////////        Starting of second drop downlist /////////
echo "<select name='subcat'><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) { 
echo  "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";
//////////////////  This will end the second drop down list ///////////
//// Add your other form fields as needed here/////




echo "<input type=submit value=Submit>";
echo "</form>";

///////////  Start of upload Documents code  ////////////////////////////

// you can change this to any directory you want
// as long as php can write to it

$uploadDir = 'C:/websites/user_login/uploads/'; 


echo "Select your story <b>$session->username</b>, then click upload. <br>";

if(isset($_POST['upload']))
{
    $fileName = $_FILES['userfile']['name'];
    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];

// get the file extension first
	$ext      = substr(strrchr($fileName, "."), 1); 
	
	// generate the random file name
	$randName = md5(rand() * time());
	
	// and now we have the unique file name for the upload file
    $filePath = $uploadDir . $randName . '.' . $ext;


    // the files will be saved in filePath 
   // $filePath = $uploadDir . $fileName;



    // move the files to the specified directory
    // if the upload directory is not writable or
    // something else went wrong $result will be false
    $result    = move_uploaded_file($tmpName, $filePath);
    if (!$result) {
        echo "Error uploading file";
        exit;
    }
    
   
	

    if(!get_magic_quotes_gpc())
    {
        $fileName  = addslashes($fileName);
        $filePath  = addslashes($filePath);

    }  


	
    
    echo "<br>File uploaded<br>";
/* Link back to main */
echo "<br>Back To [<a href=\"main.php\">Main</a>]<br>";

}        

///////////////////////////////// End of document upload  ////////////////////////



?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<link rel="stylesheet" type="text/css" href="/styles/yourfile.css"> 
<title>Upload File To MySQL Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">

  <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr> 
      <td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile">
         </td>
     
    </tr>
  </table>


</head>

<SCRIPT language=JavaScript>
function reload(form)

{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dd.php?cat=' + val ;
}

</script>

<body>


Decription:<input type="text" name="description"><br>


<?

?>
<center><a href='http://www.plus2net.com'>PHP SQL HTML free tutorials and scripts</a></center> 
</body>

</html>
<html> 
<head> 
<title>Personal INFO</title> 
</head> 
<body>

This information is passed to the DB using dd-check.php

Code: Select all

<?php

error_reporting(E_ALL); 

include 'config.php';
    include 'opendb.php';

$description=$_POST['description'];
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

	

    $query = "INSERT INTO upload2 (name, size, type, path, description, AuthorID, category, subcategory ) ".
             "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$description', '$session->username', '$cat', '$subcat')";

    mysql_query($query) or die('Error, query failed : ' . mysql_error());  

	
echo $query;

    include 'closedb.php';
	
    
echo "<br>Back To [<a href=\"main.php\">Main</a>]<br>";
    echo "<br>File uploaded<br>";
/* Link back to main */

    

?>
When I hit the "Submit Button" I get the following messages

Code: Select all

INSERT INTO upload2 (name, size, type, path, description, AuthorID, category, subcategory ) VALUES ('', '', '', '', '', '', '2', 'Blue') 
Back To [Main]

File uploaded
PHP Notice: Undefined index: description in C:\websites\user_login\upload2\dd-check.php on line 8 PHP Notice: Undefined index: userfile in C:\websites\user_login\upload2\dd-check.php on line 9 PHP Notice: Undefined index: userfile in C:\websites\user_login\upload2\dd-check.php on line 10 PHP Notice: Undefined index: userfile in C:\websites\user_login\upload2\dd-check.php on line 11 PHP Notice: Undefined index: userfile in C:\websites\user_login\upload2\dd-check.php on line 12 PHP Notice: Undefined variable: filePath in C:\websites\user_login\upload2\dd-check.php on line 17 PHP Notice: Undefined variable: session in C:\websites\user_login\upload2\dd-check.php on line 17 PHP Notice: Trying to get property of non-object in C:\websites\user_login\upload2\dd-check.php on line 17
As you can see only the "catagory" & the subcatagory have been passed to the DB.

Can anyone tell mw why, I'm a total novice at PHP as you can see.

Geoff

Posted: Mon Jun 26, 2006 5:28 am
by JayBird
Where is your <form> tag?

You need to add

Code: Select all

enctype="multipart/form-data"
When uploading files

i.e.

Code: Select all

<form action="" method="post" enctype="multipart/form-data">

Posted: Mon Jun 26, 2006 6:19 am
by JimiH
Thanks

Code: Select all

<?php

    include 'config.php';
    include 'opendb.php';

///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category"); 
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory"); 
}else{$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); } 
////////// end of query for second subcategory drop down list box ///////////////////////////

echo "<form method=post name=f1 action='dd-check.php'>";


/// Add your form processing page address to action in above line. Example  action=dd-check.php////
//////////        Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo  "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";
//////////////////  This will end the first drop down list ///////////

//////////        Starting of second drop downlist /////////
echo "<select name='subcat'><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) { 
echo  "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";
//////////////////  This will end the second drop down list ///////////
//// Add your other form fields as needed here/////




echo "<input type=submit value=Submit>";
echo "</form>";

///////////  Start of upload Documents code  ////////////////////////////

// you can change this to any directory you want
// as long as php can write to it

$uploadDir = 'C:/websites/user_login/uploads/'; 


echo "Select your story <b>$session->username</b>, then click upload. <br>";

if(isset($_POST['upload']))
{
    $fileName = $_FILES['userfile']['name'];
    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];

// get the file extension first
	$ext      = substr(strrchr($fileName, "."), 1); 
	
	// generate the random file name
	$randName = md5(rand() * time());
	
	// and now we have the unique file name for the upload file
    $filePath = $uploadDir . $randName . '.' . $ext;


    // the files will be saved in filePath 
   // $filePath = $uploadDir . $fileName;



    // move the files to the specified directory
    // if the upload directory is not writable or
    // something else went wrong $result will be false
    $result    = move_uploaded_file($tmpName, $filePath);
    if (!$result) {
        echo "Error uploading file";
        exit;
    }
    
   
	

    if(!get_magic_quotes_gpc())
    {
        $fileName  = addslashes($fileName);
        $filePath  = addslashes($filePath);

    }  

    echo "<br>File uploaded<br>";
/* Link back to main */
echo "<br>Back To [<a href=\"main.php\">Main</a>]<br>";

}        

///////////////////////////////// End of document upload  ////////////////////////



?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<link rel="stylesheet" type="text/css" href="/styles/yourfile.css"> 
<title>Upload File To MySQL Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
<form action="" method="post" enctype="multipart/form-data" name="uploadform">

  <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr> 
      <td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile">
         </td>
    
    </tr>
  </table>

</head>

<SCRIPT language=JavaScript>
function reload(form)

{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dd.php?cat=' + val ;
}

</script>

<body>


Decription:<input type="text" name="description"><br>


<?

?>


I've added that line back again (to much copy & pasting from the net)

Can you see where I'm going wrong?

Geoff

Posted: Mon Jun 26, 2006 7:42 am
by Verminox
May I ask what the form and table are doing in the head of your document?

Also, you need to specify the filename of the PHP file that is handling the data under the action attribute. For example if 'process.php' is processing the $_POST data, then use:

Code: Select all

<form action="process.php" method="post" enctype="multipart/form-data">
And make sure to close the form tag with a </form> once you are done with it.

Posted: Mon Jun 26, 2006 7:56 am
by JayBird
Verminox wrote:Also, you need to specify the filename of the PHP file that is handling the data under the action attribute.
You dont have to, not specifying an action will submit the form to itself usually

Posted: Mon Jun 26, 2006 8:10 am
by Verminox
Pimptastic wrote:
Verminox wrote:Also, you need to specify the filename of the PHP file that is handling the data under the action attribute.
You dont have to, not specifying an action will submit the form to itself usually
I think the first post mentions that the form should go to 'dd-check.php', which is why I brought that up.

Posted: Mon Jun 26, 2006 8:13 am
by JayBird
Verminox wrote:
Pimptastic wrote:
Verminox wrote:Also, you need to specify the filename of the PHP file that is handling the data under the action attribute.
You dont have to, not specifying an action will submit the form to itself usually
I think the first post mentions that the form should go to 'dd-check.php', which is why I brought that up.
Just testing ya ;)

Posted: Mon Jun 26, 2006 8:38 am
by JimiH
Verminox wrote:May I ask what the form and table are doing in the head of your document?

Also, you need to specify the filename of the PHP file that is handling the data under the action attribute. For example if 'process.php' is processing the $_POST data, then use:

Code: Select all

<form action="process.php" method="post" enctype="multipart/form-data">
And make sure to close the form tag with a </form> once you are done with it.
Thanks, I put dd-check.php in there and this was the result

Code: Select all

INSERT INTO upload2 (name, size, type, path, description, AuthorID, category, subcategory ) VALUES ('ARFORM.XLS', '65536', 'application/vnd.ms-excel', '', 'uuuuuuuuuuu', '', '', '') 
Back To [Main]

File uploaded
PHP Notice: Undefined variable: filePath in C:\websites\user_login\upload2\dd-check.php on line 17 PHP Notice: Undefined variable: session in C:\websites\user_login\upload2\dd-check.php on line 17 PHP Notice: Trying to get property of non-object in C:\websites\user_login\upload2\dd-check.php on line 17 PHP Notice: Undefined variable: cat in C:\websites\user_login\upload2\dd-check.php on line 17 PHP Notice: Undefined variable: subcat in C:\websites\user_login\upload2\dd-check.php on line 17
So the "Upload" button enters "name, size, type, path, description, AuthorID," into the DB
& the "Submit" button enters the "category, subcategory" into the DB, I really only want one button that will enter the lot!

Any ideas?

Geoff