<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="content-type">
  <meta name="Author" content="John Donaldson">
  <title>MySQL PHP Forms Test</title>
</head>
<body>
<div class="example-contents">
<div class="phpcode"><code><span class="html"><span class="default">
<h1>Accessing MySQL on the Web Using PHP</h1>
<?php
   // get the artist id and name
   $artist = $_GET["artist"];
   if($artist){
      $arr = explode('|',$artist);
      $cid =  $arr[0];
      if(sizeof($arr)>0){
         $aname = $arr[1];
      }
   }

   ini_set('display_errors', 'On');
   error_reporting(E_ALL);

   $mysqli = new mysqli("sql.cs.oberlin.edu","alice","tiger","moma");
   
   if($mysqli->connect_errno){
      echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
   }

   $mysqli->set_charset('utf8mb4');

  // Form an SQL query  
  $res = $mysqli->query("SELECT DISTINCT cid,displayname FROM artists natural join produced group by cid,displayname having count(*)>10 and count(*)<30 ORDER BY displayname limit 50");
  if(!$res){
    echo "Error in query";
  }

  // Display a drop-down menu containing artist names
  print '<FORM method="get" action="example5.php">';
  print 'Select an Artist from the Artists Table:&nbsp;&nbsp;';
  print '<select name="artist">';

  // For each artist name
  for($row_no = 0; $row_no < $res->num_rows; $row_no++){
    $res->data_seek($row_no);
    $row = $res->fetch_assoc();
    if($row['cid'] === $cid)
       print '<option selected';
    else
       print '<option';
    print ' value="' . $row['cid'] . '|' . $row['displayname'] . '">';
    print($row['displayname']);
    print '</option>';
  }

  ?>

</select>

<input type="submit" value="Submit">
</FORM>

<?php
// if an artist has been chosen, print a table of the artist's artworks
if($artist){
  // An SQL query to get all of the artworks produced by a specified artist
  $res = $mysqli->query("SELECT objectid,title,workdate,medium,creditline,classification,department,dateacquired,url from artworks natural join produced where cid='" . $cid . "' order by objectid");
  if(!$res){
    echo "Error in query";
  }

  // Print the result as a table
  print '<h2>Artworks by '.$aname.'</h2>';

  print '<table border="1" width="90%">'; 

  // Print column headings
  $ncols = $res->field_count;

  print '<tr>';
  $column_names = $res->fetch_fields();
  for ($i = 1; $i <= $ncols; $i++) {
    $column_name = $res->fetch_field()->name;
    print "<td>" . $column_name . "</td>";
  }

  // Print the query results in a table
  while ($row = $res->fetch_row()) {
    print '<tr>';
    foreach ($row as $item) {
	$htmlitem = $item?htmlentities($item):'&nbsp;';
	// if the item begins with http, display it as a hyperlink
	$pos = strpos($htmlitem,'http');
	if($pos === 0){
          print '<td><a href='.$htmlitem.'>'.$htmlitem.'</a></td>';
	}
	else {
	  print '<td>'.($item?htmlentities($item):'&nbsp;').'</td>';
	}
    }
    print '</tr>';
  }
  print '</table>';

}

?>
<br>
<br>
</span><span class="default"></span></span>
</code></div>
</div>
</body>
</html>
