0

I tried to update a paragraph from mysql table,but i got error like this

"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 's first-ever super-villainess."

My mysql Query

cmd.CommandText = "UPDATE `moviemaster` SET  `Runtime`='" + runtime + "',`DateMasterId`='" + dateid + "',`Trailer`='" + trailer + "',`Synopsis`='" + synopsis + "' WHERE `MovieMasterId`='" + movieid + "'";

I got error in 'synopsis',it's a big data containing a large paragraph.If i romove 'Synopsis' section from the query,everything working fine.What exactly the problem.How can i resolve this?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Unnikrishnan.S
  • 185
  • 2
  • 17
  • What is the `synopsis` variable _exactly_? `MovieMasterId` seems like a numerical column as well. If so, you don't need single quotes with it. You should always use [parameterized queries](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/) by the way. This kind of string concatenations are open for [SQL Injection](http://en.wikipedia.org/wiki/SQL_injection) attacks. And parameterized statements handle the escape characters as well. – Soner Gönül Nov 19 '15 at 09:37
  • @SonerGönül:its a content of movie synopsis. – Unnikrishnan.S Nov 19 '15 at 09:38
  • Just a side-node: please use parameters instead string-concatenation otherwise your code is potentially open for SQL-Injection. – Marc Nov 19 '15 at 09:40
  • @Unnikrishnan.S No, I mean it's _real_ value that it contains. The value when you see on debugger. – Soner Gönül Nov 19 '15 at 09:43
  • You probably have some sort of character in the paragraph that ends the statement such as a '`'. Using parametrized queries will fix that issue. Add a line break to the update statement and check to see what query is being passed. – Paul Nov 19 '15 at 09:44

2 Answers2

1

@SonerGönül:Ok,fine.. then please show me an example of parameterised query

Sure. I also wanna add a few best practice as well.

using (var con = new SqlConnection(conString))
using(var cmd = con.CreateCommand())
{
     cmd.CommandText = @"UPDATE moviemaster 
                         SET Runtime = @runtime, DateMasterId = @dateid, Trailer = @trailer, Synopsis = @synopsis
                         WHERE MovieMasterId = @movieid";
     cmd.Parameters.Add("@runtime", MySqlDbType.VarChar).Value = runtime; ;
     cmd.Parameters.Add("@dateid", MySqlDbType.VarChar).Value = dateid;
     cmd.Parameters.Add("@trailer", MySqlDbType.VarChar).Value = trailer;
     cmd.Parameters.Add("@synopsis", MySqlDbType.VarChar).Value = synopsis;
     cmd.Parameters.Add("@movieid", MySqlDbType.VarChar).Value = movieid;
     // I assumed your column types are VarChar.
     con.Open();
     cmd.ExecuteNonQuery();
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

Please avoid using inline query. Your database can be subjected to SQL Injection. See this example, on what can be done using SQL Injection.

And use paramterized query instead. Here is the example taken from here. This way, even if your string has special characters, it will not break and let you insert/update/select based on parameters.

private String readCommand = "SELECT LEVEL FROM USERS WHERE VAL_1 = @param_val_1 AND VAL_2 = @param_val_2;";
public bool read(string id)
{
    level = -1;
    MySqlCommand m = new MySqlCommand(readCommand);
    m.Parameters.AddWithValue("@param_val_1", val1);
    m.Parameters.AddWithValue("@param_val_2", val2);
    level = Convert.ToInt32(m.ExecuteScalar());
    return true;
}

and finally, your query will become

cmd.CommandText = "UPDATE `moviemaster` SET  `Runtime`= @param1,`DateMasterId`= @dateid, `Trailer`= @trailer,`Synopsis`= @synopsis WHERE `MovieMasterId`= @movieid";

cmd.Parameters.AddWithValue("@param1", runtime);
cmd.Parameters.AddWithValue("@dateid", dateid);
cmd.Parameters.AddWithValue("@trailer", trailer);
cmd.Parameters.AddWithValue("@synopsis", synopsis);
cmd.Parameters.AddWithValue("@movieid", movieid);
Community
  • 1
  • 1
Yahya
  • 3,386
  • 3
  • 22
  • 40