Show Code:
artist-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 artists read page-->
<?php if(!empty($_POST['artist_name'])){ ?>
<meta http-equiv="refresh" content="0.5;URL='artists-read.php'">
<?php } ?>
</head>
<body>
<?php include 'nav.php'; ?>
<?php include 'db-connect.php'; ?>
<main>
<?php
if(empty($_POST)){
//if the user has just clicked on edit
$artist_id=$_GET['artist_id'];
$sql='SELECT * FROM t_artists WHERE artist_id='.$artist_id;
$result=mysqli_query($con,$sql);
$rowArtists=mysqli_fetch_array($result);
?>
<form method="post" action="artist-update.php">
<label>Artist</label>
<input type="text" name="artist_name" value="<?php echo $rowArtists['artist_name']?>">
<!--Create movements Dropdown Menu-->
<label>movements</label>
<select name="movement_fk">
<?php
//populate the drop down menu from the movements table
$sql='SELECT * FROM t_movements ORDER BY movement_name';
$result=mysqli_query($con,$sql);
while ($rowMediums=mysqli_fetch_array($result)){
echo '<option value="'.$rowMediums['movement_id'].'"';
if($rowMediums['movement_id']==$rowArtists['movement_fk']){
echo ' selected ';
}
echo '>';
echo $rowMediums['movement_name'].'</option>'.PHP_EOL;
}
?>
</select>
<!--end of movements Dropdown Menu-->
<label>birth</label>
<input type="number" name="year_birth" value="<?php echo $rowArtists['year_birth']?>">
<label>death</label>
<input type="number" name="year_death" value="<?php echo $rowArtists['year_death']?>">
<!--Pass hidden artist_id as well-->
<input type="hidden" name="artist_id" value="<?php echo $artist_id; ?>">
<input type="submit" value="Update">
</form>
<?php
}
else{
//otherwise - recursive form handling
$artist_name=$_POST['artist_name'];
$artist_id=$_POST['artist_id'];
$movement_fk=$_POST['movement_fk'];
$year_birth=$_POST['year_birth'];
$year_death=$_POST['year_death'];
$sql='UPDATE t_artists SET artist_name="'.$artist_name.'", movement_fk="'.$movement_fk.'",
year_birth="'.$year_birth.'",
year_death="'.$year_death.'"
WHERE artist_id='.$artist_id;
if(mysqli_query($con,$sql)){
echo 'artist has been updated';
}
else{
echo "Error updating artist: " . mysqli_error($con);
}
}
?>
<?php mysqli_close($con); //Close Connection?>
</main>
</body>
</html>