Logo  

CS479/579 - Web Programming II

Displaying exercises/e10/solution/update.php

<?php
$myconn = new mysqli("localhost", "sbaker", "sbaker", "sbaker");
// create table `grades` (
//   `sid`		int		not null default 0,
//   `tid`		int		not null default 0,
//   `points`	float		default null,
//   index	`sid` (`sid`),
//   index `tid` (`tid`)
// );

$tid = isset($_POST['tid'])? (int)$_POST['tid'] : null;
$sid = isset($_POST['sid'])? (int)$_POST['sid'] : null;
if ($sid == null || $tid == null) {
  echo json_encode("Missing sid or tid");
  exit(0);
}

if (isset($_POST['points'])) {
  if ($_POST['points'] == "") $points = null;
  else $points = (float)$_POST['points'];
} else $points = null;

$myconn->query("lock tables grades write");

try {
  $stmt = $myconn->prepare("update grades set points=? where tid=? and sid=?");
  $stmt->bind_param("dii", $points, $tid, $sid);
  $stmt->execute();
  $stmt->close();
} finally {
  $myconn->query("unlock tables");
}

echo json_encode($points);
?>