Okay, I will pay $50 via paypal to the first person that can PM a WORKING version File upload problem while displaying data from a Mysql table Ok so I am deveopling a site for one of my clients, and I had to use some php I am not familiar with to do what he wants. Basically, all I am doing is displaying data from a mysql table on an html table on a website. However, one of the feilds is a BLOB and I am uploading pdfs to this feild. I need to be able to display them as a little pdf icon so they are downloadable. Currently, when I upload the pdf to the table, it is displayed all funky with tons of weirds characters. Here is one of the pages I am talking about. http://gator452.hostgator.com/~teiweb/?page_id=58 Where I currently have "test1,test2" etc is the feild where I need it to be downloadable. I need it like this page here, where they have "specs" as a downloadable file on each one. http://www.sinwan.com/sinwan_web/sho...ial_Plastic_AC Here is the code I am currently using -----------------Here is the Code---------------------------- <?PHP # Param 1 : MySQL Host Name # Param 2 : MySQL Username # Param 3 : MySQL Password # Param 4 : MySQL Database # Param 5 : SQL Statement (SELECT) show_table("localhost","MYUSERNAME","MYPASS","MYDATABASE","SELECT * FROM MYTABLENAME"); function show_table($hostName,$userName,$passWord,$dataBase,$sqlQuery) { # Connect to MySQL $conn=mysql_connect($hostName,$userName,$passWord); # Select Database mysql_select_db($dataBase,$conn); # Validate SQL Statement $array=explode(" ORDER",$sqlQuery); $sqlQuery=$array[0]; if(!strstr($sqlQuery,"SELECT")) die("Invalid Query : SQL statement should be a SELECT statement."); # ORDER records by requested column if($_GET['order']) $sqlQuery=$sqlQuery." ORDER BY ".$_GET['order']; # Execute SQL query $result=mysql_query($sqlQuery) or die("Invalid Query : ".mysql_error()); $row=mysql_fetch_array($result); # Check whether NULL records found if(!mysql_num_rows($result)) die("No records found."); echo "<table border=1><tr>"; # Make the row for table column names while (list($key, $value) = each($row)) { $i++; if(!($i%2)) echo "<td><b><a href='?order=$key'>$key</a></td>"; } echo "</tr>"; $result=mysql_query($sqlQuery); // Make rows for records while($rec=mysql_fetch_array($result)) { echo "<tr>"; for($i=0;$i<count($rec);$i++) { if($rec[$i]) echo "<td>$rec[$i]</td>"; } echo "</tr>"; } echo "</table>"; } ?> ----------------------------- Like I said, if you help me I will pay $50 through paypal. I either need the corrected code or a better way of doing it. But I need it to be explained so I understand. Please, I am very new to php so consider me as knowing nothing when explaining it.
Easiest way to do it, and the most common, is to save the uploaded file to a folder, then save a link to the DB. Then when the user views the page you show an image(The PDF icon) hot-linked to where you saved the file, so much simpler eh?
Agree with warll. Or use fpdi to open your pdf file and open it at browser. <?PHP # Param 1 : MySQL Host Name # Param 2 : MySQL Username # Param 3 : MySQL Password # Param 4 : MySQL Database # Param 5 : SQL Statement (SELECT) show_table("localhost","MYUSERNAME","MYPASS","MYDATABASE","SELECT * FROM MYTABLENAME"); function show_table($hostName,$userName,$passWord,$dataBase,$sqlQuery){ # Connect to MySQL $conn = mysql_connect($hostName,$userName,$passWord); # Select Database mysql_select_db($dataBase,$conn); # Validate SQL Statement $array=explode(" ORDER",$sqlQuery); $sqlQuery=$array[0]; if(!strstr($sqlQuery,"SELECT")) die("Invalid Query : SQL statement should be a SELECT statement."); # ORDER records by requested column // Use mysql_real_escape_string to escape query if($_GET['order']) $sqlQuery = sprintf("%s ORDER BY %s",$sqlQuery,mysql_real_escape_string($_GET['order'])); # Execute SQL query $result=mysql_query($sqlQuery) or die("Invalid Query : ".mysql_error()); $row=mysql_fetch_array($result); # Check whether NULL records found if(!mysql_num_rows($result)) die("No records found."); echo "<table border=1><tr>"; # Make the row for table column names while (list($key, $value) = each($row)){ $i++; if(!($i%2)) echo "<td><b><a href='?order=$key'>$key</a></td>"; } echo "</tr>"; $result = mysql_query($sqlQuery); // Make rows for records while($rec = mysql_fetch_array($result)){ echo "<tr>"; /*for($i=0;$i < count($rec);$i++){ if($rec[$i]) echo "<td>".$rec[$i]."</td>"; }*/ echo "<td>".$rec[0]."</td>"; echo "<td>".$rec[1]."</td>"; echo "<td>".$rec[2]."</td>"; echo "<td>".$rec[3]."</td>"; echo "<td>".$ref[4]."</td>"; // Use css class, and hidden span content echo "<td><a href=\"".$rec[5]."\" class=\"pdf-icon\"><span>Download</span></a>"; echo "</tr>"; } echo "</table>"; } ?> PHP:
Other solution is with this your php <?PHP # Param 1 : MySQL Host Name # Param 2 : MySQL Username # Param 3 : MySQL Password # Param 4 : MySQL Database # Param 5 : SQL Statement (SELECT) show_table("localhost","MYUSERNAME","MYPASS","MYDATABASE","SELECT * FROM MYTABLENAME"); function show_table($hostName,$userName,$passWord,$dataBase,$sqlQuery){ # Connect to MySQL $conn = mysql_connect($hostName,$userName,$passWord); # Select Database mysql_select_db($dataBase,$conn); # Validate SQL Statement $array=explode(" ORDER",$sqlQuery); $sqlQuery=$array[0]; if(!strstr($sqlQuery,"SELECT")) die("Invalid Query : SQL statement should be a SELECT statement."); # ORDER records by requested column // Use mysql_real_escape_string to escape query if($_GET['order']) $sqlQuery = sprintf("%s ORDER BY %s",$sqlQuery,mysql_real_escape_string($_GET['order'])); # Execute SQL query $result=mysql_query($sqlQuery) or die("Invalid Query : ".mysql_error()); $row=mysql_fetch_array($result); # Check whether NULL records found if(!mysql_num_rows($result)) die("No records found."); echo "<table border=1><tr>"; # Make the row for table column names while (list($key, $value) = each($row)){ $i++; if(!($i%2)) echo "<td><b><a href='?order=$key'>$key</a></td>"; } echo "</tr>"; $result = mysql_query($sqlQuery); // Make rows for records while($rec = mysql_fetch_array($result)){ echo "<tr>"; /*for($i=0;$i < count($rec);$i++){ if($rec[$i]) echo "<td>".$rec[$i]."</td>"; }*/ echo "<td>".$rec['model']."</td>"; echo "<td>".$rec['size']."</td>"; echo "<td>".$rec['voltage']."</td>"; echo "<td>".$rec['airflow']."</td>"; echo "<td>".$rec['noise']."</td>"; echo "<td>".$rec['pressure']."</td>"; // Use css class, and hidden span content echo "<td><a href=\"pdf.php?id=".$rec['MODELID']."\" class=\"pdf-icon\"><span>Download</span></a>"; echo "</tr>"; } echo "</table>"; } ?> PHP: pdf.php <?php # We should get integer, not character, string, or sql injection. ;) $id = intval($_GET['id']); if(empty($id)){ exit; } $sql = sprinf("SELECT PDF_FIELD FROM MYTABLENAME WHERE MODELID='%s'",mysql_real_escape_string($id)); $result = mysql_query($sql); if($result){ $rows = mysql_fetch_row($result); // Set Content-type for pdf header("Content-type: application/pdf"); // It will be called downloaded.pdf and force to download header("Content-Disposition: attachment; filename=downloaded.pdf"); // Shout out the pdf content echo $rows[0]; } ?> PHP:
ok ..i will help you just PM me.You will get the ultimate solution without increasing the size of database
Ok I am going to test guardians999's solution. See the thing is my client will be uploading data himself, and the whole link thing is probably too complicated for him. He wants to just hit browse and upload the pdf. I created a php admin panel for him with some wizard software. Is this possible?
I have just put guardian999's code in and for some reason its not showing the data from the table. Here is the link: http://gator452.hostgator.com/~teiweb/?page_id=60 Did I do something wrong? I copy and pasted it exactly I just changed the login credentials
echo "<td>".$rec['model']."</td>"; echo "<td>".$rec['size']."</td>"; echo "<td>".$rec['voltage']."</td>"; echo "<td>".$rec['airflow']."</td>"; echo "<td>".$rec['noise']."</td>"; echo "<td>".$rec['pressure']."</td>"; change these names of fields as in db Also you need to add one more field modelid in the db for each record Regards Alex
modelid should be a unique interger for each record 1 2 3 4 5 u shd change the type of field to int not test test2 normally modelid is kept Autonumber so make it autonumber and for missing add manually(if any) Regards Alex
This is driving me CRAZY!!!! I just uploaded a pdf to the database and its STILL showing up all F*CKED UP. I need to be able to easily upload a pdf in an admin panel. Here is the link to that admin panel. [LINK REMOVED] Is this what I need to be able to do this? Please help!!! Please, talk to me like I know NOTHING. I mean pretend I can barely use email or something. Like I said I will sen $50 paypal immediatly when this thing works to whoever gets it working. Thanks in advance.
<?PHP # Param 1 : MySQL Host Name # Param 2 : MySQL Username # Param 3 : MySQL Password # Param 4 : MySQL Database # Param 5 : SQL Statement (SELECT) show_table("localhost","MYUSERNAME","MYPASS","MYDATABASE","SELECT * FROM MYTABLENAME"); function show_table($hostName,$userName,$passWord,$dataBase,$sqlQuery){ # Connect to MySQL $conn = mysql_connect($hostName,$userName,$passWord); # Select Database mysql_select_db($dataBase,$conn); # Validate SQL Statement $array=explode(" ORDER",$sqlQuery); $sqlQuery=$array[0]; if(!strstr($sqlQuery,"SELECT")) die("Invalid Query : SQL statement should be a SELECT statement."); # ORDER records by requested column // Use mysql_real_escape_string to escape query if($_GET['order']) $sqlQuery = sprintf("%s ORDER BY %s",$sqlQuery,mysql_real_escape_string($_GET['order'])); # Execute SQL query $result=mysql_query($sqlQuery) or die("Invalid Query : ".mysql_error()); $row=mysql_fetch_array($result); # Check whether NULL records found if(!mysql_num_rows($result)) die("No records found."); echo "<table border=1><tr>"; # Make the row for table column names while (list($key, $value) = each($row)){ $i++; if(!($i%2)) echo "<td><b><a href='?order=$key'>$key</a></td>"; } echo "</tr>"; $result = mysql_query($sqlQuery); // Make rows for records while($rec = mysql_fetch_array($result)){ echo "<tr>"; /*for($i=0;$i < count($rec);$i++){ if($rec[$i]) echo "<td>".$rec[$i]."</td>"; }*/ echo "<td>".$rec['model']."</td>"; echo "<td>".$rec['size']."</td>"; echo "<td>".$rec['voltage']."</td>"; echo "<td>".$rec['airflow']."</td>"; echo "<td>".$rec['noise']."</td>"; echo "<td>".$rec['pressure']."</td>"; // Use css class, and hidden span content echo "<td><a href=\"pdf.php?model=".$rec['MODEL']."&size=".$rec['size']."&voltage=".$rec['voltage']."&airflow=".$rec['airflow']."&noise=".$rec['noise']."&pressure=".$rec['pressure']."\" class=\"pdf-icon\"><span>Download</span></a>"; echo "</tr>"; } echo "</table>"; } ?> pdf.php <?php # We should get integer, not character, string, or sql injection. ;) $model = mysql_real_escape_string($_GET['model']); $size = mysql_real_escape_string($_GET['size']); $voltage =mysql_real_escape_string( $_GET['voltage']); $noise = mysql_real_escape_string($_GET['noise']); $airflow = mysql_real_escape_string($_GET['airflow']); $pressure = mysql_real_escape_string($_GET['pressure']); if(empty($id)){ exit; } $sql = "SELECT PDF_FIELD FROM MYTABLENAME WHERE MODEL='".$model."' and size='".$size."' and voltage='".$voltage."' and noise='".$noise."' and airflow='".$airflow."' and pressure='".$pressure."'"; $result = mysql_query($sql); if($result){ $rows = mysql_fetch_row($result); // Set Content-type for pdf header("Content-type: application/pdf"); // It will be called downloaded.pdf and force to download header("Content-Disposition: attachment; filename=downloaded.pdf"); // Shout out the pdf content echo $rows[0]; } ?> PHP: Change the field names as u changed earlier Also the pdf file stored in which field change the name Can u post the details of table name of field type Give me cpanel or ftp and phpmyadmin details i will make it work in 10 min Regards Alex
Kmap!!! Thanks man!!! You got it! Also thanks to guardian for helping, but kmap logged in and fixed everything.