Ok, so I am trying to insert a dynamical data inside a row. This is most likely not the best way to do it, but after banging my head on the wall for hours I still can't understand why the $insert string won't get queried by mysql_query. Even when I echo it and copy what is echoed to the query it works, but querying the variable doesn't.
$insert = '"INSERT INTO '.$_SESSION['tabsel'].' (';
echo "<form method='post' action=''>";
while($row = mysql_fetch_array($result))
{
    echo "Enter ".$row[0]." <input type='text' name='data[]'>";
    echo "<br>";
    $insert .= $row[0].",";
    $_SESSION['insert'] = $insert;
}
echo "<input type='submit' value='Add'>";
echo "</form>";
if(isset($_POST['data']))
{
    $insert = $_SESSION['insert'];
    $strlength = strlen($insert);
    $insert = substr($insert,0,($strlength-1));
    $insert .= " VALUES (";
    foreach($_POST['data'] as $value)
    {
        $insert .= "'$value',";
        $_SESSION['insert'] = $insert;
    }
}
$insert = $_SESSION['insert'];
$strlength = strlen($insert);
$insert = substr($insert,0,($strlength-1));
$insert .= ')"';
$_SESSION['insert'] = $insert;
$insert = $_SESSION['insert'];
echo $insert."<br>";
$seldb = mysql_select_db($_SESSION['sel']);
if($seldb && (!empty($_POST['data'])) && (isset($_SESSION['sel'])) && (isset($_SESSION['tabsel'])))
{
    $insert = $_SESSION['insert'];
    echo $insert;
    $query = mysql_query($insert, $con);
    if($query)
    {
        echo "Record succesfully added!";
    }
    else
    {
        echo mysql_error();
    }
}
Error given:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
"INSERT INTO mtable (id,nr,d,ra) VALUES ('d','d','d','d')"at line 1
 
     
    