mjgdunne Posted April 30, 2008 Share Posted April 30, 2008 Hi all, i have a form which takes in car make, car model, car registration, and an image of the car. The image is stored in a directory, and the make model and registration is stored in the database, i have this working. But what i need is to be able to have a link in the database to the uploaded file so i can display all of the details together. Here is my form: <form enctype="multipart/form-data" action="upload.php" method="post" name="upload_file"> <input type="hidden" name="MAX_FILE_SIZE" value="5000000"> Please enter the car make: <input name=mymake><br> Please enter the car model: <input name=mymodel><br> Please enter the car registration: <input name=myreg><br> Please select an image to upload: <input name="userfile" type="file"></br> <input type="submit" value="upload" name="file_uploaded"> </form> Here is the code for placing the details in the database and the image into the directory: <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="reff"; // Table name // Connect to server and select databse. mysql_connect("$host", "root", "root")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $mymake=$_POST['mymake']; $mymake=$_POST['mymodel']; $myreg=$_POST['myreg']; $userfile=$_POST['userfile']; mysql_select_db("test"); $result = mysql_query("INSERT INTO reff (make, model, registration, imgdata) VALUES ('$_POST[mymake]', '$_POST[mymodel]', '$_POST[myreg]', '$_POST[userfile]')"); if (is_uploaded_file($_FILES['userfile']['tmp_name'])) { $file_realname = $_FILES['userfile']['name']; copy($_FILES['userfile']['tmp_name'], realpath("upload").trim($file_realname)); } else { echo "<b><font color=red>No file uploaded.</font></b><BR>No file available or file too big to upload."; } echo $file_realname; ?> Quote Link to comment https://forums.phpfreaks.com/topic/103587-solved-placing-link-into-database/ Share on other sites More sharing options...
derrick1123 Posted April 30, 2008 Share Posted April 30, 2008 I am not the best at PHP & MySQL but.... $result = mysql_query("INSERT INTO reff (make, model, registration, imgdata) VALUES ($_POST[mymake], $_POST[mymodel], $_POST[myreg], $_POST[userfile]) "); I don't think you need to quote variables. Once again I am not the best... -derrick1123 Quote Link to comment https://forums.phpfreaks.com/topic/103587-solved-placing-link-into-database/#findComment-530471 Share on other sites More sharing options...
robos99 Posted April 30, 2008 Share Posted April 30, 2008 I'm a little rusty on file uploads but I don't beleive you can use $_POST[userfile] as your file name, which I think is what you're trying to do. You need to use $_FILES['userfile']['name'] combined with your path to where you're storing the image files to create the full URL to the images. Alternatively, you could just store the file name in the database, and when building your links on the other end include the static path to the images. Quote Link to comment https://forums.phpfreaks.com/topic/103587-solved-placing-link-into-database/#findComment-530480 Share on other sites More sharing options...
mjgdunne Posted April 30, 2008 Author Share Posted April 30, 2008 Hi all thanks for your replys, i have managed to insert the image name into the database, using: $result = mysql_query("INSERT INTO reff (make, model, registration, image) VALUES ('$_POST[mymake]', '$_POST[mymodel]', '$_POST[myreg]', '$file_realname')"); My question now is when i want to get the image out, how do i echo it out, i need to code in the static path? Any suggestions on how to do this thanks? Quote Link to comment https://forums.phpfreaks.com/topic/103587-solved-placing-link-into-database/#findComment-530515 Share on other sites More sharing options...
mjgdunne Posted April 30, 2008 Author Share Posted April 30, 2008 Hi i have written this search script which print out all the details, it prints out the car make, model, registration, but the image doesnt display, i dont think its getting the link. Here is my code, any help would be great. <?php $host="localhost"; // Host name $username="root"; // Mysql username $password="root"; // Mysql password $db_name="test"; // Database name $tbl_name="reff"; // Table name // Connect to server and select databse. mysql_connect("$host", "root", "root")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $pid = $_GET['pid']; $sql = "SELECT * FROM reff WHERE `pid` = '$pid'"; $result = mysql_query($sql) or die(mysql-error()); $row = mysql_fetch_assoc($result); echo "<table border=\"1\" align=\"center\">"; echo "<tr><th>ID</th>"; echo "<td>"; echo $row['pid']; echo "</td>"; echo "<tr><th>Make</th>"; echo "<td>"; echo $row['make']; echo "</td>"; echo "<tr><th>Model</th>"; echo "<td>"; echo $row['model']; echo "</td>"; echo "<tr><th>Registration</th>"; echo "<td>"; echo $row['registration']; echo "</td>"; echo "<tr><th>Image</th>"; echo "<td>"; echo "<img src='upload/<$file_realname>'>"; echo "</td>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/103587-solved-placing-link-into-database/#findComment-530532 Share on other sites More sharing options...
947740 Posted April 30, 2008 Share Posted April 30, 2008 Can you tell us what the value of the link is in the database? Quote Link to comment https://forums.phpfreaks.com/topic/103587-solved-placing-link-into-database/#findComment-530550 Share on other sites More sharing options...
mjgdunne Posted April 30, 2008 Author Share Posted April 30, 2008 What do you mean when you say link, i have the image name in the database and the image is located in directory. I have the name of the images saved as images in the database. Quote Link to comment https://forums.phpfreaks.com/topic/103587-solved-placing-link-into-database/#findComment-530556 Share on other sites More sharing options...
robos99 Posted April 30, 2008 Share Posted April 30, 2008 Can you show us exactly what is in the image column? Assuming you have just the image name, you might want to try something like this: echo "<img src='path/to/your/image/" . $file_realname . "'>" Quote Link to comment https://forums.phpfreaks.com/topic/103587-solved-placing-link-into-database/#findComment-530693 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.