Connecting Tech Pros Worldwide Forums | Help | Site Map

Having Problems with a Php code Involving File Upload and MySQL database entries

Newbie
 
Join Date: Aug 2007
Posts: 4
#1: Aug 27 '07
Hello everyone. I'm having problems with a page I've created that is used to insert data into a database.

It uploads two files into two distinct folder, and it takes the filenames and inserts them into a MYSQL database along with other product information.
Here is the entire .php file. I list it because the errors are not showing at all, and I need a fresh pair of eyes to look at it.

Any answers, critisims (constructive), or other talk that is related to the question is welcome. No security has been added because I want to make sure everything works before I start adding more code ( that and that is one of my weaknesess).

Here is the code (Working code is going to be italized):

Expand|Select|Wrap|Line Numbers
  1. <!--Session Cookie -->
  2. <?php
  3. if($_COOKIE['auth'] == '1'){
  4.     //do nothing
  5. } else {
  6.     //redirect bakc to login form if not authorized
  7.     header("Location: productlogin.php");
  8.     exit();
  9. }
  10. ?>
  11. <!-- End of Session Cookie -->
  12.  
  13. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  14. <html xmlns="http://www.w3.org/1999/xhtml">
  15. <head>
  16.  
  17. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  18. <title>Onsisa - Enter a Product</title><br />
  19. <link href="../OnsisaMainStyle.css" type="text/css" rel="stylesheet" />
  20. <link href="../OnsisaProductStyle.css" type="text/css" rel="stylesheet" />
  21.  
  22. </head>
  23. <body class="MainPageBody">
  24.  
  25.  
  26. <div align="center">
  27. <table border="0" class="TableBG">
  28.     <tr>
  29.         <td>
  30.             <!--Header-->
  31.             <a href="../index.html" target="_top"><img class="MainLogo" src="../images/onsisalogo.gif" border="0"/></a>
  32.             <!--End of Header-->
  33.         </td>
  34.     </tr>
  35. </table>
  36.  
  37.  
  38. <table border="0">
  39.     <tr>
  40.         <td colspan="2">
  41.             <!-- CSS Menu-->
  42.             <div class="MainMenu">                
  43.                 <a href="productenter.php"  target="_top">Entre Producto</a>&nbsp;
  44.                 <a href="productdelete.php"  target="_top">Destruye Producto</a>&nbsp;
  45.                 <a href="productsadmin.php" target="_top">Listas de Productos</a>&nbsp;
  46.                 <a href="productlogin.php" target="_top" onclick="<?php setcookie("auth", "", time() - 3600); ?>">Log Out</a>
  47.             </div>
  48.             <!--End of Css Menu-->
  49.         </td>
  50.     </tr>
  51.  
  52.     <tr>
  53.         <td colspan="2">
  54.             <hr style="background-color:#CC0000; color:#CC0000;" />
  55.         </td>
  56.     </tr>
  57.  
  58.     <tr>
  59.         <td>
  60.             <div align="left" style="font-family:Arial, Helvetica, sans-serif; font-weight:bold;">
  61.                 Seleccione Division Para Presentar
  62.             </div>
  63.         </td>
  64.  
  65.         <td>
  66.             <div align="right" style="font-family:Arial, Helvetica, sans-serif; font-weight:bold;">
  67.                 (Codigo de Divisiones: PP, CS, TO, & OW)
  68.             </div>
  69.         </td>
  70.     </tr>
  71.  
  72.     <tr>
  73.         <th colspan="2">
  74.  
  75.  
  76.     <?php $divisioncode = "PP"; ?>
  77.     <form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>" enctype="multipart/form-data">
  78.         <input type="submit" name="proteccion_personal" value="Proteccion Personal (PP)" class="redbuttons" />
  79.         <input type="submit" name="calzados_seguridad" value="Calzados de Seguridad (CS)" class="redbuttons" />
  80.         <input type="submit" name="herramientas" value="Herramientas (TO)" class="redbuttons" />
  81.         <input type="submit" name="on_work_wear" value="ON Work Wear (OW)" class="redbuttons" />
  82.         <input type="submit" name="show_all" value="Enseñe Todo" class="redbuttons" style="margin-top:5px;"/>
  83.         <hr style="color:#CC0000; background-color:#CC0000;" />
  84.         Entre <u><b>TODA</b></u> informacion correspondiente al producto, no deje ningun espacio en blanco.<br />
  85.         Imagen del producto: <input type="file" name="imageFile"/> <br/>
  86.  
  87.         <table border="0">
  88.             <tr>
  89.                 <th>Name</th><th>Type</th><th>Codigo</th><th>descripcion</th><th>Codigo de Division</th>
  90.             </tr>
  91.  
  92.             <tr>
  93.                 <td><input type="text" name="productName" /></td>
  94.                 <td><input type="text" name="productType" /></td>
  95.                 <td><input type="text" name="productCode" /></td>
  96.                 <td><input type="text" name="productDescription" /></td>
  97.                 <td><input type="text" name="divisionCode" /></td>
  98.             </tr>
  99.         </table>
  100.         <br />
  101.         Manual technico del producto: <input type="file" name="manualFile"/><br />
  102.         <input type="submit" name="submitProduct" value="Submite Nuevo Producto" class="redbuttons" />
  103.         <hr style="color:#CC0000; background-color:#CC0000;" />
  104.  
  105.  
  106.  
  107.  
  108.         <?php
  109.             error_reporting(E_ALL);
  110.             ini_set('display_errors', True);
  111.  
  112. //====================================================================================
  113. /* The item code is a code that differentiates products on one table between the different 
  114. company divisions*/
  115. /*
  116. -------------------------------
  117. Item Codes
  118. -------------------------------
  119. Proteccion Personal   = PP 
  120. Calzados de Seguridad = CS
  121. Herramientas          = TO
  122. ON Work Waer          = OW
  123. -------------------------------
  124. */
  125. //====================================================================================
  126.  
  127.  
  128.         if($_POST["proteccion_personal"]){
  129.             $divisioncode = "PP";
  130.         } else if($_POST["calzados_seguridad"]){
  131.             $divisioncode = "CS";
  132.         } else if($_POST["herramientas"]){
  133.             $divisioncode = "TO";
  134.         } else if($_POST["on_work_wear"]){
  135.             $divisioncode = "OW";
  136.         } else if($_POST["show_all"]){
  137.             $divisioncode = "*";
  138.         }
  139.  
  140.         $mysql = mysql_connect("localhost", "onsisac", "picobebe");
  141.          mysql_select_db("onsisac_onsisaproducts", $mysql);
  142.  
  143.         if(mysql_error()){
  144.             die("Connect failed: %s\n". mysql_error());
  145.             exit();
  146.         } else {
  147.  
  148.     //=======================
  149.     //Files & Data Insertion
  150.     //=======================
  151.             if($_POST['submitProduct']){
  152.  
  153.                 if ($_FILES["imageFile"]["error"] > 0 || $_FILES["manualFile"]["error"] > 0){
  154.                       echo "Error: " . $_FILES["imageFile"]["error"] . "<br />";
  155.                     echo "Error: " . $_FILES["manualFile"]["error"] . "<br />";
  156.                   } else {
  157.  
  158.                     $sqlAllFile = "INSERT INTO products (id, image, name, type, item_code, description, techmanual, division_code) VALUES ('','". $_FILES['imageFile']['name']."', '".$_POST['productName']."', '".$_POST['productType']."', '".$_POST['productCode']."',' ".$_POST['productDescription']."',' ".$_FILES['manualFile']['name']."', '".$_POST['divisionCode']."')";
  159.  
  160.                     $resAllFile = mysql_query($sqlAllFile, $mysql) or  or die('MySQL Error: ' .mysql_error());
  161.                     if($resAllFile){
  162.                         echo "<span style='color:#FF0000; font-weight:bold;'>Producto fue agregado con exito!</span><br/>";
  163.                     } else {
  164.                         echo "Error: producto no se pudo agregar - " . mysql_error()."<br />";
  165.                     }
  166.                  }
  167.  
  168.  
  169.             }
  170.  
  171.  
  172.  
  173.     //=======================
  174.     //Displaying of list
  175.     //=======================
  176.             if($divisioncode != "*"){
  177.                 $sql = "SELECT * FROM products WHERE item_code = '".$divisioncode."'";
  178.             } else if($divisioncode == "*"){
  179.                 $sql = "SELECT * FROM products";
  180.             }
  181.  
  182.             $res = mysql_query($sql, $mysql);
  183.  
  184.             if($res){
  185.                 $number_of_rows = mysql_num_rows($res);
  186.                 echo "<center>";
  187.                 if($divisioncode == "OW"){
  188.                     echo "<img src='../images/onworkwearlogo2.gif'/>";
  189.                 }else if($divisioncode == "TO"){
  190.                     echo "<span style='font-family:Arial, Helvetica, sans-serif; font-weight:bold;'>Herramientas</span>";
  191.                 }else if($divisioncode == "CS"){
  192.                     echo "<span style='font-family:Arial, Helvetica, sans-serif; font-weight:bold;'>Calzados de Seguridad</span>";
  193.                 }else if($divisioncode == "PP"){
  194.                     echo "<span style='font-family:Arial, Helvetica, sans-serif; font-weight:bold;'>Protección Personal</span>";
  195.                 }else if($divisioncode == "*"){
  196.                     echo "<span style='font-family:Arial, Helvetica, sans-serif; font-weight:bold;'>Todas Divisiones</span>";
  197.                 }else{
  198.                     printf("Error: No such item code %s.<br />", $divisioncode);
  199.                 }
  200.  
  201.                 echo "<hr style='background-color:#CC0000; color:#CC0000;' />";
  202.                 echo "<table border='0' style='border:#CC0000;' class='productlist'><tr>";
  203.  
  204.                 echo "<th>ID #</th><th>Image</th><th>Name</th><th>Type</th><th>Item Code</th><th>Description</th><th>Tech. Manual</th><th>Division Code</th></tr><tr>";
  205.  
  206.                 while ($row = mysql_fetch_assoc($res)) {
  207.  
  208.                     echo "<td>".$row['id']."</td><td><img src='../productimages/".$row['image']."' /></td><td>".$row['name']."</td><td>".$row['type']."</td><td>". $row['item_code']."</td><td>".$row['description']."</td><td><a href='../manuals/".$row['techmanual']." target='_blank'/></td><td>".$row['division_code']."</td></tr><tr>";
  209.  
  210.                 }
  211.  
  212.  
  213.                 echo "</tr></table>";    
  214.                 echo "<p>";
  215.                 printf("Resultados contiene %d products.\n", $number_of_rows);
  216.                 echo "</center>";
  217.  
  218.             } else {
  219.                 printf("Could not retrieve records: %s\n", mysql_error($mysql));
  220.             }
  221.  
  222.             mysql_close($mysql);
  223.         }
  224.     ?>
  225.                     </form>
  226.  
  227.                 </th>
  228.             </tr>
  229.         </table>
  230.  
  231.     </div>
  232.  
  233. </body>
  234. </html>
  235.  

Sorry for large post, but after a week, it is causing me to loose sleep.
Thank you very much.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,747
#2: Aug 27 '07

re: Having Problems with a Php code Involving File Upload and MySQL database entries


Hi, and welcome to TSDN!

What exactly is the problem with the code?
Have you tried to turn on PHP debug messages?

And please put your code inside [code] (or [php]) tags. It's impossible to read it without them ;)
Newbie
 
Join Date: Aug 2007
Posts: 4
#3: Aug 27 '07

re: Having Problems with a Php code Involving File Upload and MySQL database entries


Sorry about that, it's my first post. I tested the code, and removed all errors but when I try to execute only the database data get's uploaded, and when I try to display using the buttons (the show all button works) it does not display, and my images, and documents files are not uploading, and I actually read the link you gave me before posting, but I get no errors so the errors must be in my logic. somewhere, but I'm blind as to what it is exactly.

Thanks again.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,747
#4: Aug 27 '07

re: Having Problems with a Php code Involving File Upload and MySQL database entries


I cant' find the code that is supposed to move the uploaded files. You put the file names in the query but you don't do anything else with the files.

I would guess that the reason why the 'Show all' button is the only one that works is that you are matching the '$divisioncode' variable against the 'item_code' column in the query for the other buttons, which logically returns no rows.

Also, you seem to have two 'or' thingies on line 160. May want to remove one of them ;)
Newbie
 
Join Date: Aug 2007
Posts: 4
#5: Aug 28 '07

re: Having Problems with a Php code Involving File Upload and MySQL database entries


You're right. I guess I needed to step away from this code for a bit. I'm still a beginner. I guess I went ahead of myself and forgot to add that code. Thanks.
wadro21's Avatar
Newbie
 
Join Date: Aug 2007
Posts: 12
#6: Aug 28 '07

re: Having Problems with a Php code Involving File Upload and MySQL database entries


i am having a similure issue but i did add the code to move the files. my issue is that i get permission errors when the files are being moved and im not sure how to code in the correct user name and password to get access to upload the files to the server. it works fine on my local machine.

any help appreciated!
Newbie
 
Join Date: Aug 2007
Posts: 4
#7: Aug 29 '07

re: Having Problems with a Php code Involving File Upload and MySQL database entries


Quote:

Originally Posted by wadro21

i am having a similure issue but i did add the code to move the files. my issue is that i get permission errors when the files are being moved and im not sure how to code in the correct user name and password to get access to upload the files to the server. it works fine on my local machine.

any help appreciated!

I don't have permission errors now, but a one point I did. Then I found out that I had messed with the permission settings on the website itself. PHP has nothing to do with it. You have to change permissions manually (on the server itself using either a command line, or (in my case) a specific permission UI or GUI with each individual file and/or folder. I use the website provider's file manager, and it provides different permission settings especifically for each file/folder.
Reply