How can I use a variable to select the SQL table?
$sselect = $_GET['series'];
$episodes = mysqli_query($conn, "SELECT * FROM '$sselect'") or die(mysqli_error());
How can I use a variable to select the SQL table?
$sselect = $_GET['series'];
$episodes = mysqli_query($conn, "SELECT * FROM '$sselect'") or die(mysqli_error());
Why don't use a simple concat?
$sselect = $_GET['series'];
$episodes = mysqli_query($conn, "SELECT * FROM ".$sselect) or die(mysqli_error());
Try this: This should work. Thanks to @Phil he pointed out that table names should be enclosed in back-ticks. I have updated my answer to do that. The extra single quotes are used to complete the MYSQL query.
$sselect = $_GET['series'];
$episodes = mysqli_query($conn, 'SELECT * FROM `'.$sselect.'`') or die(mysqli_error());
UPDATE:
What Phil is saying is that if you use a $_GET variable someone can easily replace the URL with a different table name leaving a huge security risk. For example let's assume I have a table called users
but I am expecting the $_GET
variable to be one of the following: cars
, pencils
, or colors
.
What you want is for the form to return an URL like this:
www.example.com/index.php?series=pencils
But instead the user can change the data to represent this:
www.example.com/index.php?series=users
Allowing the user/attacker access into a confidential table. This is not a good practice and Phil was suggesting you change your schema as in your table structure so the user does not pick the table name.