Displaying a single image and data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mike6256
    New Member
    • Feb 2009
    • 16

    Displaying a single image and data

    Hi all
    I am extremely new to php and need some help

    I found a code that Atli addressed in a 11/23/07 thread and have been working with it-all is great. My main question is how do I search for a single file and display the image for that single file. The code example by Atli will list all files

    Thanks
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    which of the 3 threads do you mean? (a link would be really helpful)

    Comment

    • mike6256
      New Member
      • Feb 2009
      • 16

      #3
      Sorry-here is the link

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Check out MySQL's WHERE clause.

        Comment

        • mike6256
          New Member
          • Feb 2009
          • 16

          #5
          Thanks for the WHERE clause link--I gave it a shot but get the result "There are no files in database" any advice for a rookie?
          # Query for existing files
          $result = mysqli_query($d bLink, "SELECT FileID, FileName, FileMime, FileSize, ToolNumber Created FROM FileStorage Where Toolnumber = 'textfield' ");

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            well, obviously there is no Toolnumber field with the value of 'textfield'.

            besides, make sure, all field names are correctly written (SQL can be/is case sensitive)

            Comment

            • mike6256
              New Member
              • Feb 2009
              • 16

              #7
              Thanks for the rapid reply
              I did find That Toolnumber should be ToolNumber-corrected it but still the same
              will 'textfield' pull over from my search page?

              Comment

              • hoopy
                New Member
                • Feb 2009
                • 88

                #8
                Isn't textfield meant to be $textfield i.e a variable?

                Comment

                • mike6256
                  New Member
                  • Feb 2009
                  • 16

                  #9
                  I tried it with the $ variable but get a undefined variable error

                  Comment

                  • hoopy
                    New Member
                    • Feb 2009
                    • 88

                    #10
                    Lets see your code, you need to take whatever the user entered in the text box, put it into a variable then use that in the SQL to retrieve.

                    Comment

                    • mike6256
                      New Member
                      • Feb 2009
                      • 16

                      #11
                      here is the code I am using that gives me undefined variable error

                      Code:
                      <?php 
                      # Connect to the database 
                      $dbLink = mysqli_connect("xx.xx.xx.xx", "xxxxx", "xxxxxxxx", "tools"); 
                      if(mysqli_connect_errno()) { 
                          die("MySQL connection failed: ". mysqli_connect_error()); 
                      } 
                        
                      # Query for a list of all existing files 
                      $result = mysqli_query($dbLink, "SELECT FileID, FileName, FileMime, FileSize, ToolNumber Created FROM FileStorage Where ToolNumber = [$textfield]"); 
                        
                      # Check if it was successfull 
                      if($result)  
                      { 
                        
                          # Make sure there are some files in there 
                          if(mysqli_num_rows($result) == 0) { 
                              echo "<p>There are no files in the database</p>"; 
                          } 
                          else 
                          { 
                              # Print the top of a table 
                              echo "<table width='100%'><tr>"; 
                              echo "<td><b>Name</b></td>"; 
                              echo "<td><b>Mime</b></td>"; 
                              echo "<td><b>Size (bytes)</b></td>"; 
                              echo "<td><b>Created</b></td>"; 
                              echo "<td><b>Tool Number</b></td>";
                              echo "<td><b>&nbsp;</b></td>"; 	
                              echo "</tr>"; 
                        
                              # Print each file 
                              while($row = mysqli_fetch_assoc($result)) 
                              { 
                                  # Print file info 
                                  echo "<tr><td>". $row['FileName']. "</td>"; 
                                  echo "<td>". $row['FileMime']. "</td>"; 
                                  echo "<td>". $row['FileSize']. "</td>"; 
                                  echo "<td>". $row['Created']. "</td>"; 
                                  echo "<td>". $row['ToolNumber']. "</td>";
                        
                                  # Print download link 
                                  echo "<td><a href='get_file.php?id=". $row['FileID'] ."'>Download</a></td>"; 
                                  echo "</tr>"; 
                              } 
                        
                              # Close table 
                              echo "</table>"; 
                          } 
                        
                          # Free the result 
                          mysqli_free_result($result); 
                      } 
                      else  
                      { 
                          echo "Error! SQL query failed:"; 
                          echo "<pre>". $dbLink->error ."</pre>"; 
                      } 
                        
                      # Close the mysql connection 
                      mysqli_close($dbLink); 
                      ?>
                      Last edited by Markus; Feb 26 '09, 03:51 PM. Reason: Added [code] tags.

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        I'm not sure what the square brackets are for... of cause $textfield has to be defined somewhere (looks like some form data).
                        Code:
                        // just one possibility...
                        $textfield = $_POST['user_input'];
                        
                        $result = mysqli_query($dbLink, "SELECT FileID, FileName, FileMime, FileSize, `ToolNumber Created` FROM FileStorage WHERE ToolNumber = " . mysql_real_escape_string($textfield));
                        // use ' around the value if it is a string

                        Comment

                        • mike6256
                          New Member
                          • Feb 2009
                          • 16

                          #13
                          would something like this work?

                          Code:
                          $textfield = $_POST['textfield']; 
                            
                          $result = mysqli_query($dbLink, "SELECT FileID, FileName, FileMime, FileSize, ToolNumber, Created FROM FileStorage WHERE ToolNumber = " . mysql_real_escape_string($textfield));
                          here is the code for the search

                          Code:
                          <form id="form1" name="form1" method="get" action="list_files.php">
                            <label>Tool Number
                            <input type="text" name="textfield" />
                            </label>
                            <p>
                              <label>
                              <input type="submit" name="Submit" value="Submit" />
                              </label>
                            </p>
                          sorry for so many questions. I have only been doing this since Monday
                          Last edited by Markus; Feb 26 '09, 03:55 PM. Reason: Added [code] tags.

                          Comment

                          • hoopy
                            New Member
                            • Feb 2009
                            • 88

                            #14
                            Your method is get on the form, not post so change this:

                            Code:
                            <form id="form1" name="form1" method="get" action="list_files.php">
                            To:

                            Code:
                            <form id="form1" name="form1" method="post" action="list_files.php">
                            And that should work OK.

                            Or alternatively change:

                            Code:
                            $textfield = $_POST['textfield'];
                            To:

                            Code:
                            $textfield = $_GET['textfield'];
                            Cheers

                            Comment

                            • mike6256
                              New Member
                              • Feb 2009
                              • 16

                              #15
                              Well I did the reccommended changes and now get another error:

                              Error! SQL query failed:
                              You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
                              PHP Warning: mysql_real_esca pe_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localho st' (using password: NO) in C:\Inetpub\wwwr oot\tlsc\list_f iles.php on line 12 PHP Warning: mysql_real_esca pe_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\Inetpub\wwwr oot\tlsc\list_f iles.php on line 12
                              This is line 12:

                              Code:
                              $result = mysqli_query($dbLink, "SELECT FileID, FileName, FileMime, FileSize, ToolNumber, Created FROM FileStorage WHERE ToolNumber = " . mysql_real_escape_string($textfield));
                              Last edited by Markus; Feb 26 '09, 05:46 PM. Reason: Added [code] and [quote] tags.

                              Comment

                              Working...