Jump to content

Upload with SQL


p_arc

Recommended Posts

Hi, Am sorry that im just new here and asking for help.

 

Im pretty new to php and I have a been reading a few tutorials about uploading via php and sql .. so i created a little script..

 

I can upload the image to a dir and save the path in sql  ,

Create a Thumbnail and save it to a dir and its path to sql ,

Display the thumbnail & link it to original via dir / and path in sql ,

 

What i cant do is display the thumbnail and link it to the original via the ID in the database..

I dont know how to get the ID .. and am sure this is going to be somthing so easy that ill kick myself for missing ..

 

I want to link the thumbnail to .. say .. viewer.php?id=

 

Here is some code  ...

 

  

$query = "INSERT INTO upload2 (id, name, size, type, path, tpath) ".
"VALUES ('$id','$fileName', '$fileSize', '$fileType', '$filePath', '$thumb')"; 

mysql_query($query) or die('Error, query failed : ' . mysql_error());

mysql_close($conn);

echo "<br><b> $fileName was uploaded </b><br><br>";        
echo "<a href=viewer.php?$id=><img src='$thumb'></a>"; // This is where i want the link but how do i grab the ID I can echo out the filetype and path thumb and everything else except that ID ... 
}

 

Also i have viewer.php file which works providing it has an ID..

 

Any help would be great..

Thanks

David ..

 

 

Link to comment
Share on other sites

First you need to make sure that the table with the image stuff has an id column with AUTO_INCREMENT and it needs to be key.

 

Then everytime you insert something into the database it will automatically give it a new id. Even the ones you already have will get an ID once you do that.

 

Then at the top of your viewer.php file or whichever one is linking add this:

 

$id = isset($HTTP_GET_VARS["id"])?$HTTP_GET_VARS["id"]:"NULL";

 

That means that in site.php?id=XX , $id will be equal to XX.

 

If there is no ?id= trailing the url then $id will be equal to NULL. You can change that to whatever you want.

 

Now that you have the $id part done, you can use this just as you would any variable. i.e.

 

echo "<a href='site.php?id=" . $id . "'>site.php</a>";

 

and in your viewer.php file you'll probably want to add a line like this

 

$sql = mysql_query("SELECT * FROM pictures WHERE id=" . $id . "") or die(mysql_error());

echo "<img src'" . mysql_result($sql,$i,"url") . "'>";

 

which obviously would get the url of the row where id=whatever.

 

Hope that helps.

Link to comment
Share on other sites

First you need to make sure that the table with the image stuff has an id column with AUTO_INCREMENT and it needs to be key.

 

Then everytime you insert something into the database it will automatically give it a new id. Even the ones you already have will get an ID once you do that.

 

Then at the top of your viewer.php file or whichever one is linking add this:

 

$id = isset($HTTP_GET_VARS["id"])?$HTTP_GET_VARS["id"]:"NULL";

 

That means that in site.php?id=XX , $id will be equal to XX.

 

If there is no ?id= trailing the url then $id will be equal to NULL. You can change that to whatever you want.

 

Now that you have the $id part done, you can use this just as you would any variable. i.e.

 

echo "<a href='site.php?id=" . $id . "'>site.php</a>";

 

and in your viewer.php file you'll probably want to add a line like this

 

$sql = mysql_query("SELECT * FROM pictures WHERE id=" . $id . "") or die(mysql_error());

echo "<img src'" . mysql_result($sql,$i,"url") . "'>";

 

which obviously would get the url of the row where id=whatever.

 

Hope that helps.

 

Hi Thanks For the Reply,

 

Yes the database already has ID auto_increment ..

 

I can call viewer.php?id=178 from the browser and it will display the content at that ID..

 

I just cant grab the ID and link it to the thumb on the upload page which is part of code on first post..

 

I added  ..

 

$id = isset($HTTP_GET_VARS["id"])?$HTTP_GET_VARS["id"]:"NULL"; // to the upload file ..

 

echo "<a href='viewer.php?id=" . $id . "'><img src='$thumb'></a>"; // This too...

 

Now when i upload, the thumbnail it links too  /viewer.php?id=NULL

 

I still dont know how to get the ID from the upload..

 


// ...... some code //

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
$thumb = addslashes($thumb);
}

$query = "INSERT INTO upload2 (id, name, size, type, path, tpath) ".
"VALUES ('$id','$fileName', '$fileSize', '$fileType', '$filePath', '$thumb')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

mysql_close($conn);

echo "<br><b> $fileName was uploaded </b><br><br>";
echo "<a href='viewer.php?id=" . $id . "'><img src='$thumb'></a>"; 


}

 

As i say, I can grab everything else from the database except the ID.. i.e  $fileName', '$fileSize', '$fileType', '$filePath', '$thumb

 

Thanks...

David..

 

 

 

 

 

 

 

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.