Thank you in advance.
"Insert Error: 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 ''ID', 'username', 'goal1', 'goaldate') VALUES ('', '', 'goalfield', '')' at line 1"
...
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$result = mysql_query(" SELECT * from login WHERE username = '$username' ") or die(mysql_error());
$row = mysql_fetch_array($result);
$ID = $row['ID'];
$username = $row['username'];
$NumGoals = $row['NumGoals'];
$NumGoals++;
$newgoal = mysql_real_escape_string(htmlspecialchars($_POST['newgoal']));
$newgoalid = "goal";
$newgoalid .= $NumGoals;
$newgoaldate = mysql_real_escape_string(htmlspecialchars($_POST['goaldate']));
// Verify if last column name already exists in table from databasename
$fields = mysql_list_fields('databasename', 'goals');
$columns = mysql_num_fields($fields);
for ($i = 0; $i < $columns; $i++) {$field_array[] = mysql_field_name($fields, $i);}
if (!in_array('$newgoalid', '$newgoaldate', $field_array))
{
// IF non-existant column ALTER table to add goal columns
if (empty($field_array))
{
$result = mysql_db_query("ALTER TABLE 'goals'
ADD '$newgoalid' TEXT NOT NULL,
ADD '$newgoaldate' DATE") or die("Alter Error: ".mysql_error());
}
}
// INSERT data INTO new column's fields to add record (newgoal#, newgoaldate)
$SQL = " INSERT INTO goals ('ID', 'username', '".$newgoalid."', 'goaldate') VALUES ('".$ID."', '".$username."', '".$newgoal."', '".$goaldate."')";
$result = mysql_query($SQL) or die("Insert Error: ".mysql_error());
// Go back succesfully to same page
header ("Location: test.php");
}
...
Edited by doofy, 01 February 2013 - 05:18 PM.












