Jump to content

PHP script help


Kitty

Recommended Posts

I found a code, that selects the layouts from my database and displays the number of the downloads/previews. Here it is:

[code]<?php

//MySQL connection variables
$user="blah"; 
$host="localhost"; 
$password="blah"; 
$database="blah"; 
$connection = mysql_connect($host,$user,$password) 
or die ("couldn't connect to server"); $db = mysql_select_db($database,$connection) 
or die ("Couldn't select database"); 

//Which mySQL table to select from and in what order
$query = "select * from `layouts` order by id desc"; 

$result = mysql_query($query, $connection) or die ("Could not execute query : $query ." . mysql_error()); 
$rows = mysql_num_rows($result);
if ($rows=="0") { echo "No layouts found."; }


//Gets and assigns variables to array
    while ($row=mysql_fetch_array($result)) { 
    $id=$row["id"]; 
    $title=$row["title"]; 
    $date=$row["date"];
    $type=$row["type"];
    $size=$row["size"]; 
    $designer=$row["designer"]; 
    $designerurl=$row["designerurl"];   
    $previewimage=$row["previewimage"];
    $download=$row["download"]; 
    $dcounter=$row["dcounter"];
    $preview=$row["preview"];
    $pcounter=$row["pcounter"]; 

    //Prints out results
    echo "<table id=\"box_top\"><tr>
    <tr><td>
    <b>$title</b>
    </td>
    </tr>
    </table>
   
    <table id=\"box\">
    <tr>
    <td valign=\"top\">
    <img src=\"$previewimage\" width=\"160\" height=\"110\" border=\"0\" alt=\"\">
    </td> 
    <td valign=\"top\" width=\"170\">
    <b>Added</b>: $date
    <br><b>Type</b>: $type
    <br><b>Size</b>: $size
    <br><b>Designer</b>: <a href=\"$designerurl\" target=\"_blank\">$designer</a>
    <br><b>Previews</b>: $pcounter
    <br><b>Downloads</b>: $dcounter
    <br><br>
<a href=\"preview.php?id=$id\"><b>Preview</b></a> &nbsp; <a href=\"download.php?id=$id\"><b>Download</b></a>
    </td>
    </tr>
    </table><br>";
}
       

?> [/code]

And here's my download.php file:

[code]<?php
//connection info
$user="blah";
$host="localhost";
$password="blah";
$database="blah";

$connection = mysql_connect($host,$user,$password)
or die ("couldn't connect to server"); $db = mysql_select_db($database,$connection)
or die ("Couldn't select database");

//chooses which table in the layout to load from
$q="SELECT * from layouts where id='$id'";
$result= mysql_query($q) or die
("Could not execute query : $q." . mysql_error());

while ($row=mysql_fetch_array($result))
{
//table fields [this may not be the same if you have different fields compared to mine]
    $id=$row["id"]; 
    $title=$row["title"]; 
    $date=$row["date"];
    $type=$row["type"];
    $size=$row["size"]; 
    $designer=$row["designer"]; 
    $designerurl=$row["designerurl"];   
    $previewimage=$row["previewimage"];
    $download=$row["download"]; 
    $dcounter=$row["dcounter"];
    $preview=$row["preview"];
    $pcounter=$row["pcounter"]; 
header('Location: '.$download);
$q="update layouts set dcounter=dcounter+1 where id='$id'";
$result= mysql_query($q) or die
("Could not execute query : $q." . mysql_error());

}
?> [/code]

I have another file called preview.php that is the same as download.php, but I only changed:

[code]header('Location: '.$preview);
$q="update layouts set pcounter=pcounter+1 where id='$id'"; [/code]

Now, the problem is when I click on the download or preview link I get a blank white page. Does anyone know what is wrong?

Thanks!
Link to comment
Share on other sites

Well by redirecting them to another page using header, you essentially are taking away the ability to echo anything out to the browser since the header HAS to be the first thing outputted.
Might also want to think about only pulling the relevant fields from the table rather than everything. Will make for a quicker query.
If you make it a link instead you could do something like this
[code]<?php
//connection info
$user="blah";
$host="localhost";
$password="blah";
$database="blah";

$connection = mysql_connect($host,$user,$password) or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection) or die ("Couldn't select database");

//chooses which table in the layout to load from
$q="SELECT * from layouts where id='$id'";
$result= mysql_query($q) or die
("Could not execute query : $q." . mysql_error());
$row=mysql_fetch_array($result);
{
//table fields [this may not be the same if you have different fields compared to mine]
    $id=$row["id"]; 
    $title=$row["title"]; 
    $date=$row["date"];
    $type=$row["type"];
    $size=$row["size"]; 
    $designer=$row["designer"]; 
    $designerurl=$row["designerurl"];   
    $previewimage=$row["previewimage"];
    $download=$row["download"]; 
    $dcounter=$row["dcounter"];
    $preview=$row["preview"];
    $pcounter=$row["pcounter"]; 
$q="update layouts set dcounter=dcounter+1 where id='$id'";
$result= mysql_query($q) or die ("Could not execute query : $q." . mysql_error());
  if(!$result){
  echo "Could not update table, Download aborted";
  } else {
  echo 'Click <a href="'.$download.'">HERE</a> to download file';
  }
?>[/code]

Also you do not need to use a while loop since you are only retrieving one row from the table.

now you preview can look like so
[code]<?php
//connection info
$user="blah";
$host="localhost";
$password="blah";
$database="blah";

$connection = mysql_connect($host,$user,$password) or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection) or die ("Couldn't select database");

//chooses which table in the layout to load from
$q="SELECT * from layouts where id='$id'";
$result= mysql_query($q) or die
("Could not execute query : $q." . mysql_error());
$row=mysql_fetch_array($result);
{
//table fields [this may not be the same if you have different fields compared to mine]
    $id=$row["id"]; 
    $title=$row["title"]; 
    $date=$row["date"];
    $type=$row["type"];
    $size=$row["size"]; 
    $designer=$row["designer"]; 
    $designerurl=$row["designerurl"];   
    $previewimage=$row["previewimage"];
    $download=$row["download"]; 
    $dcounter=$row["dcounter"];
    $preview=$row["preview"];
    $pcounter=$row["pcounter"]; 
$q="update layouts set pcounter=pcounter+1 where id='$id'";
$result= mysql_query($q) or die ("Could not execute query : $q." . mysql_error());
  if(!$result){
  echo "Could not update table, Preview aborted";
  } else {
  echo '<img src="'.$previewimage.'">';
  }
?>[/code]

Also you have to remember this is based on your paths and such being correct. I am not sure if you are storing the image in your database or just the name of the image. So the code may need to be adjusted accordingly.

Ray
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.