0

I am taking table name as php variable to insert data into table. But it gives error. What's bad here?

            if($flag == 1)
                $table = 'frrole_pupolar_article';
            else
                $table = 'frrole_category_article';
            $insertQuery1 = "INSERT INTO '.$table.' (`url`, `sentiment`, `category`, `title` ,`time`,`img_url`,`rt_count`,`tweet_count`) VALUES ('".$url."','".$setiment."','".$category."','".$title."','".$time."','".$img_url."','".$rt_count."','".$tweet_count."')";

error:

Error: 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 ''.frrole_category_article.' (`url`, `sentiment`, `category`, `title` ,`time`,`im' at line 1
2
  • 1
    No dots: $insertQuery1 = "INSERT INTO $table ( ..." Commented Jan 13, 2014 at 5:48
  • if all your columns and values are the same why are you putting this in different tables? just add a 'type' column and make your $table = type. I don't see why you need to have same data types in diff tables. Re think your approach. Commented Jan 13, 2014 at 14:23

3 Answers 3

1
$insertQuery1 = "INSERT INTO '" .$table. "' (`url`, `sentiment`, `category`, `title` ,`time`,`img_url`,`rt_count`,`tweet_count`) VALUES ('".$url."','".$setiment."','".$category."','".$title."','".$time."','".$img_url."','".$rt_count."','".$tweet_count."')";

You wrongly written '.$table.' instead of '" .$table. "'

Sign up to request clarification or add additional context in comments.

2 Comments

thanks but still gives error 'Error: 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 ''frrole_category_article' (url, sentiment, category, title , time , `i' at line 1'
$insertQuery1 = "INSERT INTO '" .$table. "' (url, sentiment, category, title, time, img_url, rt_count, tweet_count) VALUES ('".$url."','".$setiment."','".$category."','".$title."','".$time."','".$img_url."','".$rt_count."','".$tweet_count."')"; try this
1

Just do

$insertQuery1 = "INSERT INTO $table (`url`, `sentiment`, `category`, 
                `title` ,`time`,`img_url`,`rt_count`,`tweet_count`) 
                 VALUES ('".$url."','".$setiment."','".$category."',
                 '".$title."','".$time."','".$img_url."','".$rt_count."',
                 '".$tweet_count."')";

remove the concatenations and single quotes

2 Comments

it says : Error: Table 'mysql.frrole_category_article' doesn't exist
remove mysql. out of your table name frrole_category_article
0

If your string is $insertQuery1 = "INSERT INTO '.$table.' (url,sentiment, You cannot escape it by using single quotes '..'

Just do double quotes:

$insertQuery1 = "INSERT INTO ".$table." (`url`, `sentiment`, ....

also for all that is holy, use damn {} for if(){} statements, not using them is poor form

2 Comments

Not if you have to read the code afterwards and everything is a mess.
it says Error: Table 'mysql.frrole_category_article' doesn't exist though table exist

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.