Jump to content

[SOLVED] Upload and MySQL Problem


SkyRanger

Recommended Posts

I am having a problem with a upload an update mysql script, the file will upload with no problems but it will not update the database and I am not sure what the problem is:

 

$upload_dir = "../images/";
//number of files to upload.
$num_files = 1;
//the file size in bytes.
$size_bytes =100000000; //51200 bytes = 50KB.
//Extensions you want files uploaded limited to.
$limitedext = array(".gif",".jpg",".jpeg",".png",".txt",".pdf",".doc",".rtf",".fdr");


   //check if the directory exists or not.
   if (!is_dir("$upload_dir")) {
      die ("Error: The directory <b>($upload_dir)</b> doesn\'t exist");
   }
   //check if the directory is writable.
   if (!is_writeable("$upload_dir")){
      die ("Error: The directory <b>($upload_dir)</b> is NOT writable, Please CHMOD (777)");
   }


//if the form has been submitted, then do the upload process
//infact, if you clicked on (Upload Now!) button.
if (isset($_POST['upload_form'])){

       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 variables to hold the values.
           $new_file = $_FILES['file'.$i];
           $file_name = $new_file['name'];
           //to remove spaces from file name we have to replace it with \"_\".
           $file_name = str_replace(' ', '_', $file_name);
           $file_tmp = $new_file['tmp_name'];
           $file_size = $new_file['size'];

           #-----------------------------------------------------------#
           # this code will check if the files was selected or not.    #
           #-----------------------------------------------------------#

           if (!is_uploaded_file($file_tmp)) {
              //print error message and file number.
              echo "File $i: Not selected.<br>";
           }else{
                 #-----------------------------------------------------------#
                 # this code will check file extension                       #
                 #-----------------------------------------------------------#

                 $ext = strrchr($file_name,'.');
                 if (!in_array(strtolower($ext),$limitedext)) {
                    echo "File $i: ($file_name) Wrong file extension. <br>";
                 }else{
                       #-----------------------------------------------------------#
                       # this code will check file size is correct                 #
                       #-----------------------------------------------------------#

                       if ($file_size > $size_bytes){
                           echo "File $i: ($file_name) Faild to upload. File must be <b>". $size_bytes / 1024 ."</b> KB. <br>";
                       }else{
                             #-----------------------------------------------------------#
                             # this code check if file is Already EXISTS.                #
                             #-----------------------------------------------------------#

                             if(file_exists($upload_dir.$file_name)){
                                 echo "File $i: ($file_name) already exists.<br>";
                             }else{
                                   #-----------------------------------------------------------#
                                   # this function will upload the files.   cool          #
                                   #-----------------------------------------------------------#
                                   if (move_uploaded_file($file_tmp,$upload_dir.$file_name)) {
							   $uploaddate = date("F j, Y");
	                           $filename = $file_name;


          include "../inc/config.inc.php";

$mysql = mysql_connect("$dbhost", "$dbuname", "$dbpass");
mysql_select_db("$dbname", $mysql) or die(mysql_error());

$result = mysql_query("UPDATE clients SET himg='$file_name', hdate='$hdate', hdesc='$hdesc', hdomain='$hdomain' WHERE mid='$mid'")
or die(mysql_error());

#			echo "<script type=\"text/javascript\">
#<!--
#window.location = \"index2.php\"
#//-->
#</script>";

                                   }else{
                                        echo "File: Faild to upload.<br>";
                                   }#end of (move_uploaded_file).

                             }#end of (file_exists).

                       }#end of (file_size).

                 }#end of (limitedext).

           }#end of (!is_uploaded_file).

       }#end of (for loop).
       # print back button.
       echo "»<a href=\"update.php?mid=$mid\">back</a>";
////////////////////////////////////////////////////////////////////////////////
//else if the form didn\'t submitted then show it.
}else{
$pmid = $_GET['mid'];
include "../inc/config.inc.php";

$mysql = mysql_connect("$dbhost", "$dbuname", "$dbpass");
mysql_select_db("$dbname", $mysql) or die(mysql_error());

$result = mysql_query("SELECT * FROM clients where mid='$pmid'")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {

echo " <form method=\"post\" action=\"update.php\" enctype=\"multipart/form-data\">";

echo "<table width=\"400\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  <tr><input type=\"hidden\" name=\"mid\" value=". $row['mid'] .">
    <td valign=\"top\"><div class=\"hage\" align=\"right\">House Age:  </div></td>
    <td><input type=\"text\" name=\"hage\" size=\"5\" value=". $row['hdate'] ."></td>
  </tr>
  <tr>
    <td valign=\"top\"><div class=\"hage\" align=\"right\">Website URL:  </div></td>
    <td><input type=\"text\" name=\"hurl\" size=\"25\" value=".$row['hdomain']."></td>
  </tr>
  <tr>
    <td valign=\"top\"><div class=\"forumtext\" align=\"right\">Description:  </div></td>
    <td><textarea name=\"desc\" cols=\"30\" rows=\"3\">".$row['hdesc']."</textarea></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td><div class=\"forumtext\" align=\"right\">File:  </div></td>
    <td>";
// show the file input field based on($num_files).
           for ($i = 1; $i <= $num_files; $i++) {
               echo "<input size=\"36\" class=\"textbox\" type=\"file\" name=\"file". $i ."\">";
           }
	   echo "</td>
  </tr>
  <tr>
    <td> </td>
    <td><input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$size_bytes\"></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td><input type=\"submit\" name=\"upload_form\" class=\"formbutton\" value=\"Upload Now!\"></td>
  </tr>
</table>
</form>";
}   }
		?></td>
          </tr>
          <tr>
            <td colspan="2"> </td>
          </tr>
        </table>

Link to comment
https://forums.phpfreaks.com/topic/154150-solved-upload-and-mysql-problem/
Share on other sites

Have you tried echoing out all the variables that you use in this query right before the query takes place...

 

$result = mysql_query("UPDATE clients SET himg='$file_name', hdate='$hdate', hdesc='$hdesc', hdomain='$hdomain' WHERE mid='$mid'")
or die(mysql_error());

 

also, I think that is supposed to be on one line...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.