Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am inserting single quote in textarea in php and its giving me error like Luke's how do i insert single quote without getting error ?

What I have tried:

<pre><form action="postsingle.php" method="post">
<textarea name="tejas"></textarea>
<input type="submit" name="submit" value="submit" />
</form>


$single=$_POST['tejas'];

$insert=mysql_query('insert into mc_test(test_name) values("'.$single.'")');

if ($insert) {echo 'done';} else {echo 'failed';}
Posted
Updated 1-Mar-21 10:10am

Looking at your INSERT statement it's really rather simple: it should be surrounded with double quotes. Like this:
PHP
$insert = mysql_query("insert into mc_test (test_name) values('$single')");



 
Share this answer
 
v2
Quote:
I am inserting single quote in textarea in php and its giving me error like Luke's how do i insert single quote without getting error ?

This problem is also known as 'SQL Injection'
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

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