Something like this
<?php
$trNum=$_REQUEST["tr_num"];
include ("dbConnect.php");
mysql_query("
CREATE TABLE schedule_".(int)$trNum."
(
sid int primary key auto_increment,
st_name varchar(20), arr_time varchar(5),
dep_time varchar(5),
halt varchar(5),
dist int,
day int
)"
);
echo "Schedule created successfully";
Note as I said in the comments, table names should not be quoted like strings. Don't use the @ sign because it will suppress errors, which we need to make sure things work. mysql_* family of functions are deprecated consider using mysqli_* or PDO.
Lastly and very important, because you are concatenating a variable into SQL it leaves you open to SQL injection attacks. In this case there probably isn't much you can do ( like prepared query etc.. ). That variable is not safe because its supplied by the Client, though $_REQUEST. So you need to check it before using it.
I showed you casting but you can check it several ways including Regx
if( preg_match('/^[0-9]+$/', $trNum ) ){
...create table code
}
This regx checks that it begins and ends with Numbers and only contains Numbers, which would limit what could be injected into the SQL. There may be better ways to sanitize this, but this should be sufficient in this case.
Casting it is ok, but it may cast a string to 0 and try to create a table named schedule_0 if someone tried to hack it, which is not Ideal. However it's better they get the error message for creating a table that exists then the alternative.
For a quick example of how this could be exploited, a user could supply a value of 0( id int ); UPDATE users SET password = 'password'; -- then your query becomes this.
mysql_query("
CREATE TABLE schedule_0( id ind ); UPDATE users SET password = 'password'; --
(
... orignal fields ..
)"
);
So the -- is the start of a comment in SQL, everything past it will not be ran by the DB. Then the 0( id int ); completes your original query creating a minimal table. Lastly we sneak in an Update to change the passwords of all your application users. And then we can login as anyone we want and do all sorts of nasty things.
This is a simplified and imagined case, but it's not to far from what is possible when leaving yourself wide open...
(int)$trNumand cast it to an integer at the very least. But without seeing the source of that variable one cannot be sure...mysqliorPDOmethod..mysqlmethods will be invalid after some days... (or) add this line on top of the pageerror_reporting(1);mysqlalso encountering the same problem..Thanks for your time.