Jump to content

UPLOAD THEN DISPLAY PHOTOS TO/FROM MySQL WITH PHP


Recommended Posts

Hi,

 

I'm trying to do a simple website where I can upload some information about homes and add photos to a MySQL database and then display the information.

 

So I have an html form that sends the details of the home to a php file called details.php. The html form is as follows.

 

<!DOCTYPE html>
<html>
<body>

<form action="details.php" method="POST">
<pre>
Name <input type="" name="name">
Bedrooms <input type="text" name="bedrooms">
Length <input type="text" name="length">
Width <input type="text" name="width">
Serial Number <input type="text" name="serialno">

<input type="submit" value="ADD RECORD">
</pre>
</body>
</html>
The details.php look like the following:
<?php
require_once 'login.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);
if (isset($_POST['delete'])&&
isset($_POST['serialno']))
{
$serialno = get_post($conn, 'serialno');
$query = "DELETE FROM holidayhomes WHERE serialno='$serialno'";
$result = $conn->query($query);
if (!$result) echo "DELETE failed: $query<br>" . $conn->error . "<br><br>";
}
if (isset ($_POST['name']) &&
isset ($_POST['bedrooms']) &&
isset ($_POST['length']) &&
isset ($_POST['width']) &&
isset ($_POST['serialno']))

{
$name = get_post ($conn, 'name');
$bedrooms = get_post ($conn, 'bedrooms');
$length = get_post ($conn, 'length');
$width = get_post ($conn, 'width');
$serialno = get_post ($conn, 'serialno');
$query = "INSERT INTO holidayhomes (name, bedrooms, length, width, serialno) VALUES ('$name','$bedrooms', '$length', '$width', '$serialno')";

$result = $conn->query($query);
if (!$result) echo "INSERT failed: $query<br>" . $conn->error . "<br><br>";
}
$query = "SELECT * FROM holidayhomes";
$result = $conn->query($query);
if (!$result) die ("Database access failed: " . $conn->error);
$rows = $result->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
{
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);


echo <<<_END
<pre>
Name $row[0]
Bedrooms $row[1]
Length $row[2]
Width $row[3]
Serial No $row[10]
MainPic
<img src="$row[4]" width=auto height=auto><br><br>
</pre>
<pre>
<form action="details.php" method="POST">
<input type="hidden" name="delete" value="yes">
<input type="hidden" name="serialno" value="$row[10]">
<input type="submit" value="DELETE RECORD">
</form>
</pre>
<pre>

<form action="mainphoto.php" method="post" enctype="multipart/form-data">
Select main photo:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</pre>
_END;
}
$result->close();
$conn->close();
function get_post($conn, $var)
{
return $conn->real_escape_string($_POST[$var]);
}

?>

​So the above inserts the information (about a home entered in the html form) into the MySQL database then displays the information and an option to delete the homes information then I have a form to upload a photo for the home. For uploading the photo I have a php file called mainphoto.php which is as follows:

 

<?php
require_once 'login.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false){
echo "FIle is an image - " . $check["mime"] . "." ;
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if ($uploadOk == 0){
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)){
echo "The file " . basename( $_FILES["fileToUpload"]["name"]) . " has been uploaded.";
} else {
echo " off!";
}
}
$mainpic = basename( $_FILES["fileToUpload"]["name"]);
$query = "INSERT INTO holidayhomes (mainpic) VALUES ('$mainpic')";
$result = $conn->query($query);
if(!$result) echo "INSERT failed: $query<br>" . $conn->error . "<br><br>";
?>

 

 

 

How do I get the form to submit and add the name of the photo to the same record(row) as the homes information? So then when it iterates through the record(rows) in the MySQL database I can display the photo to the relevant information about the home?

Link to post
Share on other sites

You use UPDATE, indentify record to upadate, using serialnumber? Maybe.

 

Oh, I think I have it now. I changed the form to include name="serialno" in details.php as follows:

 

<form action="mainphoto.php" method="post" enctype="multipart/form-data">

Select main photo:

<input type="hidden" name="serialno" value="$row[10]">

<input type="file" name="fileToUpload" id="fileToUpload">

<input type="submit" value="Upload Image" name="submit">

</form>

</pre>

 

Then included in mainphoto.php the following:

 

$mainpic = basename( $_FILES["fileToUpload"]["name"]);

if(isset($_POST["serialno"])) $serialno = $_POST['serialno'];

$query = "UPDATE holidayhomes SET mainpic='$mainpic' WHERE serialno='$serialno'";

$result = $conn->query($query);

if(!$result) echo "INSERT failed: $query<br>" . $conn->error . "<br><br>";

 

Thanks for all your help. I just need to work out how to keep it all on one page so that it doesn't keep navigating of and stays on the one page like a single page application. It would also be nice to be able to delete the photo from uploads when I click delete "DELETE RECORD".

 

Thanks for all your help!

Edited by rich_web_development
Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...