$50 REWARD!!! Help me with this code!!!

Discussion in 'PHP' started by Tango4Cash, Apr 13, 2010.

  1. #1
    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.
     
    Tango4Cash, Apr 13, 2010 IP
  2. Warll

    Warll Peon

    Messages:
    122
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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?
     
    Warll, Apr 13, 2010 IP
  3. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    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:
     
    Last edited: Apr 13, 2010
    guardian999, Apr 13, 2010 IP
  4. Warll

    Warll Peon

    Messages:
    122
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    FYI 'file_put_contents' in case you don't already know how to save files in php.
     
    Warll, Apr 13, 2010 IP
  5. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    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:
     
    Last edited: Apr 13, 2010
    guardian999, Apr 13, 2010 IP
  6. alberrambo

    alberrambo Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ok ..i will help you just PM me.You will get the ultimate solution without increasing the size of database
     
    alberrambo, Apr 13, 2010 IP
  7. Warll

    Warll Peon

    Messages:
    122
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    guardian999 just provided a perfectly fine solution, he even provided code without prior agreement.
     
    Warll, Apr 13, 2010 IP
  8. Tango4Cash

    Tango4Cash Peon

    Messages:
    97
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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?
     
    Tango4Cash, Apr 13, 2010 IP
  9. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #9
    tango you will need to add an additional field modelid in the table

    Regards

    Alex
     
    kmap, Apr 13, 2010 IP
  10. Tango4Cash

    Tango4Cash Peon

    Messages:
    97
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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
     
    Tango4Cash, Apr 13, 2010 IP
  11. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #11
    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
     
    kmap, Apr 13, 2010 IP
  12. Tango4Cash

    Tango4Cash Peon

    Messages:
    97
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Okay thanks alex, had to change the names. But what is this about "modelid". What is that?
     
    Tango4Cash, Apr 13, 2010 IP
  13. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #13
    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
     
    Last edited: Apr 13, 2010
    kmap, Apr 13, 2010 IP
  14. Tango4Cash

    Tango4Cash Peon

    Messages:
    97
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    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.
     
    Last edited: Apr 13, 2010
    Tango4Cash, Apr 13, 2010 IP
  15. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #15
    
    
    <?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
     
    Last edited: Apr 13, 2010
    kmap, Apr 13, 2010 IP
  16. Tango4Cash

    Tango4Cash Peon

    Messages:
    97
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Kmap!!! Thanks man!!! You got it!

    Also thanks to guardian for helping, but kmap logged in and fixed everything.
     
    Tango4Cash, Apr 14, 2010 IP
  17. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #17
    I'm too late -__-"
     
    guardian999, Apr 14, 2010 IP