If I'm understanding what you are doing, you are using a variable variable.
Variable 1 = $name1
Variable 2 = $name2
$name$x won't work, ,neither will $name[$x] (which would be an array)
What you need to do is this,
while ($num<5){
$num++;
$namevar = "name$x";
$thisname = ${$namevar}; // This is the imporant part of this reply.
// Do insert query here. Insert $thisname for name
}
For more information about variable variables check here:
http://php.us.themoes.org/manual/en/language.variables.variable.php