Jump to content

Recommended Posts

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;
?>

Link to comment
https://forums.phpfreaks.com/topic/103587-solved-placing-link-into-database/
Share on other sites

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

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.

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?

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>";


?>

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.