I am reading a CSV file and need to add the information into an existing database. I am able to read and display the information correctly.

When I try the code below, I get the error *"Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 51".

$insert_csv['first_name'] = $csv_array[1];

$insert_csv['first_name'] = substr($insert_csv['first_name'], 1, strlen($insert_csv['first_name']) - 2); //remove quotes and comma

settype($insert_csv['first_name'], "string");

This is line 51 => $query = "INSERT INTO mailing (`test`) VALUES ($insert_csv['first_name'])";

The field "test" is set for type of text. When set for VARCHAR I get the same error.

If I try this sample code...
$insert_csv['first_name'] = "Clifford";
I get the same error. But if I try
$query = "INSERT INTO mailing (test) VALUES ('Clifford')";
I do not get the error.

How do I get this to work without causing an error?

Most probably error is due to variables not working inside quotes.
try this:-

$query = "INSERT INTO mailing (`test`) VALUES ('".$insert_csv['first_name']."')";

If still it is not working do let us know the error you get b adding die statement after running query.

you cannot use the array reference inside the "" marks without wrapping the variable with {} so
$query = "INSERT INTO mailing (test) VALUES ('${insert_csv['first_name']}')";
should work better

Member Avatar

As THPG suggests, but with the $ sign inside the brace:

"INSERT INTO mailing (`test`) VALUES ('{$insert_csv['first_name']}')"