I have created a image upload for the site i am working on i now want the uer to be able to click on a preview button and this takes them to a separate page where they can see the images that have been uploaded.

for this to happen would i have to send a query to the database to retrieve the information or can i simply display them using the variable name which was set for the images.

Hi Kevin,
the basic question: do you store the images in a database or as files in a file system.

Also, what should I imagine when you say "the variable name which was set for the images"?

so you want to make a thumbnail? also you can make a field called "id" and match the id with the image location. and then select like that.

best way to store an image is to upload it. Store its directory as a varchar in a database then simply echo it back when you need it :D
there are better and more complicated but this is by far the easiest method
:D

hope this helps

i think what you need is this:

$target_path = $target_path . basename( $_FILES);
$_FILES;

all this does is make a variable that holds the directory (ex: "uploads/filename.extension" )
and that you can store in the database as a varchar :D

if you want me to make you a script just ask, np lol im bored.

hope this helps

i have now tried to connect to my database which is set up on the server and i keep getting could not connect. i have db.php page which sets up the database and recall this by using include db.php

Hi Kevin,
there's no way we could check the problem remotely.
Either you have the login data wrong (server name, username, password, db name) or the script doesn't connect or I don't know. Can you attach both db.php and the main script?

here is the db.php code

<?php


$dbhost = 'www.acmeart.co.uk';
$dbusername = 'acmeart';
$dbpasswd = 'all0wn01';
$database_name = 'acmeartdb';
$sitepath = "http://www.acmeart.co.uk/mercury/";
$sitename = "Mercury Email Distribution System";
$adminemail = "mercury@acmeart.co.uk";


$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd")
or die ("Couldn't connect to server.");


$db = mysql_select_db("$database_name", $connection)
or die("Couldn't select database.");
?>

here is the code i am using to connect to the db.

<?php


include "db.php";


$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die     line 168
("QUERY ERROR: ".mysql_error());


$db = mysql_select_db("$database_name", $connection)
or die("QUERY ERROR: ".mysql_error());


// Create table in my_db database
mysql_select_db("$database_name", $connection);
$sql = "CREATE TABLE images
(
imageID int NOT NULL AUTO_INCREMENT,
Broad1 varchar(50),
Broad2 varchar(50),
Broad3 varhar(50)
)";


if (!$con)
{
die('Could not connect: ' . mysql_error());
}


mysql_select_db("$database_name", $con);


mysql_query("INSERT INTO image (Broad1)
VALUES ('$thumb_name')");


mysql_query($sql,$con);


mysql_close($con);


?>

i keep getting the error

(Using password: NO) in /home/acmeart/public_html/mercury/upload.php on line 168 QUERY ERROR: i have labelled line 168 for you

Edited 3 Years Ago by happygeek: fixed formatting

i have changed 2 typo's in the code in the $conn = mysql_connect($dbhost, $dbuser, $dbpass)

$dbuser= $dbusername

$dbpass= $dbpasswd

Hi Kevin,
you will have to change your db login data because now that you posted them here anyone can connect to your database and do whatever he wants.

I ran db.php and I got a valid connection.
To verify it I put this to see what was the mysql_pconnect() result.

var_dump($connection);

Because db.php already connects, you don't have to connect again in your main script. $connection will be available in the main script.
Frankly, you can just not use $connection at all - if you don't need more than one connection in your script.

You don't have to run mysql_select_db() each time before you use a database. It's enough to run it just once in db.php and it will stay selected. You only need to run mysql_select_db() again if you want to use a different database.

The error that you were looking for was $dbusername versus $dbuser - you define and use different variables. The proper solution is to remove the second connect attempt.

Also, you don't need to run mysql_close() at the script's end because you're using permanent connections mysql_pconnect() and they are not affected by mysql_close().

all the passwords and connection details will be changed this is only for testing purposes. thank you for the reply is was helpful.

This article has been dead for over six months. Start a new discussion instead.