Modifying database using POSTed variables in PHP I am writing PHP code to update a database, but this code is extremely repetitive and thus error prone. I am effectively typing the variable names six times each! Can this be improved without loosing functionality? I am certain there must be better way!

First, the definition of the demandArrayValue function:

function demandArrayValue($array, $key) {
  $value = isset($array[$key]) ? $array[$key] : null;
  if (is_null($value)) {
    throw new Exception("Failed to get value for '".$key."'.");
  }
  return $value;
}


Next an example of my extremely repetitive code:

try{
  if ($_POST) {
    $var1 = demandArrayValue($_POST, "Var1");
    $var2 = demandArrayValue($_POST, "Var2");
    $var3 = demandArrayValue($_POST, "Var3");
    [ ... and so on. ]

    $query = "UPDATE Table SET Var1 = :var1, Var2 = :var2, Var3 = :var3, [...] WHERE VarX = :varX";
    $stmt = $db-&gt;prepare($query);
    $stmt-&gt;bindParam(':var1', $Var1);
    $stmt-&gt;bindParam(':var2', $Var2);
    $stmt-&gt;bindParam(':var3', $Var3);
    [ ... and so on. ]
    $stmt-&gt;execute();
  }
}
catch [ ... ]


Can a better PHP programmer than I am help me out please? Many many thanks in advance!

Disclaimer: I asked this same question on stackoverflow and was pointed to this site as a potentially better location.  I have no idea how you move a question from one site to another, so I am sorry to duplicate this.
If I were you, I'd create a function that looked like this:

function getArrayValues( array $array, array $keys)
{
    $result = array();
    foreach($keys as $key)
    {
        if (!isset($array[$key]))
        {
            throw new RuntimeException($key.' is not set');
        }
        $result[$key] = $array[$key];
    }
    return $result;
}


This function will return an array, containing just those keys you were actually looking for. Then, in your try-catch block, I'd write this:

$values = getArrayValues($_POST, array('var1','var2','var3'));
$stmt = $db-&gt;prepare('your query here');
$stmt-&gt;execute($values);


And that's it.
Now I do have some worries/thoughts/suggestions about your code, but I'll add them in a future edit.

Edit:
You're throwing exceptions when an array-key does not exist. In this particular case, that might be desirable. You can't insert a row in your db, lest all data is at hand. I get that. But how often might you end up writing either:

try
{
    $var = demandArrayValue($someArray, 'isNotSet');
}
catch(Exception $e)
{
    $var = 'default value';
}


Or, to avoid having to write the try-catch too many times: 

$var = isset($someArray['notSet'] ? $someArray['notSet'] : 'default value';


You could write 2 distinct functions: one throwing exceptions, one that doesn't, but that's just silly. getting array-keys shouldn't throw an exception. your code should be able to handle a missing parameter.
What's more, you place the demandArrayValue in the same try-catch block as all of your DB operations. If you're using PDO, and you've set the PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION, then you're actually handling 2 distinct exceptions in the same catch block:

catch(Exception $e)
{//you have to catch Exception, since that's what demandArrayValue throws
    if ($e instanceof PDOException)
    {
        $db-&gt;rollBack();
        echo $e-&gt;getMessage();//handle DB exception
    }
    else
    {
        echo $e-&gt;getMessage();//handle other Exception
    }
}


Now, that's not nice. Besides, like I said before: dealing with the request parameters is something that is, actually, quite a complex task, but most, if not all frameworks, implement a (series of) request objects. You can write your own, too. 
Once you've done that, you can actually use the getKey or get or getAll methods you might implement:

class PostRequest
{
    private $raw = null;
    private $data = null;
    public function __construct(array $data = null)
    {
        $data = $data ?: $_POST;
        $this-&gt;raw = $data;
        $this-&gt;setData();
    }
    public function get($name, $default = null)
    {
        if (isset($this-&gt;data[$name]))
        {
            return $this-&gt;data[$name];
        }
        return $default;
    }
    private function setData()
    {
        $this-&gt;data = array();
        foreach ($this-&gt;raw as $k =&gt; $v)
        {//validate, sanitize, decode... do whatever
            $this-&gt;data[$k] = trim($v);
        }
        return $this;
    }
}


You could expand on this basic template, with custom email validation, or number formatting and what have you, but you could just  as well implement some form of constant-system, to set the get method's behaviour:

const POST_EXCEPTION = 1;
const POST_DEFAULT = 0;
private $exceptionMode = false;

public function setExceptionMode($exceptionMode = self::POST_DEFAULT)
{
    $this-&gt;exceptionMode = !!$exceptionMode;
    return $this;
}
public function get($name, $default = null)
{
    if (!isset($this-&gt;data[$name]))
    {
        if ($this-&gt;exceptionMode === true)
        {
            throw new Exception($name. ' key is not set');
        }
        return $default;
    }
    return $this-&gt;data[$name];
}


This get method does everything yours does, and more... but please, do look into the linked answer, and look into how other frameworks deal with the request. It's all been done before, and you're free to copy with pride