0
$tablename = "channel";

mysql_query("INSERT INTO '".$tablename."' (episode_name,episode_title,episode_date)
  values ('$videoname','$videotitle','$date')");

 

4

3 Answers 3

1

In PHP a double quoted string literal will expand scalar variables. So that can be done like this

$sql = "INSERT INTO $tablename (episode_name,episode_title,episode_date)
                        values ('$videoname','$videotitle','$date')";

I assume you thought that the single quotes were requred around the table name, they are not in fact they are syntactically incorrect.

You may wrap the table name and the columns names in backtick like this

$sql = "INSERT INTO `$tablename` (`episode_name`,`episode_title`,`episode_date`)
                        values ('$videoname','$videotitle','$date')";

The reason that the Values(....) are wrapped in single quotes is to tell MYSQL that these are text values, so that is not only legal syntax but required syntax if the columns are defined as TEXT/CHAR/VARCHAR datatypes

However I must warn you that

the mysql_ database extension, it is deprecated (gone for ever in PHP7) Specially if you are just learning PHP, spend your energies learning the PDO database extensions. Start here its really pretty easy

And

Your script is at risk of SQL Injection Attack Have a look at what happened to Little Bobby Tables Even if you are escaping inputs, its not safe! Use prepared statement and parameterized statements

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

Comments

0

Dont use quotes arround table name or use backtick

   mysql_query("INSERT INTO $tablename (episode_name,episode_title,episode_date)
    values ('$videoname','$videotitle','$date')");

Comments

0
"INSERT INTO `$tablename` (episode_name,episode_title,episode_date) values ('$videoname','$videotitle','$date')";

OR

"INSERT INTO `".$tablename."` (episode_name,episode_title,episode_date) values ('$videoname','$videotitle','$date')";

Comments

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.