I might have set this up completely wrong but its working all except the last step. if someone could help and tell me why i would really appreciate. I was able to figure out how to display one picture per unit, or the total pictures under every unit, however i really want to display all the pictures that are connected to a certain unit.
<!doctype html>
<html lang="en">
<head>
<style>
.container{
padding: 1em;
}
</style>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <meta charset="utf-8">
<meta charset="utf-8">
<title>Add an office or warehouse</title>
</head>
<body>
<div class= "container">
<h1>Office or warehouse unit</h1>
<?php // Script 12.4 - add_size.php
/* This script adds a blog size to the database. */
if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.
// Connect and select:
$connection = mysqli_connect('localhost', $user , $password, $database);
mysqli_set_charset($connection, 'utf8');
// Validate the form data:
$problem = FALSE;
if (!empty($_POST['unit']) && !empty($_POST['size'])&& !empty($_POST['price'])){
$unit = mysqli_real_escape_string($connection, trim(strip_tags($_POST['unit'])));
$size = mysqli_real_escape_string($connection,trim(strip_tags($_POST['size'])));
$price= mysqli_real_escape_string($connection,trim(strip_tags($_POST['price'])));
if (isset($_FILES['image'])){
//these are now arrays
for ($i=0; $i<count($_FILES['image']['name']);$i++){
$image = $_FILES['image']['name'][$i];
$tmpname=$_FILES['image']['tmp_name'][$i];
$type_array=$_FILES['image']['type'][$i];
$img_size_array=$_FILES['image']['size'][$i];
$img_error_array=$_FILES['image']['error'][$i];
//print_r($_POST).'</br>';
//print_r($_FILES);
$target = "images/".basename($image);
if (move_uploaded_file($tmpname, $target)) {
$sql= "INSERT INTO photos (image_name, unit)VALUES ('$image', '$unit')";
mysqli_query($connection, $sql);
echo "</br>".$image[$i]." uploaded successfully<br>";
}else{
$msg = $image."Failed to upload";
}
}
}
} else {
print '<p style="color: red;">Please submit a unit and an size and price.</p>';
}
if (!$problem) {
// Define the query:
//$query = "ALTER TABLE 8052Monticello DROP COLUMN image";
$query = "INSERT INTO 8052Monticello (id, unit, size, price, date_entered) VALUES (0, '$unit', '$size', '$price', NOW())";
// Execute the query:
if (@mysqli_query($connection, $query)) {
print '<p>The unit has been added!</p>';
// why doesnt print "$msg"; work when using $i
} else {
print '<p style="color: red;">Could not add the unit because:<br>' . mysqli_error($connection) . '.</p><p>The query being run was: ' . $query . '</p>';
print "$msg";
}
mysqli_close($connection); // Close the connection.
} // No problem!
} // End of form submission IF.
// Display the form:
?>
<form action="add_entry.php" method="post" enctype="multipart/form-data">
<p>Enter Unit: <input type="text" name="unit" size="40" maxsize="100"></p>
<p>Enter Size in Sq Feet: <input type="number" name="size" size="40" maxsize="100"></p>
<p>Enter Price: <input type="text" name="price" size="40" maxsize="100"></p>
<p>Upload photos: <input type="file" name="image[]" multiple=""> </p>
<input type="submit" name="submit" value="Post This unit!">
</form>
</div>
</body>
</html>
The above is working as it should its uploading the files and storing their referances in the pictures table, I have all the unit information in the 8052monticello table and the pictures are going into a separate pictures table, using the unit name entered in by the user as the foreign key.
here is view_units.php
<!doctype html>
<html lang="en">
<head>
<style>
.container{
padding: 1em;
}
</style>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<meta charset="utf-8">
<title>View the units @ the office buildings</title>
</head>
<body>
<div class="container">
<h1>Monticello</h1><hr>
<?php // Script 12.6 - view_BUILDINGS.php
/* This script retrieves blog BUILDINGS from the database. */
/* for file writing enable
$search_dir = '../uploads';
$contents = scandir($search_dir);
echo '<pre>';
print_r($contents);
echo '</pre>';
*/
// Connect and select:
$connection = mysqli_connect('localhost', 'user', 'password', 'BUILDINGS');
// Define the query:
//$query = 'SELECT * FROM 8052monticello ORDER BY date_entered DESC';
$query = 'SELECT 8052monticello.UNIT, 8052monticello.id, 8052monticello.SIZE, 8052monticello.PRICE, photos.image_name, photos.unit';
$query.= ' FROM 8052monticello, photos';
//$query.= ' WHERE 8052monticello.UNIT = photos.unit';
if ($r = mysqli_query($connection, $query)) { // Run the query.
// Retrieve and print every record:
while ($row = mysqli_fetch_assoc($r)) {
// echo '<pre>';
// print_r($row);
// echo '</pre>';
/*
foreach ($contents as $item) {
if ( (is_file($search_dir . '/' . $item)) AND (substr($item, 0, 1) != '.') ) {
print "<img src=\"../uploads/$item\"
width='100'>";
}
}
*/
if ($row['image_name']){
foreach ($row as $image) {
echo "<img src='images/".$row['image_name']."' width='100'>";
// echo '<pre>';
//print_r($row);
//echo '</pre>';
}
}
print "<p><h3>{$row['UNIT']} Unit #</h3>
{$row['SIZE']} Sq Feet<br>
{$row['PRICE']} Monthly rent<br>
<a href=\"edit_UNIT.php?id={$row['id']}\">Edit</a>
<a href=\"delete_UNIT.php?id={$row['id']}\">Delete</a>
</p><hr>\n";
}
} else { // Query didn't run.
print '<p style="color: red;">Could not retrieve the data because:<br>' . mysqli_error($connection) . '.</p><p>The query being run was: ' . $query . '</p>';
} // End of query IF.
mysqli_close($connection); // Close the connection.
?>
</div>
</body>
</html>
here is the photos table:
and here is the 8052monticello table:
so to recap, how can i get the script to print out all the images associated with a particular upload:
i.e. the user will enter the data in the form I.E. unit size, price, and unit name,
along with the images he wants to upload, they each go to their respective table and the unit name is the foreign key. but I am having trouble displaying each unit with its corresponding connected photos.