I found the issue and now my insert query works, but I don't understand why. Here is my current working query:
$add_movie = mysql_query("INSERT INTO ownedmovies VALUES ('', '{$movie_data['Title']}',
'{$movie_data['Year']}', '{$movie_data['Rated']}', '{$movie_data['Runtime']}',
'{$movie_data['Genre']}', '{$movie_data['Director']}', '{$movie_data['Writer']}',
'{$movie_data['Actors']}', \"{$movie_data['Plot']}\", '{$movie_data['imdbRating']}')");
Notice that I used double quotes around the plot field and normal around everything else. When I did the plot field the same way as the others, it would not error but nothing would get inserted into the table... now it works perfectly.
Could anyone enlighten me on why this is?
Thank you
I suspect that your plot string contains a single quote. To avoid the problem you should be escaping your string values using mysql_real_escape_string
, or (better) use parameterized queries.
Your "solution" of changing the single quote to a double quote may appear to work in this case, but it will fail if the plot string contains a double quote.
Double and single quotes are used together to keep the program from confusing where the query ended.
for example a query
("Select 'name' From 'contacts'")
If you used all double quotes, then it would look like this
("Select "name" From "contacts"")
And the program could be let to think the actual query is "Select "
That is how I think it is.
In PHP double quotes will parse variables in side it, the single quotes inside them will just be single quotes in the string.
To pass varchar parameters to mysql, they need to be enclosed in quotes, so the single quotes are getting passed to mysql as the varchar parameters.
Your php variables are being parsed inside the double quotes and the sql string will contain the values you pass it in the variables.
Take care when using double quotes in MySQL queries - they have different meanings depending on the SQL mode that MySQL is running in.
select "fred" from table
Will return the values from the column fred when in ANSI_QUOTE mode otherwise it will return the literal value fred.