table name problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
madu
Forum Commoner
Posts: 32
Joined: Sat Dec 25, 2010 3:19 am
Location: india

table name problem

Post by madu »

hi friends,
i have store database table name in $rt which is get from user,,,,,, but when i am using this variable in my query it shows a sql error that is ( 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 '' )

Code: Select all

$un=$_POST['uname'];
$e=$_POST['eid'];
if(isset($_POST[but]))
 {
$sql="insert into ' ".$rt." ' values (' ".$un." ',' ".$e." ')";
if(mysql_query($sql,$con) or die("error in insertion".mysql_error()))
echo "Successfully Added";
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: table name problem

Post by Christopher »

Use backticks, not single quotes, around table and column names.

http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
(#10850)
madu
Forum Commoner
Posts: 32
Joined: Sat Dec 25, 2010 3:19 am
Location: india

Re: table name problem

Post by madu »

if i give table name directly instead of using variable it works,,,,,problem is only om variable
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: table name problem

Post by social_experiment »

Where do you define the value of $rt?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
madu
Forum Commoner
Posts: 32
Joined: Sat Dec 25, 2010 3:19 am
Location: india

Re: table name problem

Post by madu »

$rt value iss defined as

$rt=trim((String)'<script language=javascript>
function show()
{
var show=document.getElementById("ty").options[document.getElementById("ty").selectedIndex].value;
document.write(show);
}
</script>');
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: table name problem

Post by Christopher »

You need to escape data inserted into the database using mysql_real_escape_string().

http://us.php.net/manual/en/ref.mysql.php
(#10850)
Post Reply