473,322 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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.
Aug 27 '07 #1
6 2146
Atli
5,058 Expert 4TB
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 ;)
Aug 27 '07 #2
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.
Aug 27 '07 #3
Atli
5,058 Expert 4TB
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 ;)
Aug 27 '07 #4
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.
Aug 28 '07 #5
wadro21
12
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!
Aug 28 '07 #6
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.
Aug 29 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: windandwaves | last post by:
Hi Folk I want my clients to load up a file to a MySql database. The file should contain rows with name, email, address, city What would be the easiest way (in terms of the big picture), to...
5
by: Codeman II | last post by:
Hi there, I am building a form where the user must upload a picture and fill in his details. Now I have a problem as all of this is on the same form. How will I be able to have the Browse...
0
by: TB | last post by:
Hi All: This news group is proving to be great help on my path towards mastering ASP.NET thanks to all of you helpful souls out there. I am looking forward to the day when I can contribute with...
5
by: TB | last post by:
Hi All: This news group is proving to be great help on my path towards mastering ASP.NET thanks to all of you helpful souls out there. I am looking forward to the day when I can contribute...
15
by: John Nagle | last post by:
I've been installing Python and its supporting packages on a dedicated server with Fedora Core 6 for about a day now. This is a standard dedicated rackmount server in a colocation facility,...
2
tolkienarda
by: tolkienarda | last post by:
hi all have finnaly figured out how this php script works. i have been trying to modify it for some time but i sturgled because i had no idea what the logic behind it was but finally i figured it...
2
by: starfi3ld | last post by:
I got a few errors with this script Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/starfi3ld/domains/starfi3ld.com/public_html/newreleases.php on line...
2
by: rustyc | last post by:
Well, here's my first post in this forum (other than saying 'HI' over in the hi forum ;-) As I said over there: ... for a little side project at home, I'm writing a ham radio web site in...
1
by: chennaibala | last post by:
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or any image file while uploading. i develop...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.