Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to insert student'coe into collegename field of table in mysql but because of
(')it is giving error to avoid it i found that mysql_real_escape_string should be used for but not getting proper syntax of it please help for proper mysql query syntax for it to inserting
JavaScript
student'coe

vaule into the table

collegename='mysql_real_escape_string(student'sCOE)'

What I have tried:

I want to insert student'coe into collegename field of table in mysql but because of
(')it is giving error to avoid it i found that mysql_real_escape_string should be used for but not getting proper syntax of it please help for proper mysql query syntax for it to inserting
JavaScript
student'coe

vaule into the table

collegename='mysql_real_escape_string(student'sCOE)'
Posted
Updated 15-Apr-16 3:20am

1 solution

The problem is that you are using string concatenation to build your queries. That leaves your code vulnerable to SQL Injection[^].

You need to change your code to use parameterized queries instead. That means switching from the mysql_ methods to either MySQLi[^] or PDO[^].

This SO answer[^] has a pretty good explanation.


EDIT: Turns out you're using C#, even though your question refers to a PHP function. Parameterized queries in .NET are simple:
C#
using (var connection = new MySqlConnection("..."))
using (var command = new MySqlCommand("INSERT INTO YourTable (Column) VALUES (@Column)"))
{
    command.Parameters.AddWithValue("@Column", "student'coe");
    
    connection.Open();
    command.ExecuteNonQuery();
}

Alternatively, use Dapper[^].


Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
 
Share this answer
 
v2
Comments
Kishor-KW 15-Apr-16 9:25am    
please give one example for query contain ' (apestop)
Richard Deeming 15-Apr-16 9:27am    
The StackOverflow answer I linked to has two examples of property parameterized queries.
Kishor-KW 15-Apr-16 9:32am    
sql injection occured when we mention control directly into the qurey e.g textbox control. but I am taking values into the variable and then mentaion it into the query.

and ' (apestop) is not accepted by mysql. then how can parameterize query solve the problem?

we have to use \ or something like mysql_real_escape_string but i don't know what exactly to use.
Richard Deeming 15-Apr-16 9:36am    
Well, you clearly don't understand SQL Injection!

If you take the control's value, copy it into a variable, and then concatenate that variable into the query, your code is still vulnerable. Copying the value into a variable doesn't magically make the vulnerability go away.

Using a parameterized query ensures that the data cannot be confused with the commands.

Seriously, read at least one of the links I gave you!
Kishor-KW 15-Apr-16 9:37am    
ok I will. but please tell me will it solved the problem of '(apestop)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900