foreign key must refrence from a Primary key then use a primary key on name,
$sql="CREATE TABLE test(name varchar(20) PRIMARY KEY,age integer)";
and also change on
$sql="CREATE TABLE test2(name varchar(10),FOREIGN KEY (name) REFERENCES test(name))";
if your name
field have chance to duplicate then take one another field id
and use it to reference.