Show Code:
piece-update.php
<?php include 'session.php'; ?> <!DOCTYPE html>
<html>
<head>
<title>Alexandria</title>
<link href="style1.css" rel="stylesheet" type="text/css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--Refresh back to pieces read page-->
<?php if(!empty($_POST['piece_name'])){ ?>
<meta http-equiv="refresh" content="1;URL='pieces-read.php'">
<?php } ?>
</head>
<?php include 'db-connect.php'; ?>
<?php
if(empty($_POST)){ //if the user has just clicked on edit
$piece_id=$_GET['piece_id'];
$sql='SELECT * FROM t_pieces WHERE piece_id='.$piece_id;
$result=mysqli_query($con,$sql);
$rowPieces=mysqli_fetch_array($result);
$piece_name=$rowPieces['piece_name'];
$piece_image=$rowPieces['piece_image'];
$piece_longitude=$rowPieces['piece_longitude'];
$piece_latitude=$rowPieces['piece_latitude'];
?>
<body onload="initMap(<?php
if($piece_longitude!='' && $piece_latitude!=''){
echo $piece_longitude.','.$piece_latitude;
}
?>)">
<?php include 'nav.php'; ?>
<main>
<form method="post" action="piece-update.php" enctype="multipart/form-data">
<label>Piece Name</label>
<input type="text" name="piece_name" value="<?php echo $piece_name?>">
<!--Create Artists Dropdown Menu-->
<label>artists</label>
<select name="artist_fk">
<?php
//populate the drop down menu from the artists table
$sql='SELECT * FROM t_artists';
$result=mysqli_query($con,$sql);
while ($rowArtists=mysqli_fetch_array($result)){
echo '<option value="'.$rowArtists['artist_id'].'"';
if($rowArtists['artist_id']==$rowPieces['artist_fk']){
echo ' selected ';
}
echo '>';
echo $rowArtists['artist_name'].'</option>'.PHP_EOL;
}
?>
</select>
<!--end of artists Dropdown Menu-->
<!--Create mediums Dropdown Menu-->
<label>mediums</label>
<select name="medium_fk">
<?php
//populate the drop down menu from the mediums table
$sql='SELECT * FROM t_mediums';
$result=mysqli_query($con,$sql);
while ($rowMediums=mysqli_fetch_array($result)){
echo '<option value="'.$rowMediums['medium_id'].'"';
if($rowMediums['medium_id']==$rowPieces['medium_fk']){
echo ' selected ';
}
echo '>';
echo $rowMediums['medium_name'].'</option>'.PHP_EOL;
}
?>
</select>
<!--end of mediums Dropdown Menu-->
<label>Artist Image: </label>
<input type="file" name="fileToUpload" id="fileToUpload">
<!--Google Maps API Location-->
<label>Location: </label>
<div id="search-container">
<input id="search-input" type="text" placeholder="Search for Location">
<button id="search-button" type="button" onclick="searchLocation()">Search</button>
</div>
<div id="map"></div>
<label for="lng">Longitude:</label>
<input type="text" id="lng" name="piece_longitude" value="<?php echo $piece_longitude?>">
<label for="lat">Latitude:</label>
<input type="text" id="lat" name="piece_latitude" value="<?php echo $piece_latitude?>">
<!--End Google Maps API Location-->
<!--Pass hidden piece_id as well-->
<input type="hidden" name="piece_id" value="<?php echo $piece_id;?>">
<!--Pass hidden existing piece_image in case user doesn't update image-->
<input type="hidden" name="piece_image" value="<?php echo $piece_image;?>">
<input type="submit" value="Update">
</form>
<?php
}
else{
//otherwise - recursive form handling
$piece_name=$_POST['piece_name'];
$piece_id=$_POST['piece_id'];
$artist_fk=$_POST['artist_fk'];
$medium_fk=$_POST['medium_fk'];
$piece_longitude=$_POST['piece_longitude'];
$piece_latitude=$_POST['piece_latitude'];
include 'image-handler.php';
if($target_file=="images/"||$target_file==""){
$target_file=$_POST['piece_image'];
}
$sql='UPDATE t_pieces SET piece_name="'.$piece_name.'", artist_fk="'.$artist_fk.'",medium_fk="'.$medium_fk.'",piece_image="'.$target_file.'",piece_longitude="'.$piece_longitude.'",
piece_latitude="'.$piece_latitude.'"
WHERE piece_id='.$piece_id;
echo $sql;
if(mysqli_query($con,$sql)){
echo 'Piece has been updated.';
}
else{
echo "Error updating Piece: " . mysqli_error($con);
}
}
?>
<?php mysqli_close($con); //Close Connection?>
</main>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyANqabCeXfuvJLMFOmswsDin9bAJzOSGF8"></script>
<script src="alexandria-maps.js"></script>
</body>
</html>