Connecting Tech Pros Worldwide Forums | Help | Site Map

File upload into MySQL database failing.

Newbie
 
Join Date: Jun 2009
Posts: 20
#1: Jul 1 '09
Hi. I have a problem here. I want to upload a file together with its semester. So I create two field. One for the semester and one for the file to be uploaded. But the script doesn't work. I don't know why. However, if I try to upload the file without the semester field, the script is working. May be the problem comes from the semester field. This is my codes.

Form.php

?>
Expand|Select|Wrap|Line Numbers
  1. <?php require_once('Connections/connection.php'); ?><?php
  2. if (!isset($_SESSION)) {
  3.   session_start();
  4. }
  5.  
  6. // ** Logout the current user. **
  7. $logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
  8. if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  9.   $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
  10. }
  11.  
  12. if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  13.  
  14.   $_SESSION['MM_Username'] = NULL;
  15.   $_SESSION['MM_UserGroup'] = NULL;
  16.   $_SESSION['PrevUrl'] = NULL;
  17.   unset($_SESSION['MM_Username']);
  18.   unset($_SESSION['MM_UserGroup']);
  19.   unset($_SESSION['PrevUrl']);
  20.  
  21.   $logoutGoTo = "login_option.php";
  22.   if ($logoutGoTo) {
  23.     header("Location: $logoutGoTo");
  24.     exit;
  25.   }
  26. }
  27. ?>
  28. <?php
  29. $colname_rsSearchJKM_sem = "-1";
  30. if (isset($_GET['sesi'])) {
  31.   $colname_rsSearchJKM_sem = (get_magic_quotes_gpc()) ? $_GET['sesi'] : addslashes($_GET['sesi']);
  32. }
  33. mysql_select_db($database_connection, $connection);
  34. $query_rsSearchJKM_sem = sprintf("SELECT sesi FROM sesi");
  35. $rsSearchJKM_sem = mysql_query($query_rsSearchJKM_sem, $connection) or die(mysql_error());
  36. $row_rsSearchJKM_sem = mysql_fetch_assoc($rsSearchJKM_sem);
  37. $totalRows_rsSearchJKM_sem = mysql_num_rows($rsSearchJKM_sem);
  38. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  39. <html xmlns="http://www.w3.org/1999/xhtml">
  40. <head>
  41. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  42. <title>Untitled Document</title>
  43. <style type="text/css">
  44. <!--
  45. body {
  46.     background-image: url(images/backgrounding.gif);
  47.     background-repeat: no-repeat;
  48.     background-attachment: fixed;
  49. }
  50. -->
  51. </style></head>
  52.  
  53. <body>
  54. <div align="center">
  55. <table width="828" border="2">
  56.   <!--DWLayoutTable-->
  57.   <tr>
  58.     <td width="822" height="148" valign="top" bgcolor="#6699FF"><form id="form1" name="form1" method="post" action="">
  59.       <img src="banner/header.gif" width="822" height="128" />
  60.       <?php include 'selamat_datang.php'; ?>
  61.       <?php include 'jam.php'; ?>
  62.       <?php include 'dropDOwn.php'; ?>
  63.     </form></td>
  64.   </tr>
  65.   <tr>
  66.     <td height="32" valign="top" bgcolor="#6699FF"><p align="right">&nbsp;<a href="<?php echo $logoutAction ?>"><font color='#FF0000'>Keluar</font></a></p>
  67.         <p align="center">
  68.         <p align="center" style="font-family:'Lucida Handwriting'; font-size: 24px; color:#990000"><strong>JABATAN KEJURUTERAAN MEKANIKAL</strong></p>        
  69.         <p align="center">        
  70.         <p align="center">
  71.         <form action="Upload.php" method="post">
  72.         <div align="left"><strong>SESI:</strong>
  73.         <select name="uploaded_file" type="file" id="uploaded_file" &nbsp;name="uploaded_file">
  74.         <?php 
  75.             $sql="SELECT sesi FROM sesi";
  76.             $result =mysql_query($sql);
  77.             while ($data=mysql_fetch_assoc($result))
  78.             {
  79.                  echo "<option value=\"{$data['sesi']}\">{$data['sesi']}</option>";
  80.                 }
  81.             ?>
  82.           </select>
  83.          </p>
  84.             <p>     
  85.           <div align="left">
  86.                 <p><strong>ABSTRAK:</strong>
  87.                       <input name="uploaded_file" type="file" size="40" id="uploaded_file"&nbsp;name="uploaded_file" />
  88.               </p>
  89.                 <p><br />
  90.                       <input type="submit"&nbsp;value="Upload file" />
  91.                   </p>
  92.                         </p>
  93.         </form>
  94.  
  95. <p>&nbsp;    </p>
  96. <p><a href="listJKM.php">Lihat semua abstrak</a>
  97. </p>
  98. *</p>
  99.       <p>&nbsp;</p>
  100.       <p>&nbsp;</p>
  101.       <p>&nbsp;</p></td>
  102.   </tr>
  103.   <tr>
  104.     <td height="18" valign="top" bgcolor="#6699FF"><form id="form4" name="form4" method="post" action="">
  105.       <div align="center">
  106.         <?php include 'copyright.php'; ?>
  107.           </div>
  108.     </form>
  109.     </td>
  110.   </tr>
  111. </table>
  112. </div>
  113. </body>
  114. </html>
  115. <?php
  116. mysql_free_result($rsSearchJKM_sem);
  117. ?>
Upload.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. if (!isset($_SESSION)) {
  4.   session_start();
  5. }
  6.  
  7. // ** Logout the current user. **
  8. $logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
  9. if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  10.   $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
  11. }
  12.  
  13. if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  14.   $_SESSION['MM_Username'] = NULL;
  15.   $_SESSION['MM_UserGroup'] = NULL;
  16.   $_SESSION['PrevUrl'] = NULL;
  17.   unset($_SESSION['MM_Username']);
  18.   unset($_SESSION['MM_UserGroup']);
  19.   unset($_SESSION['PrevUrl']);
  20.  
  21.   $logoutGoTo = "login_option.php";
  22.   if ($logoutGoTo) {
  23.     header("Location: $logoutGoTo");
  24.     exit;
  25.   }
  26. }
  27. ?>
  28. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  29. <html xmlns="http://www.w3.org/1999/xhtml">
  30. <head>
  31. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  32. <title>Untitled Document</title>
  33. <style type="text/css">
  34. <!--
  35. body {
  36.     background-image: url(images/backgrounding.gif);
  37.     background-repeat: no-repeat;
  38.     background-attachment: fixed;
  39. }
  40. -->
  41. </style></head>
  42.  
  43. <body>
  44. <div align="center">
  45. <table width="828" border="0">
  46.   <!--DWLayoutTable-->
  47.   <tr>
  48.     <td width="822" height="148" valign="top" bgcolor="#6699FF"><form id="form1" name="form1" method="post" action="">
  49.       <img src="banner/header.gif" width="822" height="128" />
  50.       <?php include 'selamat_datang.php'; ?>
  51.       <?php include 'dropDown.php'; ?>
  52.     </form></td>
  53.   </tr>
  54.   <tr>
  55.     <td height="32" valign="top" bgcolor="#6699FF"><p align="right">&nbsp;<a href="<?php echo $logoutAction ?>"><font color='#FF0000'>Keluar</font></a></p>
  56.         <p align="center">
  57. <?php
  58.  
  59. if(isset($_FILES['uploaded_file'])) 
  60.  
  61.     if($_FILES['uploaded_file']['error'] == 0) 
  62.     {
  63.  
  64.         $dbLink = mysqli_connect("localhost", "root", "", "inovasi");
  65.         if(mysqli_connect_errno()) {
  66.             die("MySQL connection failed: ". mysqli_connect_error());
  67.         }       
  68.         $sesi = mysqli_real_escape_string($dbLink, $_FILES['uploaded_file']['sesi']);
  69.         $name = mysqli_real_escape_string($dbLink, $_FILES['uploaded_file']['name']);
  70.         $mime = mysqli_real_escape_string($dbLink, $_FILES['uploaded_file']['type']);
  71.         $size = $_FILES['uploaded_file']['size'];
  72.         $data = mysqli_real_escape_string($dbLink, file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
  73.  
  74.         $query = "
  75.             INSERT INTO FileStorage (
  76.                 FileName, FileMime, FileSize, FileData, Created, sesi
  77.             )
  78.             VALUES (
  79.                 '{$name}', '{$mime}', {$size}, '{$data}', NOW(), '{$sesi}'
  80.             )";
  81.  
  82.         $result = mysqli_query($dbLink, $query);
  83.  
  84.         if($result) 
  85.         {
  86.             echo "Berjaya! Abstrak telah di upload.";
  87.         }
  88.         else 
  89.         {
  90.             echo "Error! Failed to insert the file";
  91.             echo "<pre>". mysqli_error($dbLink) ."</pre>";
  92.         }
  93.     }
  94.     else 
  95.     {
  96.         echo "Error! 
  97.                 An error accured while the file was being uploaded. 
  98.                 Error code: ". $_FILES['uploaded_file']['error'];
  99.     }
  100.     mysqli_close($dbLink);
  101. }
  102. else 
  103. {
  104.     echo "Gagal! Abstrak tidak berjaya di upload!";
  105. }
  106. echo '<p>Klik <a href="JKM_admin.php">di sini</a> untuk kembali</p>';
  107. ?>
  108.  
  109. &nbsp;</p>
  110.       </td>
  111.   </tr>
  112.   <tr>
  113.     <td height="18" valign="top" bgcolor="#6699FF"><form id="form4" name="form4" method="post" action="">
  114.       <div align="center">
  115.         <?php include 'copyright.php'; ?>
  116.           </div>
  117.     </form>
  118.     </td>
  119.   </tr>
  120. </table>
  121. </div>
  122. </body>
  123. </html>
  124.  
Someone please help me...Thanks

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Denver, CO
Posts: 1,144
#2: Jul 1 '09

re: File upload into MySQL database failing.


That's too much code to go through and find one problem. Have you done your part of troubleshooting? what's the semester field? if with it, it fails, have you tried looking into to see if it's correct?

can you run a manual test query with the semester field from a client?

At least trace the problem (follow the values with die(print_r($value)) lines) until you find the code that doesn't do what it's suppose to do.


Good luck,







Dan
Reply